Normally you do NOT want to block operating system key combinations but there are a few legitimate cases where you do. In my case I am making a pygame script for my 1 year old to bang on the keyboard and see/hear shapes/color/sounds in response. Brian Fischer on the pygame mailing list pointed me to pyHook. This example was taken from here: http://www.mindtrove.info/articles/pyhook.html and modified to use the pygame event system.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | import pyHook
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 = pyHook.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()
|
pyHook does not block the ctrl-alt-del combination even if you try to make it. (which is a good thing) But in my case I can now easily block the windows key and the alt-tab combination. I plan to just use the hook to block a few events and do my regular event processing after the pygame.event.pump() call, like any normal pygame loop.
UPDATE 09/2011 I've added this same example to my blog, where I intend to post future recipes and discussions. http://fadedbluesky.com/2011/using-pyhook-to-block-windows-key/