python - 在 python 中使用系统范围的热键运行函数
问题描述
假设有这个代码
import time
function():
print("abcd")
time.sleep(1)
print("efgh")
function()
function()
每当我从电脑上的任何地方按下组合键时,我都希望它运行。就像,假设我在 chrome 上观看 youtube 视频,我按下假设 shift+alt+b 然后function()
应该开始运行并打印这些东西。
我猜我在 Windows 10 上使用 python 3.8。
解决方案
哦,我自己想通了它比这容易得多pynput
您需要使用该keyboard
模块
这是代码:
import keyboard
import time
function():
print("abcd")
time.sleep(1)
print("efgh")
while True: # making a loop
try: # used try so that if user pressed other than the given key error will not be shown
if keyboard.is_pressed('shift'):
if keyboard.is_pressed('alt'):
if keyboard.is_pressed('b'):
function()
except:
pass
它比其他解决方案简单
推荐阅读
- javascript - 从未调用过自定义函数
- r - R tmap:: 内边距出现虽然设置为零
- javascript - 无法弄清楚为什么我的脚本停止工作。.
- angular - 从角度服务返回可观察的
- python-3.x -
ImportError:缺少必需的依赖项 ['numpy'] - java - 在 Spark 中读取之前如何检查文件夹的可用性?
- python - 如何对颠倒的单词进行列表理解?
- c# - 使用正则表达式匹配从字符串中获取列表[C#]
- css - CSS 按内容优先级管理嵌套的 flexbox 宽度
- java - 为什么我运行'JBoss 11.0.0.Final'时出错:无法打开调试器端口(127.0.0.1:56884):java.net.SocketException“套接字已关闭”?