如何区分pynput和键盘/鼠标输入?
假设以下代码:
from pynput.keyboard import Key, Controller
from pynput.keyboard import Listener
from time import sleep
def onPress(key):
if key == Key.space:
print("Hi")
keyboardController = Controller()
keyboardListener = Listener(on_press=onPress)
keyboardListener.start()
while True:
sleep(5)
keyboardController.press(Key.space)
每次我按键盘上的空格,也将每5秒钟按空格打印“ HI”。
一些游戏能够使用键BD_EVENT函数生成生成的“忽略”键盘/鼠标输入(这是PYNPUT用于生成WM_KEYUP/WM_KEYDOWN消息AFAIK的用途)。但是,pynput中的键盘侦听器似乎无法区分“驱动程序键按消息”(?)和键BD_EVENT。有没有办法只听真正的键盘键按下? (最好使用pynput,但如果不可能,其他解决方案也受到高度欢迎)
Assume the following code:
from pynput.keyboard import Key, Controller
from pynput.keyboard import Listener
from time import sleep
def onPress(key):
if key == Key.space:
print("Hi")
keyboardController = Controller()
keyboardListener = Listener(on_press=onPress)
keyboardListener.start()
while True:
sleep(5)
keyboardController.press(Key.space)
This will print "Hi" everytime I press space on my keyboard but also every 5 seconds.
Some games are able to "ignore" keyboard/mouse inputs which are generated using the keybd_event function (This is what pynput uses to generate WM_KEYUP/WM_KEYDOWN messages afaik). However, the keyboard listener in pynput does not seem to be able to distinguish between "driver key press messages" (?) and keybd_events. Is there a way to only listen to real keyboard key presses? (Preferably using pynput but if thats not possible other solutions are highly welcomed as well)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果其他人想知道该如何解决:
Win32_Event_Filter似乎为此提供了一个可能的解决方案。您可以检查
data.flags == 16
,如果键按键键注入,否则最有可能是物理键按。但是,注射修饰符钥匙按下有“误报”。If anyone else is wondering how this could be solved:
win32_event_filter seems to provide a possible solution for this. You can check if
data.flags == 16
, if true the key press has been injected otherwise its most likely a physical key press. However, there are "false positives" for injected modifier key presses.