import os
import sys
import signal
import ctypes
import atexit
from ctypes import wintypes, windll, CFUNCTYPE, POINTER, c_int, c_void_p, byref
from collections import namedtuple
event_types = {
0x100: 'key down',
0x101: 'key up',
0x104: 'key down',
0x105: 'key up',
}
KeyEvent = namedtuple("KeyEvent", ['event_type', 'key_code', 'scan_code', 'alt_pressed', 'time'])
handlers = []
def listener():
def hook(nCode, wParam, lParam):
event = KeyEvent(event_types[wParam], lParam[0], lParam[1], lParam[2] == 32, lParam[3])
for handler in handlers:
handler(event)
return windll.user32.CallNextHookEx(hook_id, nCode, wParam, lParam)
CMPFUNC = CFUNCTYPE(c_int, c_int, c_int, POINTER(c_void_p))
pointer = CMPFUNC(hook)
windll.kernel32.GetModuleHandleW.restype = wintypes.HMODULE
windll.kernel32.GetModuleHandleW.argtypes = [wintypes.LPCWSTR]
hook_id = windll.user32.SetWindowsHookExA(0x00D, pointer, windll.kernel32.GetModuleHandleW(None), 0)
atexit.register(windll.user32.UnhookWindowsHookEx, hook_id)
while True:
msg = windll.user32.GetMessageW(None, 0, 0, 0)
windll.user32.TranslateMessage(byref(msg))
windll.user32.DispatchMessageW(byref(msg))
def run_on_enter(event):
if event.event_type == 'key up' and event.key_code == 120259084301:
os.system('notepad.exe')
if __name__ == '__main__':
signal.signal(signal.SIGINT, lambda s, f: sys.exit())
handlers.append(run_on_enter)
listener()