Python - How To Detect Key Presses? - Stack Overflow

Just browsing Stack Overflow? Help us improve your experience. Sign up for research
    1. Home
    2. Questions
    3. Tags
    4. Users
    5. Companies
    6. Labs
    7. Jobs
    8. Discussions
    9. Collectives
    10. Communities for your favorite technologies. Explore all Collectives

  1. Teams

    Ask questions, find answers and collaborate at work with Stack Overflow for Teams.

    Try Teams for free Explore Teams
  2. Teams
  3. Ask questions, find answers and collaborate at work with Stack Overflow for Teams. Explore Teams

Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

Get early access and see previews of new features.

Learn more about Labs How to detect key presses? Ask Question Asked 10 years, 5 months ago Modified 1 year, 2 months ago Viewed 1.0m times 213

I am making a stopwatch type program in Python and I would like to know how to detect if a key is pressed (such as p for pause and s for stop), and I would not like it to be something like raw_input, which waits for the user's input before continuing execution.

Anyone know how to do this in a while loop?

I would like to make this cross-platform but, if that is not possible, then my main development target is Linux.

Share Improve this question Follow edited Sep 29, 2023 at 15:39 General Grievance's user avatar General Grievance 4,96737 gold badges36 silver badges53 bronze badges asked Jun 6, 2014 at 1:26 lobuo's user avatar lobuolobuo 2,3653 gold badges15 silver badges8 bronze badges 1
  • 2 for OS X stackoverflow.com/a/47197390/5638869 works in Python 2 and 3 – neoDev Commented Nov 9, 2017 at 8:56
Add a comment |

17 Answers 17

Sorted by: Reset to default Highest score (default) Trending (recent votes count more) Date modified (newest first) Date created (oldest first) 144

Python has a keyboard module with many features. Install it, perhaps with this command:

pip3 install keyboard

Then use it in code like:

import keyboard # using module keyboard while True: # making a loop try: # used try so that if user pressed other than the given key error will not be shown if keyboard.is_pressed('q'): # if key 'q' is pressed print('You Pressed A Key!') break # finishing the loop except: break # if user pressed a key other than the given key the loop will break Share Improve this answer Follow edited Nov 19, 2019 at 19:31 Community's user avatar CommunityBot 11 silver badge answered Jun 26, 2017 at 6:35 user8167727user8167727 14
  • 6 I am not sure for linux but it works on Windows for me.user8167727 Commented Jun 26, 2017 at 6:52
  • 213 keyboard apparently requires root in linux :/ – Inaimathi Commented Nov 14, 2017 at 16:05
  • 8 "To avoid depending on X, the Linux parts reads raw device files (/dev/input/input*) but this requries root." – jrouquie Commented Aug 6, 2018 at 13:39
  • 13 I don't see why would the try: except: be useful. – TypicalHog Commented Sep 26, 2018 at 21:04
  • 4 This solution seems to be using a lot of CPU. Am I alone on that? – jason Commented Jun 8, 2020 at 2:48
| Show 9 more comments 109

For those who are on windows and were struggling to find an working answer here's mine: pynput.

Here is the pynput official "Monitoring the keyboard" source code example:

from pynput.keyboard import Key, Listener def on_press(key): print('{0} pressed'.format( key)) def on_release(key): print('{0} release'.format( key)) if key == Key.esc: # Stop listener return False # Collect events until released with Listener( on_press=on_press, on_release=on_release) as listener: listener.join()

The function above will print whichever key you are pressing plus start an action as you release the 'esc' key. The keyboard documentation is here for a more variated usage.

Markus von Broady highlighted a potential issue that is: This answer doesn't require you being in the current window to this script be activated, a solution to windows would be:

