如何在Python中检查用户是否按下箭头键?我想要这样的东西:
if right_key.pressed():
do_some_shit()
elif left_key.pressed():
do_other_stuff()
在终端(或 anacoonda 提示符)中运行此命令来安装 pynputlibrary
:
pip install pynput
并且,在你的编辑器中
from pynput import keyboard
from pynput.keyboard import Key
def on_key_release(key):
if key == Key.right:
print("Right key clicked")
elif key == Key.left:
print("Left key clicked")
elif key == Key.up:
print("Up key clicked")
elif key == Key.down:
print("Down key clicked")
elif key == Key.esc:
exit()
with keyboard.Listener(on_release=on_key_release) as listener:
listener.join()
更多信息
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)