python - Simulating key hold without stopping loop
问题描述
I'm using pynput to simulate key press.
Here's my function i'm using in a loop
def press_hotkey(key, time):
keyboard.press(key)
sleep(time) #hold time
keyboard.release(key)
Sometimes I need to press one key for certain amount of time, for example 2 seconds, but I don't want to stop my loop, as it should continue and press next key if needed.
What would be the most correct way to do that? Is running a function in new thread every iteration a good idea?
解决方案
你可以使用线程模块
import threading
然后像这样在线程中调用你的函数
thread = threading.Thread(target=press_hotkey, args=(desired_key, desired_time))
thread.start()
推荐阅读
- java - 用于 pom 和 jar 文件的构建工具的 Gradle 错误
- sql - 在 PostgreSQL 中使用 COPY TO
- typescript - TypeScript 使用本地声明扩展 discord.js
- azure-language-understanding - 与实体无关的 LUIS 意图
- excel - 获取变量中包含的数字的最后一位
- python-3.x - Windows10 ubuntu bash:chromedriver无法启动Chrome?
- deterministic - Jetson TX2 上的 TensorRT“浮点 16”精度模式是否不确定?
- python - 在密度图上覆盖 Shapefile 数据点
- javascript - 包与其内部 node_modules 包依赖项无关,而是使用外部范围
- python-3.x - 连接到 docker 容器 (aiohttp) 内的 PostreSQL 时连接调用失败