python禁用键盘鼠标_在Python中禁用或锁定鼠标和键盘?

我已经将Fábio Diniz的答案扩展到一个类,该类提供block()和unblock()函数,这两个函数(有选择地)阻止鼠标/键盘输入。我还添加了一个超时功能(希望)来解决锁定自己的烦恼。import pyHook

from threading import Timer

import win32gui

import logging

class blockInput():

def OnKeyboardEvent(self,event):

return False

def OnMouseEvent(self,event):

return False

def unblock(self):

logging.info(" -- Unblock!")

if self.t.is_alive():

self.t.cancel()

try: self.hm.UnhookKeyboard()

except: pass

try: self.hm.UnhookMouse()

except: pass

def block(self, timeout = 10, keyboard = True, mouse = True):

self.t = Timer(timeout, self.unblock)

self.t.start()

logging.info(" -- Block!")

if mouse:

self.hm.MouseAll = self.OnMouseEvent

self.hm.HookMouse()

if keyboard:

self.hm.KeyAll = self.OnKeyboardEvent

self.hm.HookKeyboard()

win32gui.PumpWaitingMessages()

def __init__(self):

self.hm = pyHook.HookManager()

if __name__ == '__main__':

logging.basicConfig(level=logging.INFO)

block = blockInput()

block.block()

import time

t0 = time.time()

while time.time() - t0 < 10:

time.sleep(1)

print(time.time() - t0)

block.unblock()

logging.info("Done.")

你可以看看主要的程序例如用法。

你可能感兴趣的:(python禁用键盘鼠标)