The standard approach is to use the select module.
However, this doesn't work on Windows. For that, you can use the msvcrt module's keyboard polling.
Often, this is done with multiple threads -- one per device being "watched" plus the background processes that might need to be interrupted by the device.
Answer from S.Lott on Stack OverflowThe standard approach is to use the select module.
However, this doesn't work on Windows. For that, you can use the msvcrt module's keyboard polling.
Often, this is done with multiple threads -- one per device being "watched" plus the background processes that might need to be interrupted by the device.
A solution using the curses module. Printing a numeric value corresponding to each key pressed:
import curses
def main(stdscr):
# do not wait for input when calling getch
stdscr.nodelay(1)
while True:
# get keyboard input, returns -1 if none available
c = stdscr.getch()
if c != -1:
# print numeric value
stdscr.addstr(str(c) + ' ')
stdscr.refresh()
# return curser to start position
stdscr.move(0, 0)
if __name__ == '__main__':
curses.wrapper(main)
I want to write a simple program in the terminal that takes in non-blocking I/O input basically. The idea as as follows (it's very simple):
while True:
print("#"*length_bar)
if keypress: # runs in "background" or concurrent with main loop
length_bar += 1Now, the problem is that I have trouble finding a way to get non-blocking keyboard input from the user while print stuff in the terminal as well. I want this bar to continuously print while waiting actively for board input. Been banging my head against this devilishly simple problem for an hour or so now but can't seem to find a satisfactory answer on google or stackoverflow ...
How to detect any key pressed without blocking execution in Python - Stack Overflow
python - Detect Keypress without blocking - Stack Overflow
python 3.x - Detect (non-blocking) key press while accepting client connections - Stack Overflow
python - How to detect key presses? - Stack Overflow
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
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()
I'm trying to create a platformer in Python 3.4.3 on OS X. Unfortunately, using event.key doesn't return anything but 0 for the letter keys. It works for the arrow keys:
IF event.key == pygame.K_DOWN: returns a value if the down key is pressed.
But not for WASD:
IF event.key == pygame.K_s: does not return a value if the s key is pressed, no matter what modifiers are active.
And using the curses library gives me a terminal error when I initialize it.
Is there any way to detect keycodes without importing a library?