在python中检测按键?

在python中检测按键?,第1张

在python中检测按键

对于那些在窗户上努力寻找可行答案的人,我的是:pynput

from pynput.keyboard import Key, Listenerdef on_press(key):    print('{0} pressed'.format(        key))def on_release(key):    print('{0} release'.format(        key))    if key == Key.esc:        # Stop listener        return False# Collect events until releasedwith Listener(        on_press=on_press,        on_release=on_release) as listener:    listener.join()

上面的功能将打印你按下的任何键,并在你释放“ esc”键时开始执行 *** 作。键盘文档在这里用于更多变化的用法

马库斯·冯·布罗迪

(Markus von Broady)
强调了一个潜在的问题,即:这个答案并不需要你在当前窗口中激活此脚本,Windows的解决方案是:

from win32gui import GetWindowText, GetForegroundWindowcurrent_window = (GetWindowText(GetForegroundWindow()))desired_window_name = "Stopwatch" #Whatever the name of your window should be#Infinite loops are dangerous.while True: #Don't rely on this line of pre too much and make sure to adapt this to your project.    if current_window == desired_window_name:        with Listener( on_press=on_press, on_release=on_release) as listener: listener.join()


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zaji/4985347.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-11-14
下一篇2022-11-14

发表评论

登录后才能评论

评论列表(0条)

    保存