My code is below:
import msvcrt
while True:
if msvcrt.getch() == 'q':
print "Q was pressed"
elif msvcrt.getch() == 'x':
sys.exit()
else:
print "Key Pressed:" + str(msvcrt.getch()
This code is based on this question; I was using it to acquaint myself with getch.
I've noticed that it takes 3 pressing the key 3 times to output the text once. Why is this? I'm trying to use it as an event loop, and that's too much of a lag...
Even if I type 3 different keys, it only outputs the 3rd keypress.
How can I force it to go faster? Is there a better way to achieve what I'm trying to achieve?
Thanks!
evamvid
you call the function 3 times in your loop. try calling it only once like this:
import msvcrt
while True:
pressedKey = msvcrt.getch()
if pressedKey == 'q':
print "Q was pressed"
elif pressedKey == 'x':
sys.exit()
else:
print "Key Pressed:" + str(pressedKey)
You can optimize things a little bit by also using themsvcrt.kbhit function which will allow you callmsvcrt.getch()only as much as is necessary:
while True:
if msvcrt.kbhit():
ch = msvcrt.getch()
if ch in '\x00\xe0': # arrow or function key prefix?
ch = msvcrt.getch() # second call returns the scan code
if ch == 'q':
print "Q was pressed"
elif ch == 'x':
sys.exit()
else:
print "Key Pressed:", ch
Note that theKey Pressedvalue printed won't make sense for things like function keys. That's because it those cases it's really the Windows scan code for the key, not a regular key code for the character.
Related
I want to check what key was pressed while waiting for the input, just like with getch().
I tried:
from msvcrt import getch
ch = ord(getch())
and
import readchar
ch = readchar.readkey()
ch = readchar.readchar()
and then I'm printing ch to see what's underneath.
print(ch)
Works fine for letters, but for Arrow keys it always returns the same value no matter the key:
for ord(getch()) it's 224
for readchar.readkey() it's None
and for readchar.readchar() it's b'\xe0'
I'm using Microsoft Visual Studio Python 3.8 if that matters.
Any way to fix it, do it while making it work like getch()? (Waiting for input)
when you run getch() the first time and you click on any arrow it will return b'\xe0' but when
you run getch() the second time it will return b'H' or b'P' or b'K' or b'M'
def read_keys():
while True:
key = getch()
if (key == b'\x00' or key ==b'\xe0'):
key = getch()
print('Arrow key', ord(key))
else:
print("Else", ord(key))
read_keys()
I am trying to build a locker with Raspberry Pi. I have a code that when entering a correct CODE = '1234' with USB keyboard it opens a servo.
Basically it works, but need to loop it somehow so, if I put wrong PIN, I am asked again to put correct.
for event in dev.read_loop():
if event.type == ecodes.EV_KEY:
e = categorize(event)
if e.keystate == e.key_down:
klawisz = e.keycode[4:]
if klawisz != "ESC":
kod = (kod + klawisz)
print(kod)
else:
break
if kod == '1234':
for event in dev.read_loop():
if event.type == ecodes.EV_KEY:
d = categorize(event)
if d.keystate == d.key_down:
klawisz = d.keycode[4:]
if klawisz == "ESC":
print('ITS OPEN')
break
else:
break
else:
print('Wrong PIN')
I tried with while loop at the beginning, but it doesn't work :(
while kod == '1234'
Hope you could you guide me to correct solution, as I am still learning Python. Thanks.
You can use while loop to repeatedly perform some operations (reading users password in your example) if some condition stays true:
def read_password():
kod = ""
for event in dev.read_loop():
if event.type == ecodes.EV_KEY:
e = categorize(event)
if e.keystate == e.key_down:
klawisz = e.keycode[4:]
if klawisz != "ESC":
kod = (kod + klawisz)
print(kod)
else:
break
return kod
while read_password() != '1234':
print('Wrong PIN, try again')
In this case you're reading password as long as it doesn't match '1234'.
Use an infinite while loop and break out of it only if the code matches.
while True:
code = input('Enter code: ')
if code == '1234':
print('Code accepted.')
break
print('Wrong code, try again.')
You can easily add an additional security feature to reduce the amount of attempts per time.
import time
attempts = 0
while True:
code = input('Enter code: ')
if code == '1234':
print('Code accepted.')
break
print('Wrong code, try again.')
attempts = attempts + 1
if attempts > 9:
print('Too many failed attempts. Please wait.')
time.sleep(600)
attempts = 0
You can run all examples above on your regular computer to test them. They use Python's built-in input function. You use the RETURN key to finalize your input instead of the ESC key in your code. You said that you read the user input with a USB keyboard, so input might even work for you with your Raspberry Pi, too.
I'm using raw_input in Python to interact with user in shell.
c = raw_input('Press s or n to continue:')
if c.upper() == 'S':
print 'YES'
It works as intended, but the user has to press enter in the shell after pressing 's'. Is there a way to accomplish what I need from an user input without needing to press enter in the shell? I'm using *nixes machines.
Under Windows, you need the msvcrt module, specifically, it seems from the way you describe your problem, the function msvcrt.getch:
Read a keypress and return the
resulting character. Nothing is echoed
to the console. This call will block
if a keypress is not already
available, but will not wait for Enter
to be pressed.
(etc -- see the docs I just pointed to). For Unix, see e.g. this recipe for a simple way to build a similar getch function (see also several alternatives &c in the comment thread of that recipe).
Actually in the meantime (almost 10 years from the start of this thread) a cross-platform module named pynput appeared.
Below a first cut - i.e. that works with lowercase 's' only.
I have tested it on Windows but I am almost 100% positive that it should work on Linux.
from pynput import keyboard
print('Press s or n to continue:')
with keyboard.Events() as events:
# Block for as much as possible
event = events.get(1e6)
if event.key == keyboard.KeyCode.from_char('s'):
print("YES")
Python does not provide a multiplatform solution out of the box.
If you are on Windows you could try msvcrt with:
import msvcrt
print 'Press s or n to continue:\n'
input_char = msvcrt.getch()
if input_char.upper() == 'S':
print 'YES'
curses can do that as well :
import curses, time
def input_char(message):
try:
win = curses.initscr()
win.addstr(0, 0, message)
while True:
ch = win.getch()
if ch in range(32, 127):
break
time.sleep(0.05)
finally:
curses.endwin()
return chr(ch)
c = input_char('Do you want to continue? y/[n]')
if c.lower() in ['y', 'yes']:
print('yes')
else:
print('no (got {})'.format(c))
To get a single character, I have used getch, but I don't know if it works on Windows.
Instead of the msvcrt module you could also use WConio:
>>> import WConio
>>> ans = WConio.getkey()
>>> ans
'y'
On a side note, msvcrt.kbhit() returns a boolean value determining if any key on the keyboard is currently being pressed.
So if you're making a game or something and want keypresses to do things but not halt the game entirely, you can use kbhit() inside an if statement to make sure that the key is only retrieved if the user actually wants to do something.
An example in Python 3:
# this would be in some kind of check_input function
if msvcrt.kbhit():
key = msvcrt.getch().decode("utf-8").lower() # getch() returns bytes data that we need to decode in order to read properly. i also forced lowercase which is optional but recommended
if key == "w": # here 'w' is used as an example
# do stuff
elif key == "a":
# do other stuff
elif key == "j":
# you get the point
I know this is old, but the solution wasn't good enough for me.
I need the solution to support cross-platform and without installing any external Python packages.
My solution for this, in case anyone else comes across this post
Reference: https://github.com/unfor19/mg-tools/blob/master/mgtools/get_key_pressed.py
from tkinter import Tk, Frame
def __set_key(e, root):
"""
e - event with attribute 'char', the released key
"""
global key_pressed
if e.char:
key_pressed = e.char
root.destroy()
def get_key(msg="Press any key ...", time_to_sleep=3):
"""
msg - set to empty string if you don't want to print anything
time_to_sleep - default 3 seconds
"""
global key_pressed
if msg:
print(msg)
key_pressed = None
root = Tk()
root.overrideredirect(True)
frame = Frame(root, width=0, height=0)
frame.bind("<KeyRelease>", lambda f: __set_key(f, root))
frame.pack()
root.focus_set()
frame.focus_set()
frame.focus_force() # doesn't work in a while loop without it
root.after(time_to_sleep * 1000, func=root.destroy)
root.mainloop()
root = None # just in case
return key_pressed
def __main():
c = None
while not c:
c = get_key("Choose your weapon ... ", 2)
print(c)
if __name__ == "__main__":
__main()
If you can use an external library, blessed (cross platform) can do do this quite easily:
from blessed import Terminal
term = Terminal()
with term.cbreak(): # set keys to be read immediately
print("Press any key to continue")
inp = term.inkey() # wait and read one character
Note that while inside the with block, line editing capabilities of the terminal will be disabled.
Documentation for cbreak, inkey, and an example with inkey.
Standard library solution for Unix-like operating systems (including Linux):
def getch():
import sys, termios
fd = sys.stdin.fileno()
orig = termios.tcgetattr(fd)
new = termios.tcgetattr(fd)
new[3] = new[3] & ~termios.ICANON
new[6][termios.VMIN] = 1
new[6][termios.VTIME] = 0
try:
termios.tcsetattr(fd, termios.TCSAFLUSH, new)
return sys.stdin.read(1)
finally:
termios.tcsetattr(fd, termios.TCSAFLUSH, orig)
This works by putting the terminal into non-canonical input mode before reading from the terminal.
Alternative solution that does not echo the user's input (e.g. if the user presses z, z will not appear on screen):
def getch():
import sys, termios, tty
fd = sys.stdin.fileno()
orig = termios.tcgetattr(fd)
try:
tty.setcbreak(fd) # or tty.setraw(fd) if you prefer raw mode's behavior.
return sys.stdin.read(1)
finally:
termios.tcsetattr(fd, termios.TCSAFLUSH, orig)
Usage example:
print('Press s or n to continue: ', end='', flush=True)
c = getch()
print()
if c.upper() == 'S':
print('YES')
use readchar:
import readchar
key = readchar.readkey()
if key == "a":
print("text")
https://pypi.org/project/readchar/ to webpage
I have created a Python script that can take commands from a pipe (named pipe1). You can find the Script on Pastebin.
I tested the script with this other script:
import sys
fw = open("pipe1", "w" )
fw.write('fd\n')
fw.close()
and it worked.
Now I want to control the script with another Python script, that could write in the pipe if I press w, a, s, d or p and display me the keys, that i press.
In this example I just want to print the keys that I press. I would later add the fw.write commands to write in the pipe, which I tested before:
def key_inp(event):
print 'Key:', event.char
key_press = event.char
sleep_time = 0.030
while True:
try:
if key_press.lower() == 'w':
print "w"
elif key_press.lower() == 's':
print "s"
elif key_press.lower() == 'a':
print "a"
elif key_press.lower() == 'd':
print "d"
elif key_press.lower() == 'q':
print "q"
elif key_press.lower() == 'e':
print "e"
elif key_press.lower() == 'p':
print "stop"
except(KeyboardInterrupt):
print('Finished')
My problem is, that the script that i wrote (and improved with a stackoverflow member) closes immediately when i open it.
Can anyone explain me why, and how i can fix it so that the script stays open the whole time until i interrupt it with Ctrl+c?
EDIT: This answer relies on installing the readchar module. You can install it via pip: pip install readchar.
The code you are trying to use has no functionality: you only define a function, but never call upon it. On top of that, it contains indentation errors.
Something along the lines of what you are trying to achieve, but with a dot as finish key:
import readchar
while True:
key = readchar.readkey()
key = key.lower()
if key in ('wsadqe'):
print 'Key:', key
elif key == 'p':
print "stop"
sleep_time = 0.030
if key == '.':
print "finished"
break
I'm new to Python, and I just made a game and a menu in Python.
Question is, that using (raw_)input() requires me to press enter after every keypress, I'd like to make it so that pressing down-arrow will instantly select the next menu item, or move down in the game. At the moment, it requires me to like type "down" and then hit enter. I also did quite a lot of research, but I would prefer not to download huge modules (e.g. pygame) just to achieve a single keyDown() method. So are there any easier ways, which I just couldn't find?
Edit:
Just found out that msvcrt.getch() would do the trick. It's not keyDown(), but it works. However, I'm not sure how to use it either, it seems quite weird, any help here? This is what I got at the moment:
from msvcrt import getch
while True:
key = getch()
print(key)
However, it keeps giving me all these nonsense bytes, for example, down-arrow is this:
b'\xe0'
b'P'
And I have no idea how to use them, I've tried to compare with chr() and even use ord() but can't really do any comparisons. What I'm trying to do is basically this:
from msvcrt import getch
while True:
key = getch()
if key == escape:
break
elif key == downarrow:
movedown()
elif key == 'a':
...
And so on... Any help?
Figured it out by testing all the stuff by myself.
Couldn't find any topics about it tho, so I'll just leave the solution here. This might not be the only or even the best solution, but it works for my purposes (within getch's limits) and is better than nothing.
Note: proper keyDown() which would recognize all the keys and actual key presses, is still valued.
Solution: using ord()-function to first turn the getch() into an integer (I guess they're virtual key codes, but not too sure) works fine, and then comparing the result to the actual number representing the wanted key. Also, if I needed to, I could add an extra chr() around the number returned so that it would convert it to a character. However, I'm using mostly down arrow, esc, etc. so converting those to a character would be stupid. Here's the final code:
from msvcrt import getch
while True:
key = ord(getch())
if key == 27: #ESC
break
elif key == 13: #Enter
select()
elif key == 224: #Special keys (arrows, f keys, ins, del, etc.)
key = ord(getch())
if key == 80: #Down arrow
moveDown()
elif key == 72: #Up arrow
moveUp()
Also if someone else needs to, you can easily find out the keycodes from google, or by using python and just pressing the key:
from msvcrt import getch
while True:
print(ord(getch()))
See the MSDN getch docs. Specifically:
The _getch and_getwch functions read a single character from the console without echoing the character. None of these functions can be used to read CTRL+C. When reading a function key or an arrow key, each function must be called twice; the first call returns 0 or 0xE0, and the second call returns the actual key code.
The Python function returns a character. you can use ord() to get an integer value you can test, for example keycode = ord(msvcrt.getch()).
So if you read an 0x00 or 0xE0, read it a second time to get the key code for an arrow or function key. From experimentation, 0x00 precedes F1-F10 (0x3B-0x44) and 0xE0 precedes arrow keys and Ins/Del/Home/End/PageUp/PageDown.
I really did not want to post this as a comment because I would need to comment all answers and the original question.
All of the answers seem to rely on MSVCRT Microsoft Visual C Runtime.
If you would like to avoid that dependency :
In case you want cross platform support, using the library here:
https://pypi.org/project/getkey/#files
https://github.com/kcsaff/getkey
Can allow for a more elegant solution.
Code example:
from getkey import getkey, keys
key = getkey()
if key == keys.UP:
... # Handle the UP key
elif key == keys.DOWN:
... # Handle the DOWN key
elif key == 'a':
... # Handle the `a` key
elif key == 'Y':
... # Handle `shift-y`
else:
# Handle other text characters
buffer += key
print(buffer)
from msvcrt import getch
pos = [0, 0]
def fright():
global pos
pos[0] += 1
def fleft():
global pos
pos[0] -= 1
def fup():
global pos
pos[1] += 1
def fdown():
global pos
pos[1] -= 1
while True:
print'Distance from zero: ', pos
key = ord(getch())
if key == 27: #ESC
break
elif key == 13: #Enter
print('selected')
elif key == 32: #Space
print('jump')
elif key == 224: #Special keys (arrows, f keys, ins, del, etc.)
key = ord(getch())
if key == 80: #Down arrow
print('down')
fdown
elif key == 72: #Up arrow
print('up')
fup()
elif key == 75: #Left arrow
print('left')
fleft()
elif key == 77: #Right arrow
print('right')
fright()
I was also trying to achieve this. From above codes, what I understood was that you can call getch() function multiple times in order to get both bytes getting from the function. So the ord() function is not necessary if you are just looking to use with byte objects.
while True :
if m.kbhit() :
k = m.getch()
if b'\r' == k :
break
elif k == b'\x08'or k == b'\x1b':
# b'\x08' => BACKSPACE
# b'\x1b' => ESC
pass
elif k == b'\xe0' or k == b'\x00':
k = m.getch()
if k in [b'H',b'M',b'K',b'P',b'S',b'\x08']:
# b'H' => UP ARROW
# b'M' => RIGHT ARROW
# b'K' => LEFT ARROW
# b'P' => DOWN ARROW
# b'S' => DELETE
pass
else:
print(k.decode(),end='')
else:
print(k.decode(),end='')
This code will work print any key until enter key is pressed in CMD or IDE
(I was using VS CODE)
You can customize inside the if for specific keys if needed
It's really late now but I made a quick script which works for Windows, Mac and Linux, simply by using each command line:
import os, platform
def close():
if platform.system() == "Windows":
print("Press any key to exit . . .")
os.system("pause>nul")
exit()
elif platform.system() == "Linux":
os.system("read -n1 -r -p \"Press any key to exit . . .\" key")
exit()
elif platform.system() == "Darwin":
print("Press any key to exit . . .")
os.system("read -n 1 -s -p \"\"")
exit()
else:
exit()
It uses only inbuilt functions, and should work for all three (although I've only tested Windows and Linux...).
I suggest keyboard module by BoppreH. This example shows how to detect keypresses outside of python console in a non blocking way. When you press the f key it calls a function that prints some text . To end the execution of the script press the q key. For more info and special key codes click here
import keyboard
def my_function():
print("button pressed")
def my_quit():
global mybool
mybool = False
keyboard.add_hotkey('f', my_function)
keyboard.add_hotkey('q', my_quit)
mybool = True
while mybool:
keyboard.read_key()
If you want to pause the execution of the script intsead of add_hotkey() use keyboard.wait('esc')