如何同时使用pynput键盘侦听器和控制器?
我是Python编程的初学者,并且一直在研究一个非常简单的程序,以帮助我在应用程序中自动化任务。基本上,在F5的印刷机上,我希望执行一系列特定的命令,在F6的出版社中,我也希望另外一系列特定的命令可以运行。
我这样做的方式是使用pynput库: 此代码用于检测何时我按F5或f6:
with keyboard.GlobalHotKeys({
'<f5>': on_activate_f5,
'<f6>': on_activate_f6}) as h:
h.join()
然后执行指令的代码(函数on_activate_f5导致了这一点):
def leave():
printer("----LEAVE INITIATED----")
Controller().press(Key.esc)
Controller().release(Key.esc)
print("--step1 complete--")
pyautogui.click(300,420)
print("--step2 complete--")
pyautogui.click(920,560)
print("--step3 complete--")
printer("----LEAVE COMPLETE----")
但是,我在上面的代码中遇到了几个问题:
1:首先所有“ f”键无法正常运行。当我用此替换Hotkeys代码...
with keyboard.GlobalHotKeys({
'<ctrl>+<alt>+h': on_activate_f5,
'<ctrl>+<alt>+j': on_activate_f6}) as h:
h.join()
...然后正确检测到键Ctrl Alt和H / J。但是,当我尝试使用所有F键(F6,F5,F10等)时,Pynput似乎没有检测到,尽管我没有错误消息:/
2:每当我尝试使用Pynput按Escape键时键盘它只会离开应用程序窗口返回控制台。我根本不知道为什么。它实际上可以与其他键(key.space)等完美搭配,但是每次我使用key.esc时,它都会离开窗口。
该代码本身实际上更长(约300行),因为它包含基本的TKINTER GUI,线程等。
如果问题位于代码的其余部分中,则我将OFC发送整个版本,如果有人觉得这是必要的。但是,根据我的测试,我非常有信心在这几行中错误。
基本上,如果有人可以向我解释“ F”键如何与Pynput一起使用,以及为什么使用Controller()。按(key.esc)离开窗口,我会非常谨慎。
互补信息:
- Windows 10
- Python 3.8
- Pynput 1.7.6
- Visual Studio 2019
- 为整个程序完成的导入:
=“ https://i.sstatic.net/dubo5.png” alt =“导入二手”>
坦克您的时间,随时提出任何要求!
I am a beginner at python programming, and I have been working on a pretty simple program to help me automate tasks in an app. Basically at the press of F5 I wish for a specific series of commands to execute, and at the press of f6 I wish for another specific of series of commands to run as well.
The way I have done it is using the pynput library:
This code for detecting when I pressed f5 or f6:
with keyboard.GlobalHotKeys({
'<f5>': on_activate_f5,
'<f6>': on_activate_f6}) as h:
h.join()
Then the code to execute instructions (the function on_activate_f5 leads to this):
def leave():
printer("----LEAVE INITIATED----")
Controller().press(Key.esc)
Controller().release(Key.esc)
print("--step1 complete--")
pyautogui.click(300,420)
print("--step2 complete--")
pyautogui.click(920,560)
print("--step3 complete--")
printer("----LEAVE COMPLETE----")
However I am experiencing several issues with the code above:
1: First of all the "f" keys are not functionning correctly. When I replace the hotkeys code with this...
with keyboard.GlobalHotKeys({
'<ctrl>+<alt>+h': on_activate_f5,
'<ctrl>+<alt>+j': on_activate_f6}) as h:
h.join()
... then the keys ctrl alt and h / j are properly detected. However when I tried with all the f keys (f6, f5, f10 etc), none seem to be detected by pynput, although I got no error message :/
2: Whenever I try to press the escape key with the pynput keyboard it just leaves the app window to go back to the console. I have simply no clue why. It actually works perfectly with other keys (key.space) etc but EVERY time I use key.esc it just leaves the window.
The code in itself is actually much longer (around 300 lines) as it includes a basic tkinter GUI, threading etc.
If the problem resides in the rest of the code then I will ofc send the entire version, if anyone feels like it is necessary. Based on my testing however I am pretty confident the error is in these few lines.
Basically if anyone could explain to me how / why the "f" keys are not working with pynput and why using Controller().press(key.esc) leaves the window, I would very gratefull.
Complementary info:
- Windows 10
- Python 3.8
- Pynput 1.7.6
- Visual Studio 2019
- Imports done for the whole program:
Tank you for your time, feel free to ask anything !
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以通过在任何窗口中按“ Alt+ESC”来找到答案。
如果您在从热键中释放ALT键之前按程序中的ESC键,则窗口立即关闭,因为“ Alt+Esc”是关闭窗口的HotKey。
You can find the answer by pressing "alt+esc" in any window.
If you press the esc key in the program before you release the alt key from the hotkey, the window closes immediately because "alt+esc" is the hotkey that closes the window.
'&lt; f5&gt;'
and'f6&gt;'
的原因是因为您不能使用该密钥类型。而是分别使用'&lt; 116&gt;'
和'&lt; 117&gt;'
。 Hotkeys的解析返回那些类型的键。The reason why
'<f5>'
and'<f6>'
is not working is because you can't use that key type. Instead use'<116>'
and'<117>'
respectively. The parsing of the hotkeys returns those type of key.