from win32gui import GetWindowText, GetForegroundWindow current_window = (GetWindowText(GetForegroundWindow())) desired_window_name = "Stopwatch" #Whatever the name of your window should be #Infinite loops are dangerous. while True: #Don't rely on this line of code too much and make sure to adapt this to your project. if current_window == desired_window_name: with Listener( on_press=on_press, on_release=on_release) as listener: listener.join() Share Improve this answer Follow edited Jun 5, 2023 at 22:53 Gabriel Staples's user avatar Gabriel Staples 51k30 gold badges269 silver badges347 bronze badges answered Nov 8, 2018 at 15:03 Mitrek's user avatar MitrekMitrek 1,3421 gold badge8 silver badges11 bronze badges 10
  • 13 @nimig18 ...and doesn't require root :) – c z Commented Aug 23, 2019 at 7:50
  • 7 There's a problem with this solution (not sure about alternatives): the key doesn't have to be pressed inside a console window for it to take effect. Imagine having a script that does some job until ESC is pressed, but then you press it in another program. – Markus von Broady Commented Sep 14, 2019 at 9:04
  • 1 @MarkusvonBroady I guess that win32gui would be enough to solve it, I've edited my answer in a way that would potentially solve it to windows users at least. – Mitrek Commented Oct 4, 2019 at 20:33
  • @Mitrek I tried this, but my code stops further execution and is stuck here. It works like input(). I have the code executing in selenium, firefox, but as soon as this sequence is encountered, there's no further action. – Lakshmi Narayanan Commented Oct 17, 2019 at 7:43
  • 2 Should have been the accepted answer, for it works both in linux and windows – Akash Karnatak Commented Apr 22, 2020 at 9:34
| Show 5 more comments 96

More things can be done with keyboard module. You can install this module using pip install keyboard Here are some of the methods:

Method #1:

Using the function read_key():

import keyboard while True: if keyboard.read_key() == "p": print("You pressed p") break

This is gonna break the loop as the key p is pressed.

Method #2:

Using function wait:

import keyboard keyboard.wait("p") print("You pressed p")

It will wait for you to press p and continue the code as it is pressed.

Method #3:

Using the function on_press_key:

import keyboard keyboard.on_press_key("p", lambda _:print("You pressed p"))

It needs a callback function. I used _ because the keyboard function returns the keyboard event to that function.

Once executed, it will run the function when the key is pressed. You can stop all hooks by running this line:

keyboard.unhook_all()

Method #4:

This method is sort of already answered by user8167727 but I disagree with the code they made. It will be using the function is_pressed but in an other way:

import keyboard while True: if keyboard.is_pressed("p"): print("You pressed p") break

It will break the loop as p is pressed.

Method #5:

You can use keyboard.record as well. It records all keys pressed and released until you press the escape key or the one you've defined in until arg and returns a list of keyboard.KeyboardEvent elements.

import keyboard keyboard.record(until="p") print("You pressed p")

Notes:

  • keyboard will read keypresses from the whole OS.
  • keyboard requires root on linux
Share Improve this answer Follow edited Jul 16, 2021 at 5:58 answered Aug 25, 2019 at 8:15 Nouman's user avatar NoumanNouman 7,2837 gold badges36 silver badges63 bronze badges 10
  • 64 The biggest NEGATIVE of using keyboard module is its requirement you run as ROOT user. This makes the module verboten in my code. Just to poll whether a key has been pressed does not require root privileges. I have read the doc and understand why the limitation exits in the module, but look elsewhere if all you need is to poll a key... – muman Commented Aug 30, 2019 at 18:19
  • 1 Very helpful info shared, Sir! I wanted to know whether I can use keyboard.wait() to wait for more than 1 key, and continue if either of them is pressed – Preetkaran Singh Commented Nov 19, 2019 at 17:19
  • @PreetkaranSingh wait() doesn't give this functionality. You will have to use keyboard.read_key() with an if condition packed in a while loop. See the method #1 – Nouman Commented Nov 19, 2019 at 18:35
  • 1 Thanks Sir!, would you like to shed some light on the suppress keyword usage in keyboard.read_key(), when to use it and when not.... – Preetkaran Singh Commented Nov 19, 2019 at 18:52
  • @PreetkaranSingh I would but I don't have enough information about the suppress argument – Nouman Commented Nov 19, 2019 at 20:15
