Python阻止键盘书写

我正在尝试找到一种方法:

  • 聆听键盘上的压力
  • 防止键盘在屏幕上书写

在研究过程中,我发现了这一点

1)BlockInput用于阻止Windows上的键盘(我试过但没用)

import ctypes
from ctypes import wintypes

BlockInput = ctypes.windll.user32.BlockInput
BlockInput.argtypes = [wintypes.BOOL]
BlockInput.restype = wintypes.BOOL

blocked = BlockInput(True)
if blocked:
    try:
        pass # do something
    finally:
        unblocked = BlockInput(False) # unblock in any case
else:
    raise RuntimeError('Input is already blocked by another thread!')

2)PyHook(不再受支持的旧库)

import pythoncom, pyHook 

def uMad(event):
    return False

hm = pyHook.HookManager()
hm.MouseAll = uMad
hm.KeyAll = uMad
hm.HookMouse()
hm.HookKeyboard()
pythoncom.PumpMessages()

还有其他解决方案(Win 10)吗?

评论