def hook_keyboard(callback):
handle = None
def low_level_callback(code, rparam, lparam):
try:
key_code = 0xFFFFFFFF & lparam[0] # key code
callback(key_code, rparam & 0xFFFFFFFF)
finally:
return CallNextHookEx(handle, code, rparam, lparam)
# The really big problem is the callback_pointer.
# Once it goes out of scope, it is destroyed
callback_pointer = CFUNCTYPE(c_int, c_int, wintypes.HINSTANCE, POINTER(c_void_p))(low_level_callback)
__CALLBACK_POINTERS.append(callback_pointer)
handle = SetWindowsHookExA(WH_KEYBOARD_LL, callback_pointer, GetModuleHandleA(None), 0)
atexit.register(UnhookWindowsHookEx, handle)
return handle
评论列表
文章目录