| Show 5 more comments 54

As OP mention about raw_input - that means he want cli solution. Linux: curses is what you want (windows PDCurses). Curses, is an graphical API for cli software, you can achieve more than just detect key events.

This code will detect keys until new line is pressed.

import curses import os def main(win): win.nodelay(True) key="" win.clear() win.addstr("Detected key:") while 1: try: key = win.getkey() win.clear() win.addstr("Detected key:") win.addstr(str(key)) if key == os.linesep: break except Exception as e: # No input pass curses.wrapper(main) Share Improve this answer Follow edited Feb 11, 2019 at 3:11 rayryeng's user avatar rayryeng 104k22 gold badges195 silver badges201 bronze badges answered Sep 3, 2015 at 22:28 Abc Xyz's user avatar Abc XyzAbc Xyz 1,41414 silver badges15 bronze badges 6
  • 2 This is really nice. Had to search forever before coming across it. Seems much cleaner than hacking around with termios and so on ... – Hugh Perkins Commented May 4, 2016 at 10:40
  • 1 If you do win.nodelay(False) instead of True, it won't generate a million exceptions per second. – Johannes Hoff Commented Feb 27, 2020 at 18:09
  • Ugly as anything but still more beautiful then anything else I've seen. The weird thing is I remember distinctly back in my python2.7 days opening file descriptor 0 (stdin) for read with non-blocking and having it behave itself as a keypress collector, but for the life of me I can't figure out how I did it. I do remember it all started with me detaching stdin, but then realize I could simply open it as a separate stream and not have to worry about crashes or returning it's state to it's original behavior. Still... it was so simple and elegant and now, how??? can't find it. – Ismael Harun Commented Jul 2, 2021 at 12:06
  • Nice. It doesn't tell me when some keys are pressed, though (like Ctrl and Win). – Brōtsyorfuzthrāx Commented Aug 20, 2022 at 10:55
  • This draws a CLI "window" over whatever was in the terminal before - can it be used without this "window" being shown? – Fosfor Commented Sep 7, 2022 at 17:03
| Show 1 more comment 31

For Windows you could use msvcrt like this:

