所以我想知道是否可以在不将按键输入Windows cmd终端的情况下收听按键。这将非常有用,因为当前的行为使我想起了我刚才输入的内容,这使我的程序混乱(这是由于向右箭头。)
我正在使用模块“ pyinput”,然后使用“ pyinstaller”将python程序转换为exe。
这是我修改后的代码版本:
def on_press(key): #Do nothing except complete the requirement for the listener
try:
pass
except AttributeError:
pass
def on_release(key):
global key_choice
if key == keyboard.Key.left:
key_choice="left"
channel4.play(click, loops = 0)
return False
elif key == keyboard.Key.right:
key_choice="right"
channel5.play(click, loops = 0)
return False
elif key == keyboard.Key.enter:
key_choice="enter"
return False
def listen():
with keyboard.Listener(
on_press=on_press,
on_release=on_release) as listener:
listener.join()
return