python - 如何查看使用 pynput python 3.7 按下的字符?
问题描述
您如何查看在 pynput 中按下了哪个按钮。
from pynput.keyboard import Key, Listener
def a(key):
print('{0} pressed'.format(
key))
if key == 'a':
print('ape')
with Listener(on_press = a) as listener:
listener.join()
看不到工作。
解决方案
from pynput.keyboard import Listener
def a(key):
print(f"{key}, was pressed")
if key.char == "a":
print("foo")
with Listener(on_press=a) as listener:
listener.join()
输出:
q'q', was pressed
w'w', was pressed
a'a', was pressed
foo
s's', was pressed
但是,例如,如果您按下大写锁定,这将导致问题,因此我将放置在 try except 块中:
try:
if key.char == 'a':
print("foo")
except AttributeError:
pass
推荐阅读
- javascript - 使用 JavaScript 的 GIF 图像位置动画
- sdn - mininet pingall 有时会因遥控器而失败
- react-native - React Native SectionList (title, data) - 在数据字段中搜索
- javascript - 使用大字符串绑定输入时 Vue 崩溃
- amazon-web-services - AWS 中有调度服务吗?
- c# - 正确读取ms access数据库中的列数据类型
- sql - Oracle LEAD - 返回下一个匹配的列值
- symfony - Symfony 2.8 > 3.4.22 - 您的需求无法解析为一组可安装的软件包
- sql-server - 使用 MIN() 而不是 top 1 进行 SQL 查询
- google-maps - 当我包含谷歌地图时,我的构建失败