import msvcrt while True: if msvcrt.kbhit(): key = msvcrt.getch() print(key) # just to show the result Share Improve this answer Follow edited Feb 15, 2023 at 14:18 Xiddoc's user avatar Xiddoc 3,6193 gold badges15 silver badges40 bronze badges answered Aug 2, 2016 at 20:16 56-'s user avatar 56-56- 6066 silver badges11 bronze badges 4
  • 10 msvcrt is a Windows-only module. – Dunatotatos Commented Nov 21, 2016 at 14:44
  • 1 I actually use pynput now, that might be a better answer – 56- Commented Feb 5, 2018 at 19:50
  • Note that pynput to work on OS X (don't know about Linux) has to run as root in order to work. That may be a non-starter for some folks. – Gabe Weiss Commented Feb 27, 2018 at 0:59
  • I could have sworn the question was for 'cross-platform' or 'linux'... – Aaron Mann Commented Jan 30, 2020 at 4:58
Add a comment | 22

neoDev's comment at the question itself might be easy to miss, but it links to a solution not mentioned in any answer here.

There is no need to import keyboard with this solution.

Solution copied from this other question, all credits to @neoDev.

This worked for me on macOS Sierra and Python 2.7.10 and 3.6.3

import sys,tty,os,termios def getkey(): old_settings = termios.tcgetattr(sys.stdin) tty.setcbreak(sys.stdin.fileno()) try: while True: b = os.read(sys.stdin.fileno(), 3).decode() if len(b) == 3: k = ord(b[2]) else: k = ord(b) key_mapping = { 127: 'backspace', 10: 'return', 32: 'space', 9: 'tab', 27: 'esc', 65: 'up', 66: 'down', 67: 'right', 68: 'left' } return key_mapping.get(k, chr(k)) finally: termios.tcsetattr(sys.stdin, termios.TCSADRAIN, old_settings) try: while True: k = getkey() if k == 'esc': quit() else: print(k) except (KeyboardInterrupt, SystemExit): os.system('stty sane') print('stopping.')
Share Improve this answer Follow answered Jun 11, 2021 at 15:05 J0ANMM's user avatar J0ANMMJ0ANMM 8,48510 gold badges65 silver badges95 bronze badges 3
  • 1 Loving this for macOS. Thanks. – Cam U Commented Feb 10, 2022 at 20:17
  • 2 Wow, this is perfect. keyboard need root access, pynput need X Server. This right here needs neither and works for non-root users in CLI via ssh. Tested on Debian 11 with Python 3+ – AJ Smith 'Smugger' Commented May 6, 2022 at 13:50
  • simple, excellent and neat – Arun Panneerselvam Commented Mar 30 at 0:02
Add a comment | 17

Use this code for find the which key pressed

from pynput import keyboard def on_press(key): try: print('alphanumeric key {0} pressed'.format( key.char)) except AttributeError: print('special key {0} pressed'.format( key)) def on_release(key): print('{0} released'.format( key)) if key == keyboard.Key.esc: # Stop listener return False # Collect events until released with keyboard.Listener( on_press=on_press, on_release=on_release) as listener: listener.join() Share Improve this answer Follow answered Jan 17, 2019 at 15:35 Manivannan Murugavel's user avatar Manivannan MurugavelManivannan Murugavel 1,56817 silver badges15 bronze badges 5
  • Here's the thing though, i'm using macOS and installed both pynput and keyboard separately, and the program runs without any errors but can only detect (on the python shell) special keys. Alphanumeric keys are not detected and on the contrary, are considered as if i were writing code on the shell. Do you know what might be the issue? – Dario Deniz Ergün Commented Apr 11, 2019 at 7:04
  • The same code worked for me in the shell. Please check it. The keyboard package does not need this code. – Manivannan Murugavel Commented Apr 11, 2019 at 7:11
  • 3 This is the way to go in linux, as the keyboard lib needs root. – David Commented May 14, 2019 at 13:33
  • 4 This solution will detect all keystroke; also those happening in a different terminal window. Unfortunately, this severely limits its possible use cases. – Serge Stroobandt Commented Oct 6, 2019 at 1:10
  • It just times out for me – Trevor Blythe Commented Oct 5, 2021 at 23:59
Add a comment | 9

Use PyGame to have a window and then you can get the key events.

For the letter p:

import pygame, sys import pygame.locals pygame.init() BLACK = (0,0,0) WIDTH = 1280 HEIGHT = 1024 windowSurface = pygame.display.set_mode((WIDTH, HEIGHT), 0, 32) windowSurface.fill(BLACK) while True: for event in pygame.event.get(): if event.key == pygame.K_p: # replace the 'p' to whatever key you wanted to be pressed pass #Do what you want to here if event.type == pygame.locals.QUIT: pygame.quit() sys.exit() Share Improve this answer Follow edited Aug 31, 2019 at 10:42 Community's user avatar CommunityBot 11 silver badge answered Jun 6, 2014 at 3:53 A.J. Uppal's user avatar A.J. UppalA.J. Uppal 19.2k7 gold badges46 silver badges80 bronze badges 1
  • 6 I couldn't get the above code to run. I first had to check that the event type was one of KEYUP or KEYDOWN: if event.type in (pygame.KEYDOWN, pygame.KEYUP): print("Key: ", event.key) if(event.key == pygame.K_q): pygame.quit() – Jonathan Allin Commented Jul 27, 2021 at 20:02
Add a comment | 6

Non-root version that works even through ssh: sshkeyboard. Install with pip install sshkeyboard,

then write script such as:

from sshkeyboard import listen_keyboard def press(key): print(f"'{key}' pressed") def release(key): print(f"'{key}' released") listen_keyboard( on_press=press, on_release=release, )

And it will print:

'a' pressed 'a' released

When A key is pressed. ESC key ends the listening by default.

It requires less coding than for example curses, tkinter and getch. And it does not require root access like keyboard module.

Share Improve this answer Follow answered Oct 28, 2021 at 8:04 ilon's user avatar ilonilon 1791 silver badge6 bronze badges 1
  • Important: on Windows, this only works (as far as I can tell) if you run the program directly from the Window's command line. It fails if you attempt to run it through an IDE. – Calvin Godfrey Commented Mar 24, 2023 at 1:31
Add a comment | 5

I made this kind of game based on this post (using msvcr library and Python 3.7).

The following is the main function of the game, that is detecting the keys pressed:

import msvcrt def _secret_key(self): # Get the key pressed by the user and check if he/she wins. bk = chr(10) + "-"*25 + chr(10) while True: print(bk + "Press any key(s)" + bk) #asks the user to type any key(s) kp = str(msvcrt.getch()).replace("b'", "").replace("'", "") # Store key's value. if r'\xe0' in kp: kp += str(msvcrt.getch()).replace("b'", "").replace("'", "") # Refactor the variable in case of multi press. if kp == r'\xe0\x8a': # If user pressed the secret key, the game ends. # \x8a is CTRL+F12, that's the secret key. print(bk + "CONGRATULATIONS YOU PRESSED THE SECRET KEYS!\a" + bk) print("Press any key to exit the game") msvcrt.getch() break else: print(" You pressed:'", kp + "', that's not the secret key(s)\n") if self.select_continue() == "n": if self.secondary_options(): self._main_menu() break

If you want the full source code of the program you can see it or download it from GitHub

The secret keypress is:

Ctrl+F12

Share Improve this answer Follow edited Jan 11, 2022 at 10:46 Tomerikoo's user avatar Tomerikoo 19.4k16 gold badges53 silver badges67 bronze badges answered Jan 6, 2019 at 20:41 Ferd's user avatar FerdFerd 1,32115 silver badges16 bronze badges 1
  • str(msvcrt.getch()).replace("b'", "").replace("'", "") is strange, use msvcrt.getch().decode("ascii") to convert binary to text. Or better, ditch the text altogether and handle the data (e.g. b'\xe0\x8a'). – c z Commented Jan 25 at 13:19
Add a comment | 4

You don't mention if this is a GUI program or not, but most GUI packages include a way to capture and handle keyboard input. For example, with tkinter (in Py3), you can bind to a certain event and then handle it in a function. For example:

import tkinter as tk def key_handler(event=None): if event and event.keysym in ('s', 'p'): 'do something' r = tk.Tk() t = tk.Text() t.pack() r.bind('<Key>', key_handler) r.mainloop()

With the above, when you type into the Text widget, the key_handler routine gets called for each (or almost each) key you press.

Share Improve this answer Follow answered Jan 9, 2021 at 6:17 GaryMBloom's user avatar GaryMBloomGaryMBloom 5,6422 gold badges27 silver badges33 bronze badges Add a comment | 4

Here is a cross-platform solution, both blocking and non-blocking, not requiring any external libraries:

import contextlib as _contextlib try: import msvcrt as _msvcrt # Length 0 sequences, length 1 sequences... _ESCAPE_SEQUENCES = [frozenset(("\x00", "\xe0"))] _next_input = _msvcrt.getwch _set_terminal_raw = _contextlib.nullcontext _input_ready = _msvcrt.kbhit except ImportError: # Unix import sys as _sys, tty as _tty, termios as _termios, \ select as _select, functools as _functools # Length 0 sequences, length 1 sequences... _ESCAPE_SEQUENCES = [ frozenset(("\x1b",)), frozenset(("\x1b\x5b", "\x1b\x4f"))] @_contextlib.contextmanager def _set_terminal_raw(): fd = _sys.stdin.fileno() old_settings = _termios.tcgetattr(fd) try: _tty.setraw(_sys.stdin.fileno()) yield finally: _termios.tcsetattr(fd, _termios.TCSADRAIN, old_settings) _next_input = _functools.partial(_sys.stdin.read, 1) def _input_ready(): return _select.select([_sys.stdin], [], [], 0) == ([_sys.stdin], [], []) _MAX_ESCAPE_SEQUENCE_LENGTH = len(_ESCAPE_SEQUENCES) def _get_keystroke(): key = _next_input() while (len(key) <= _MAX_ESCAPE_SEQUENCE_LENGTH and key in _ESCAPE_SEQUENCES[len(key)-1]): key += _next_input() return key def _flush(): while _input_ready(): _next_input() def key_pressed(key: str = None, *, flush: bool = True) -> bool: """Return True if the specified key has been pressed Args: key: The key to check for. If None, any key will do. flush: If True (default), flush the input buffer after the key was found. Return: boolean stating whether a key was pressed. """ with _set_terminal_raw(): if key is None: if not _input_ready(): return False if flush: _flush() return True while _input_ready(): keystroke = _get_keystroke() if keystroke == key: if flush: _flush() return True return False def print_key() -> None: """Print the key that was pressed Useful for debugging and figuring out keys. """ with _set_terminal_raw(): _flush() print("\\x" + "\\x".join(map("{:02x}".format, map(ord, _get_keystroke())))) def wait_key(key=None, *, pre_flush=False, post_flush=True) -> str: """Wait for a specific key to be pressed. Args: key: The key to check for. If None, any key will do. pre_flush: If True, flush the input buffer before waiting for input. Useful in case you wish to ignore previously pressed keys. post_flush: If True (default), flush the input buffer after the key was found. Useful for ignoring multiple key-presses. Returns: The key that was pressed. """ with _set_terminal_raw(): if pre_flush: _flush() if key is None: key = _get_keystroke() if post_flush: _flush() return key while _get_keystroke() != key: pass if post_flush: _flush() return key

You can use key_pressed() inside a while loop:

while True: time.sleep(5) if key_pressed(): break

You can also check for a specific key:

while True: time.sleep(5) if key_pressed("\x00\x48"): # Up arrow key on Windows. break

Find out special keys using print_key():

>>> print_key() # Press up key \x00\x48

Or wait until a certain key is pressed:

>>> wait_key("a") # Stop and ignore all inputs until "a" is pressed. Share Improve this answer Follow edited Jan 11, 2022 at 10:03 answered Jan 11, 2022 at 9:49 Bharel's user avatar BharelBharel 26.7k7 gold badges49 silver badges94 bronze badges Add a comment | 3

Using the keyboard package, especially on linux is not an apt solution because that package requires root privileges to run. We can easily implement this with the getkey package. This is analogous to the C language function getchar.

Install it:

pip install getkey

And use it:

from getkey import getkey while True: #Breaks when key is pressed key = getkey() print(key) #Optionally prints out the key. break

We can add this in a function to return the pressed key.

def Ginput(str): """ Now, this function is like the native input() function. It can accept a prompt string, print it out, and when one key is pressed, it will return the key to the caller. """ print(str, end='') while True: key = getkey() print(key) return key

Use like this:

inp = Ginput("\n Press any key to continue: ") print("You pressed " + inp) Share Improve this answer Follow edited Jul 17, 2021 at 22:00 answered Jul 17, 2021 at 21:54 Joel G Mathew's user avatar Joel G MathewJoel G Mathew 8,02115 gold badges61 silver badges93 bronze badges 1
  • 3 Per several issues shown on that project, getkey does not appear to be actively maintained any longer, and pip install on Windows is broken. – Marc L. Commented Jul 22, 2021 at 19:48
Add a comment | 3

The curses module does that job.

You can test it running this example from the terminal:

import curses screen = curses.initscr() curses.noecho() curses.cbreak() screen.keypad(True) try: while True: char = screen.getch() if char == ord('q'): break elif char == curses.KEY_UP: print('up') elif char == curses.KEY_DOWN: print('down') elif char == curses.KEY_RIGHT: print('right') elif char == curses.KEY_LEFT: print('left') elif char == ord('s'): print('stop') finally: curses.nocbreak(); screen.keypad(0); curses.echo() curses.endwin() Share Improve this answer Follow answered Sep 12, 2021 at 19:11 Rodolfo Leibner's user avatar Rodolfo LeibnerRodolfo Leibner 1911 silver badge7 bronze badges 2
  • does this handle left and up arrow pressed at same time? – AwokeKnowing Commented Sep 20, 2021 at 19:42
  • 1 This does not work on Windows – jpp1 Commented Nov 11, 2022 at 15:42
Add a comment | 2 import cv2 key = cv2.waitKey(1)

This is from the openCV package. The delay arg is the number of milliseconds it will wait for a keypress. In this case, 1ms. Per the docs, pollKey() can be used without waiting.

Share Improve this answer Follow edited Jul 22, 2021 at 20:04 Marc L.'s user avatar Marc L. 3,3721 gold badge33 silver badges43 bronze badges answered Feb 14, 2019 at 20:32 user1261273user1261273 2
  • 6 You need to write more about how it is supposed to work. Also, it will be helpful if you explain why does "key" and "1" mean in this example. I cannot make this example to work. – Valentyn Commented Feb 13, 2021 at 21:19
  • 6 A 35MB computer-vision module + a dependency on numpy seems like a lot of baggage for this wee bit of functionality. – Marc L. Commented Jul 22, 2021 at 20:06
Add a comment | 1

You can use pygame's get_pressed():

import pygame while True: keys = pygame.key.get_pressed() if (keys[pygame.K_LEFT]): pos_x -= 5 elif (keys[pygame.K_RIGHT]): pos_x += 5 elif (keys[pygame.K_UP]): pos_y -= 5 elif (keys[pygame.K_DOWN]): pos_y += 5 Share Improve this answer Follow edited Jan 11, 2022 at 10:51 Tomerikoo's user avatar Tomerikoo 19.4k16 gold badges53 silver badges67 bronze badges answered Nov 4, 2021 at 5:21 pondol's user avatar pondolpondol 9397 silver badges9 bronze badges Add a comment | 0

I was finding how to detect different key presses subsequently until e.g. Ctrl + C break the program from listening and responding to different key presses accordingly.

Using following code,

while True: if keyboard.is_pressed("down"): print("Reach the bottom!") if keyboard.is_pressed("up"): print("Reach the top!") if keyboard.is_pressed("ctrl+c"): break

It will cause the program to keep spamming the response text, if I pressed arrow down or arrow up. I believed because it's in a while-loop, and eventhough you only press once, but it will get triggered multiple times (as written in doc, I am awared of this after I read.)

At that moment, I still haven't went to read the doc, I try adding in time.sleep()

while True: if keyboard.is_pressed("down"): print("Reach the bottom!") time.sleep(0.5) if keyboard.is_pressed("up"): print("Reach the top!") time.sleep(0.5) if keyboard.is_pressed("ctrl+c"): break

This solves the spamming issue.

But this is not a very good way as of subsequent very fast taps on the arrow key, will only trigger once instead of as many times as I pressed, because the program will sleep for 0.5 second right, meant the "keyboard event" happened at that 0.5 second will not be counted.

So, I proceed to read the doc and get the idea to do this at this part.

while True: # Wait for the next event. event = keyboard.read_event() if event.event_type == keyboard.KEY_DOWN and event.name == 'down': # do whatever function you wanna here if event.event_type == keyboard.KEY_DOWN and event.name == 'up': # do whatever function you wanna here if keyboard.is_pressed("ctrl+c"): break

Now, it's working fine and great! TBH, I am not deep dive into the doc, used to, but I have really forgetten the content, if you know or find any better way to do the similar function, please enlighten me!

Thank you, wish you have a great day ahead!

Share Improve this answer Follow answered Dec 13, 2022 at 4:21 jas99's user avatar jas99jas99 3012 silver badges4 bronze badges Add a comment | Highly active question. Earn 10 reputation (not counting the association bonus) in order to answer this question. The reputation requirement helps protect this question from spam and non-answer activity.

Not the answer you're looking for? Browse other questions tagged or ask your own question.

  • The Overflow Blog
  • Your docs are your infrastructure
  • Featured on Meta
  • More network sites to see advertising test [updated with phase 2]
  • We’re (finally!) going to the cloud!
  • Call for testers for an early access release of a Stack Overflow extension...
Visit chat

Linked

1 How to register specific key pressed with other keys being pressed? -1 How to break out of a loop when a keyboard key is pressed? 0 Python detecting keypress 1 Detect keyboard presses in Python 3 on Raspberry Pi 0 Using Pyside2/Pyqt5 to detect keyboard input when QT application is minimised and out of focus 1 How to measure time between next pressed keys? 0 How do I add a time limit which doesn't require the user to enter the input? 1 How can I constantly check if a specific key is pressed 0 Easiest way to check if particular key is pressed 0 How to detect if a key is being held down See more linked questions 19 Detect in python which keys are pressed 2 How can I tell if a certain key was pressed in Python? 2 Keystroke detection using Python 3 Get key pressed event 5 Python - Detect keypress 0 Detect keypress with python (not msvct) 1 Im Trying to Detect a Certain Key being Pressed (Python) 1 Keypress detection 0 Detect keyboard press 3 How to detect any key press with the keyboard module?

Hot Network Questions

  • If "de-" means "down" or "off" and "sub-" means "under", what is the latin prefix- meaning "up"?
  • Meaning of "rankear" - ¿Qué significa "rankear"?
  • Adjective meaning "with gaps" or "with holes"
  • How can I avoid overusing her/she or the character name when describing character action
  • In GR, what is Gravity? A force or curvature of spacetime?
  • Can one publication contribute to two separate grants?
  • If scent means a pleasant smell, why do we say "lovely scent" or "sweet scent"?
  • Is Holy Terra Earth?
  • How can I fix this leaking connection in the heating system?
  • What is this insect found in an apartment in Brazil?
  • Why don't routers answer ARP requests for IP addresses they can handle even if they aren't assigned that IP address themselves?
  • Preventing resulting shapefile being added to ArcGIS Pro map by ArcPy
  • Did renaissance actors learn their parts by heart?
  • Lebesgue equivalence class with no continuous representative
  • How can I reference sky photos to a star map?
  • Should sudo ask for root password?
  • Do I need Letter of invitation to Iceland?
  • What could be causing drywall tape to buckle?
  • How can I politely decline a request to join my project by a free rider professor
  • What would T-Rex cavalry be used for?
  • How Do Copulas Provide Insight Into Dependence Between Random Variables?
  • Being honest with rejection vs. preparing applicant for future possibility
  • Are there any examples of exponential algorithms that use a polynomial-time algorithm for a special case as a subroutine (exponentially many times)?
  • Is it problematic to use percentages to describe a sample with less than 100 people?
more hot questions Question feed Subscribe to RSS Question feed

To subscribe to this RSS feed, copy and paste this URL into your RSS reader.

lang-py

Từ khóa » Thư Viện Pynput