forked from Answeror/lit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhook.py
60 lines (50 loc) · 1.88 KB
/
hook.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import time
from ctypes import *
import win32con, win32api, win32gui
class KeyboardHook(object):
"""
Written by: TwhK / Kheldar
What do? Installs a global keyboard hook
To install the hook, call the (gasp!) installHook() function.
installHook() takes a pointer to the function that will be called
after a keyboard event. installHook() returns True if everything
was successful, and False if it failed
Note: I've also provided a function to return a valid function pointer
To make sure the hook is actually doing what you want, call the
keepAlive() function
Note: keepAlive() doesn't return until kbHook is None, so it should
be called from a separate thread
To uninstall the hook, call uninstallHook()
Note: relies on modules provided by pywin32.
http://sourceforge.net/projects/pywin32/
"""
def __init__(self):
self.user32 = windll.user32
self.kbHook = None
def installHook(self, pointer):
self.kbHook = self.user32.SetWindowsHookExA(
win32con.WH_KEYBOARD_LL,
pointer,
win32api.GetModuleHandle(None),
0 # this specifies that the hook is pertinent to all threads
)
if not self.kbHook:
return False
return True
def stop(self):
self.uninstallHook()
self.kbHook = None
def keepAlive(self):
if self.kbHook is None:
return
while self.kbHook is not None:
win32gui.PumpWaitingMessages()
#time.sleep(0.04) # 25Hz
time.sleep(0.001) # prevent delay when hold press
def uninstallHook(self):
if self.kbHook is None:
return
self.user32.UnhookWindowsHookEx(self.kbHook)
self.kbHook = None