def hook_keyboard():
hotkey_list = register_hotkeys()
try:
message = wintypes.MSG()
while True:
msg = windll.user32.GetMessageW(byref(message), 0, 0, 0)
print("found message {0}".format(msg))
if msg == -1:
sys.exit(0)
elif msg == 0: # WM_QUIT
sys.exit(0)
else:
windll.user32.TranslateMessage(byref(message))
windll.user32.DispatchMessageW(byref(message))
finally:
unregister_hotkeys(hotkey_list)
python类MSG的实例源码
def shell__pump_messages(on_exit_callback=None):
assert on_exit_callback is None or callable(on_exit_callback)
message = wintypes.MSG()
while True:
msg = GetMessageW(byref(message), 0, 0, 0)
if msg <= 0:
# print("DEBUG quit message in queue")
# 0 means WM_QUIT, < 0 means error
if on_exit_callback is not None and callable(on_exit_callback):
on_exit_callback()
else:
sys.exit(0)
else:
TranslateMessage(byref(message))
DispatchMessageW(byref(message))
# see https://msdn.microsoft.com/en-us/library/windows/desktop/ms644967(v=vs.85).aspx
def _translate_message_hook(self, pmsg: MSG):
msg = pmsg.contents
if msg.message == WM_HOTKEY:
keycode = (msg.lParam >> 16) & 0xFFFF
modifiers = msg.lParam & 0xFFFF
key = (keycode, modifiers)
if key in self.shortcuts:
QTimer.singleShot(0, self.shortcuts[key])
return False
return self._TranslateMessageReal(pmsg)
def run(self):
if self.install_hook():
#print "mouselogger installed"
pass
else:
raise RuntimeError("couldn't install mouselogger")
msg = MSG()
user32.GetMessageA(byref(msg),0,0,0)
while not self.stopped:
time.sleep(1)
self.uninstall_hook()
def run(self):
if self.install_hook():
#print "mouselogger installed"
pass
else:
raise RuntimeError("couldn't install mouselogger")
msg = MSG()
user32.GetMessageA(byref(msg),0,0,0)
while not self.stopped:
time.sleep(1)
self.uninstall_hook()
def handle_hotkey(root, callback):
msg = wintypes.MSG()
if windll.user32.GetMessageA(byref(msg), None, 0, 0) != 0:
if msg.message == win32con.WM_HOTKEY:
if msg.wParam == 1:
print 'Hotkey triggered!'
callback()
windll.user32.TranslateMessage(byref(msg))
windll.user32.DispatchMessageA(byref(msg))
root.after(1, handle_hotkey, root, callback)
# hotkey map refs: https://msdn.microsoft.com/en-us/library/windows/desktop/dd375731(v=vs.85).aspx
# not yet used here.
def run(self):
if self.install_hook():
#print "mouselogger installed"
pass
else:
raise RuntimeError("couldn't install mouselogger")
msg = MSG()
user32.GetMessageA(byref(msg),0,0,0)
while not self.stopped:
time.sleep(1)
self.uninstall_hook()
def handle_hotkey(root, callback):
msg = wintypes.MSG()
if windll.user32.GetMessageA(byref(msg), None, 0, 0) != 0:
if msg.message == win32con.WM_HOTKEY:
if msg.wParam == 1:
print 'Hotkey triggered!'
callback()
windll.user32.TranslateMessage(byref(msg))
windll.user32.DispatchMessageA(byref(msg))
root.after(1, handle_hotkey, root, callback)
# hotkey map refs: https://msdn.microsoft.com/en-us/library/windows/desktop/dd375731(v=vs.85).aspx
# not yet used here.
def startKeyLog(self):
msg = MSG()
ctypes.windll.user32.GetMessageA(ctypes.byref(msg),0,0,0)
def startKeyLog(self):
msg = MSG()
ctypes.windll.user32.GetMessageA(ctypes.byref(msg),0,0,0)
def startKeyLog():
msg = MSG()
user32.GetMessageA(byref(msg), 0, 0, 0)
def handle_hotkey(root, callback):
msg = wintypes.MSG()
if windll.user32.GetMessageA(byref(msg), None, 0, 0) != 0:
if msg.message == win32con.WM_HOTKEY:
if msg.wParam == 1:
# print 'Hotkey triggered!'
callback()
windll.user32.TranslateMessage(byref(msg))
windll.user32.DispatchMessageA(byref(msg))
root.after(1, handle_hotkey, root, callback)
# hotkey map refs: https://msdn.microsoft.com/en-us/library/windows/desktop/dd375731(v=vs.85).aspx
# not yet used here.
def WinMSGLoop():
"""Run the main windows message loop."""
LPMSG = POINTER(MSG)
LRESULT = c_ulong
GetMessage = get_winfunc("user32", "GetMessageW", BOOL, (LPMSG, HWND, UINT, UINT))
TranslateMessage = get_winfunc("user32", "TranslateMessage", BOOL, (LPMSG,))
# restype = LRESULT
DispatchMessage = get_winfunc("user32", "DispatchMessageW", LRESULT, (LPMSG,))
msg = MSG()
lpmsg = byref(msg)
while GetMessage(lpmsg, HWND(), 0, 0) > 0:
TranslateMessage(lpmsg)
DispatchMessage(lpmsg)
def startKeyLog(self):
msg = MSG()
ctypes.windll.user32.GetMessageA(ctypes.byref(msg),0,0,0)
def hook_keyboard():
def handler(key_code, event_code):
print("{0} {1}".format(hex(key_code), hex(event_code)))
handle = [None]
def low_level_callback(code, rparam, lparam):
try:
key_code = 0xFFFFFFFF & lparam[0] # key code
handler(key_code, rparam & 0xFFFFFFFF)
finally:
return CallNextHookEx(handle[0], code, rparam, lparam)
callback_pointer = CFUNCTYPE(c_int, c_int, wintypes.HINSTANCE, POINTER(c_void_p))(low_level_callback)
handle[0] = SetWindowsHookExA(WH_KEYBOARD_LL, callback_pointer, GetModuleHandleA(None), 0)
atexit.register(UnhookWindowsHookEx, handle[0])
# Message pumper
message = wintypes.MSG()
while True:
msg = GetMessageW(byref(message), 0, 0, 0)
if msg == -1:
UnhookWindowsHookEx(handle[0])
sys.exit(0)
elif msg == 0: # GetMessage return 0 only if WM_QUIT
sys.exit(0)
else:
TranslateMessage(byref(message))
DispatchMessageW(byref(message))