Python keyboard library arrow keys problem(Python键盘库方向键问题)
问题描述
我正在编写一个脚本,它会截取屏幕截图并解码以图像名称命名的特定按键,如下所示.我的问题是,当我按下左键盘箭头时,也按下了数字 4.我在谷歌或键盘库的文档中找不到任何东西.我正在使用 Windows 和 Python 3.6.5
I was writing a script, which takes a screenshot and decodes specific key presses in the name of the image as seen below. My problem is that when I press the left keyboard arrow, also the number 4 is pressed. I can't find anything on google or in the documentation of the keyboard library. I am using Windows and Python 3.6.5
(75,)
left arrow pressed
(5, 75)
4 pressed
向下箭头也会发生同样的事情,但数字 3 是这样的.
The same thing happens with the down arrow, but with the number 3.
(80,)
down arrow pressed
(3, 80)
2 pressed
代码:
from PIL import ImageGrab
import keyboard # using module keyboard
import time
keys = [
"down arrow",
"up arrow",
"left arrow",
"right arrow",
"w",
"s",
"a",
"d",
"1",
"2",
"3",
"4",
"q",
e",f"]
if __name__ == "__main__":
while True:
code = []
try:
for key in keys:
if keyboard.is_pressed(key):
print(keyboard.key_to_scan_codes(key))
print(f"{key} pressed")
code.append(1)
else:
code.append(0)
if keyboard.is_pressed('esc'):
print(key + " pressed")
break
c = "".join(map(str, code))
snapshot = ImageGrab.grab()
save_path = str(int(time.time()*1000)) + "-" + c + ".jpg"
snapshot.save("tmp\" + save_path)
except:
break
推荐答案
keyboard
模块对于此类实例有简单的解决方案,它们使用 event-triggered
激活而不是polling
在您的尝试中使用.
The keyboard
module has simple solutions for instances like these, they use event-triggered
activation rather than polling
as is used in your attempt.
示例代码:
import keyboard
def handleLeftKey(e):
if keyboard.is_pressed("4"):
print("left arrow was pressed w/ key 4")
# work your magic
keyboard.on_press_key("left", handleLeftKey)
# self-explanitory: when the left key is pressed down then do something
keyboard.on_release_key("left", handleLeftKey02)
# also self-explanitory: when the left key is released then do something
# don't use both ...on_release & ...on_press or it will be
# triggered twice per key-use (1 up, 1 down)
替换下面的代码并根据您的需要进行更改.
Replace the code below and change it to suit your needs.
if __name__ == "__main__":
while True:
code = []
try:
for key in keys:
if keyboard.is_pressed(key):
print(keyboard.key_to_scan_codes(key))
print(f"{key} pressed")
code.append(1)
else:
code.append(0)
另一种更动态的方法如下所示:
Another, more dynamic approach would look like:
import keyboard
keys = [
"down",
"up",
"left",
"right",
"w",
"s",
"a",
"d",
"1",
"2",
"3",
"4",
"q",
"e",
"f"
]
def kbdCallback(e):
found = False
for key in keys:
if key == keyboard.normalize_name(e.name):
print(f"{key} was pressed")
found = True
# work your magic
if found == True:
if e.name == "left":
if keyboard.is_pressed("4"):
print("4 & left arrow were pressed together!")
# work your magic
keyboard.on_press(kbdCallback)
# same as keyboard.on_press_key, but it does this for EVERY key
我注意到的另一个问题是您使用 "left arrow"
而实际上它被识别为 "left"
(至少在我的系统上,它可能会有所不同在你的,但我假设你希望它在所有系统上工作,所以使用 "left"
会更安全)
Another issue I noticed was that you were using "left arrow"
when really it was recognized as "left"
(at least on my system, it may be different on yours, but I assume you want it to work on all systems so it'd be safer using "left"
instead)
您可以使用的最后一种方法是非常静态类型的并且没有动态功能,但可以在 "4+left"
或 "left+4"
The last method you could use is very statically typed and has no dynamic capabilities, but would work in the case of "4+left"
or "left+4"
import keyboard
def left4trigger:
print("the keys were pressed")
keyboard.add_hotkey("4+left", left4trigger)
# works as 4+left or left+4 (all of the examples do)
你看起来很聪明,可以从那里弄清楚其余的事情.
You seem smart enough to figure out the rest from there.
这篇关于Python键盘库方向键问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Python键盘库方向键问题
基础教程推荐
- 如何在海运重新绘制中自定义标题和y标签 2022-01-01
- 在 Python 中,如果我在一个“with"中返回.块,文件还会关闭吗? 2022-01-01
- 用于分类数据的跳跃记号标签 2022-01-01
- Python kivy 入口点 inflateRest2 无法定位 libpng16-16.dll 2022-01-01
- 何时使用 os.name、sys.platform 或 platform.system? 2022-01-01
- 使用PyInstaller后在Windows中打开可执行文件时出错 2022-01-01
- Dask.array.套用_沿_轴:由于额外的元素([1]),使用dask.array的每一行作为另一个函数的输入失败 2022-01-01
- 如何让 python 脚本监听来自另一个脚本的输入 2022-01-01
- 线程时出现 msgbox 错误,GUI 块 2022-01-01
- 筛选NumPy数组 2022-01-01