def prepare_intercept(callback):
_setup_name_tables()<EOL>def process_key(event_type, vk, scan_code, is_extended):<EOL><INDENT>global shift_is_pressed, altgr_is_pressed, ignore_next_right_alt<EOL>if vk == <NUM_LIT> and ignore_next_right_alt:<EOL><INDENT>ignore_next_right_alt = False<EOL>return True<EOL><DEDENT>modifiers = (<EOL>('<STR_LIT>',) * shift_is_pressed +<EOL>('<STR_LIT>',) * altgr_is_pressed +<EOL>('<STR_LIT>',) * (user32.GetKeyState(<NUM_LIT>) & <NUM_LIT:1>) +<EOL>('<STR_LIT>',) * (user32.GetKeyState(<NUM_LIT>) & <NUM_LIT:1>) +<EOL>('<STR_LIT>',) * (user32.GetKeyState(<NUM_LIT>) & <NUM_LIT:1>)<EOL>)<EOL>entry = (scan_code, vk, is_extended, modifiers)<EOL>if entry not in to_name:<EOL><INDENT>to_name[entry] = list(get_event_names(*entry))<EOL><DEDENT>names = to_name[entry]<EOL>name = names[<NUM_LIT:0>] if names else None<EOL>if vk in shift_vks:<EOL><INDENT>shift_is_pressed = event_type == KEY_DOWN<EOL><DEDENT>if scan_code == <NUM_LIT> and vk == <NUM_LIT>:<EOL><INDENT>ignore_next_right_alt = True<EOL>altgr_is_pressed = event_type == KEY_DOWN<EOL><DEDENT>is_keypad = (scan_code, vk, is_extended) in keypad_keys<EOL>return callback(KeyboardEvent(event_type=event_type, scan_code=scan_code or -vk, name=name, is_keypad=is_keypad))<EOL><DEDENT>def low_level_keyboard_handler(nCode, wParam, lParam):<EOL><INDENT>try:<EOL><INDENT>vk = lParam.contents.vk_code<EOL>fake_alt = (LLKHF_INJECTED | <NUM_LIT>)<EOL>if vk != VK_PACKET and lParam.contents.flags & fake_alt != fake_alt:<EOL><INDENT>event_type = keyboard_event_types[wParam]<EOL>is_extended = lParam.contents.flags & <NUM_LIT:1><EOL>scan_code = lParam.contents.scan_code<EOL>should_continue = process_key(event_type, vk, scan_code, is_extended)<EOL>if not should_continue:<EOL><INDENT>return -<NUM_LIT:1><EOL><DEDENT><DEDENT><DEDENT>except Exception as e:<EOL><INDENT>print('<STR_LIT>')<EOL>traceback.print_exc()<EOL><DEDENT>return CallNextHookEx(None, nCode, wParam, lParam)<EOL><DEDENT>WH_KEYBOARD_LL = c_int(<NUM_LIT>)<EOL>keyboard_callback = LowLevelKeyboardProc(low_level_keyboard_handler)<EOL>handle =  GetModuleHandleW(None)<EOL>thread_id = DWORD(<NUM_LIT:0>)<EOL>keyboard_hook = SetWindowsHookEx(WH_KEYBOARD_LL, keyboard_callback, handle, thread_id)<EOL>atexit.register(UnhookWindowsHookEx, keyboard_callback)<EOL>
Registers a Windows low level keyboard hook. The provided callback will
be invoked for each high-level keyboard event, and is expected to return
True if the key event should be passed to the next program, or False if
the event is to be blocked.

No event is processed until the Windows messages are pumped (see
start_intercept).