|
Description:
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.
Source: Text Source
import pyHook
import pygame
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
else:
return True
hm = pyHook.HookManager()
hm.KeyDown = OnKeyboardEvent
hm.HookKeyboard()
pygame.init()
while(1):
pygame.event.pump()
Discussion:
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.
|