[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [pygame] full keyboard control



I know it's a little late, but  here's a solution, though it's not a
good one.  Disclaimer: I wrote this on a Linux box, so I don't know if
it will work on windows. (See attached file)

Essentially, you let the OS's Alt-Tab do its thing, then just bring
the game back to the forefront.  I assume the game is fullscreen to
start with, and when the user hits "ALT TAB", the game becomes
windowed, then fullscreen again.

As for the windows key, I think you'll want to check for K_LSUPER and
K_RSUPER, but I'm not sure.  (see the key constants here:
http://www.pygame.org/docs/ref/key.html)

Brad

PS: Do all Father/Mother programmers try to write a game for their 1-year-old?
'''
    This is an example that shows a hack to prevent Alt-Tab switching
    If we find an Alt-Tab key combination, make the game Windowed, then 
    immediately make it fullscreen again. We'll see a quick "blink" when 
    this happens.

    Written by Brad Montgomery (bradmontgomery.net) on 2008-03-08.

    This code is public domain. Have fun and be nice.
'''
import sys, pygame
from pygame.locals import * 

resolution = (800,600)
keys = [] # list of keys to remember
num_keys_to_remember = 3

def input(events): 
    ''' A function to handle keyboard/mouse/device input events. '''
    global keys
    for event in events: 
        if event.type == QUIT or (event.type == KEYDOWN and event.key == K_ESCAPE):
            sys.exit(0)
        elif event.type == KEYDOWN:
            if len(keys) >= num_keys_to_remember:
                keys = []
            keys.append(event.key)

    # check for Alt-Tab 
    if K_TAB in keys and (K_RALT in keys or K_LALT in keys):
        print 'Caught ALT-TAB'
        if pygame.display.get_active:
            pygame.display.toggle_fullscreen() # sets app to not-fullscreen
            pygame.display.toggle_fullscreen() # sets fullscreen again
            keys = [] # since we caught what we were 
                  # looking for (ALT-TAB), reset this list.

def main():
    ''' make the display blink '''
    pygame.init()
    screen = pygame.display.set_mode((800,600), FULLSCREEN)
    pygame.display.set_caption('test') 
    R = 0 
    G = 0
    B = 0

    screen.fill((R,G,B)) 

    while True:
        
        input(pygame.event.get()) # handle input events.
    
        # change the colors...
        if R == 255: R = 0
        else: R += 1  

        screen.fill((R,G,B)) 
        pygame.display.flip() 

##############################################################
if __name__ == "__main__":
    main()