在python中检测按键,每次迭代可能需要几秒钟以上?
编辑:以下使用键盘的答案。按(callback,suppress=False)在ubuntu中运行良好,没有任何问题。 但在Redhat/Amazon linux中,它无法工作 我已经使用了本文中的代码片段 但是上面的代码要求每次迭代都在纳秒内执行。在以下情况下失败:在python中检测按键,每次迭代可能需要几秒钟以上?,python,python-3.x,redhat,keypress,amazon-linux,Python,Python 3.x,Redhat,Keypress,Amazon Linux,编辑:以下使用键盘的答案。按(callback,suppress=False)在ubuntu中运行良好,没有任何问题。 但在Redhat/Amazon linux中,它无法工作 我已经使用了本文中的代码片段 但是上面的代码要求每次迭代都在纳秒内执行。在以下情况下失败: import keyboard # using module keyboard import time while True: # making a loop try: # used try so that if u
import keyboard # using module keyboard
import time
while True: # making a loop
try: # used try so that if user pressed other than the given key error will not be shown
print("sleeping")
time.sleep(5)
print("slept")
if keyboard.is_pressed('q'): # if key 'q' is pressed
print('You Pressed A Key!')
break # finishing the loop
except:
print("#######")
break # if user pressed a key other than the given key the loop will break
你可以用一根线
import threading
class KeyboardEvent(threading.Thread):
def run(self):
if keyboard.is_pressed('q'): # if key 'q' is pressed
print('You Pressed A Key!')
break # finishing the loop
keyread = KeyboardEvent()
keyread.start()
这将与主线程中的任何内容并行运行,并主要用于侦听按键。您可以使用
键盘
模块中的事件处理程序来实现所需的结果
一个这样的处理程序是键盘。按下(callback,suppress=False):
它为每个按键向下
事件调用回调。
你可以在
以下是您可以尝试的代码:
import keyboard # using module keyboard
import time
stop = False
def onkeypress(event):
global stop
if event.name == 'q':
stop = True
# ---------> hook event handler
keyboard.on_press(onkeypress)
# --------->
while True: # making a loop
try: # used try so that if user pressed other than the given key error will not be shown
print("sleeping")
time.sleep(5)
print("slept")
if stop: # if key 'q' is pressed
print('You Pressed A Key!')
break # finishing the loop
except:
print("#######")
break # if user pressed a key other than the given key the loop will break
编辑:没关系,另一个答案使用了几乎相同的方法 这就是我能想到的,使用相同的“键盘”模块,见下面的代码注释
import keyboard, time
from queue import Queue
# keyboard keypress callback
def on_keypress(e):
keys_queue.put(e.name)
# tell keyboard module to tell us about keypresses via callback
# this callback happens on a separate thread
keys_queue = Queue()
keyboard.on_press(on_keypress)
try:
# run the main loop until some key is in the queue
while keys_queue.empty():
print("sleeping")
time.sleep(5)
print("slept")
# check if its the right key
if keys_queue.get()!='q':
raise Exception("terminated by bad key")
# still here? good, this means we've been stoped by the right key
print("terminated by correct key")
except:
# well, you can
print("#######")
finally:
# stop receiving the callback at this point
keyboard.unhook_all()
您的程序在睡眠呼叫期间将检测不到任何内容。“你想干什么?”詹姆斯,上面的节目只是一个例子。问题是,如果while循环中的每次迭代都需要几秒钟以上,则不会检测到按键。请检查此示例,您是否尝试使用其他库?就像evemu和python evdev一样,键盘也不一致(至少对我来说)@narenbabr您必须以root用户身份运行此程序,如键盘文档中所述。在运行程序之前,执行sudo su可以解决problem@NarenBabuR是的,那就行了如果我没弄错的话,一旦线程启动,它就会检查按键,然后线程就会退出。你的意思是不是说不是键盘时按了
。而是按了?
import keyboard, time
from queue import Queue
# keyboard keypress callback
def on_keypress(e):
keys_queue.put(e.name)
# tell keyboard module to tell us about keypresses via callback
# this callback happens on a separate thread
keys_queue = Queue()
keyboard.on_press(on_keypress)
try:
# run the main loop until some key is in the queue
while keys_queue.empty():
print("sleeping")
time.sleep(5)
print("slept")
# check if its the right key
if keys_queue.get()!='q':
raise Exception("terminated by bad key")
# still here? good, this means we've been stoped by the right key
print("terminated by correct key")
except:
# well, you can
print("#######")
finally:
# stop receiving the callback at this point
keyboard.unhook_all()