传递
suppress=True
标志给监听器将抑制系统范围内的所有事件。若这不是想要的,必须为不同后端运用不同解决方案。
若选取的后端下文未列出,则它不支持特定事件的抑制。
For
macOS
,传递命名自变量
darwin_intercept
到监听器构造函数。此自变量应该是可调用接受自变量
(event_type, event)
,其中
event_type
是任何鼠标相关的事件类型常量,和
event
是
CGEventRef
。
event
自变量可以被操纵通过查找函数在
Apple 文档编制
.
若拦截器函数确定应抑制事件,返回
None
,否则返回
event
可以修改。
这里是键盘范例:
def darwin_intercept(event_type, event): import Quartz length, chars = Quartz.CGEventKeyboardGetUnicodeString( event, 100, None, None) if length > 0 and chars == 'x': # Suppress x return None elif length > 0 and chars == 'a': # Transform a to b Quartz.CGEventKeyboardSetUnicodeString(event, 1, 'b') else: return event
For
Windows
,传递自变量命名
win32_event_filter
到监听器构造函数。此自变量应该是可调用接受自变量
(msg, data)
,其中
msg
是当前消息,和
data
关联数据如
MSLLHOOKSTRUCT
或
KBDLLHOOKSTRUCT
,取决于创建的是鼠标监听器还是键盘监听器。
若过滤函数确定应抑制事件,则调用
suppress_event
在监听器。若返回
False
,事件将对其它监听器回调被隐藏。
这里是键盘范例:
# Values for MSLLHOOKSTRUCT.vkCode can be found here: # https://docs.microsoft.com/en-us/windows/win32/inputdev/virtual-key-codes def win32_event_filter(msg, data): if data.vkCode == 0x58: # Suppress x listener.suppress_event()
ImportError
在启动时
¶
这会发生当使用打包程序时,譬如 PyInstaller 打包应用程序。
The reason for the error is that the packager attempts to build a dependency tree of the modules used by inspecting
import
statements, and
pynput
finds the platform dependent backend modules at runtime using
importlib
.
To solve this problem, please consult the documentation of your tool to find how to explicitly add modules.
Which modules to add depends on your distribution platform. The backend modules are those starting with an underscore (
'_'
) in the
pynput.keyboard
and
pynput.mouse
packages. Additionally, you will need modules with corresponding names from the
pynput._util
包。