🪝
Portfolio & Project Blog/OpenCrashCart - A $50 USB KVM Console/🪝pyWinHook (Hooking keyboard input for shortcuts)

pyWinHook (Hooking keyboard input for shortcuts)

This works to block keyboard input for various keys! It uses an updated version of pyhook (pyWinhook) to block modifer keys and such, then present them to the program. Next steps are making that toggle whether or not the KVM window is focused!
 
## {{{ http://code.activestate.com/recipes/553270/ (r1) import pyWinhook import pygame # create a keyboard hook def OnKeyboardEvent(event): print ('MessageName:',event.MessageName) print ('Message:',event.Message) print ('Time:',event.Time) print ('Window:',event.Window) print ('WindowName:',event.WindowName) print ('Ascii:', event.Ascii, chr(event.Ascii)) print ('Key:', event.Key) print ('KeyID:', event.KeyID) print ('ScanCode:', event.ScanCode) print ('Extended:', event.Extended) print ('Injected:', event.Injected) print ('Alt', event.Alt) print ('Transition', event.Transition) print ('---') if event.Key.lower() in ['lwin', 'tab', 'lmenu']: return False # block these keys else: # return True to pass the event to other handlers return True # create a hook manager hm = pyWinhook.HookManager() # watch for all keyboard events hm.KeyDown = OnKeyboardEvent # set the hook hm.HookKeyboard() # initialize pygame and start the game loop pygame.init() while(1): pygame.event.pump() ## end of http://code.activestate.com/recipes/553270/ }}}
this works, but next steps are to make it register on both key up and key down - right now, it’s juts key down, and I need to pass the events properly. look at https://pyhook.sourceforge.net/doc_1.5.0/pyhook.HookManager.KeyboardEvent-class.html#IsTransition in the docs!
 
Rest of keyboard info is here:
⌨️
Revamped Keyboard Capture