栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

在python中检测按键?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

在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()


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/393793.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号