2014-07-24 32 views

risposta

6

Usa pygame.key.get_mods() per ottenere lo stato di tasti speciali come controllo o Maiusc.

get_mods() dà un intero ed è necessario utilizzare gli operatori bit a bit per confrontarlo con costanti come KMOD_SHIFT

vedere la documentazione: pygame.key


EDIT: esempio

import pygame 
import pygame.locals 

pygame.init() 

screen = pygame.display.set_mode((300,200)) 

running = True 

while running: 
    for event in pygame.event.get(): 
     if event.type == pygame.QUIT: 
      running = False 
     elif event.type == pygame.KEYDOWN: 
      if event.key == pygame.K_ESCAPE: 
       running = False 
      elif event.key == pygame.K_a and pygame.key.get_mods() & pygame.KMOD_SHIFT: 
       print "pressed: SHIFT + A" 

pygame.quit() 

BTW : è possibile utilizzare KMOD_LSHIFT o KMOD_RSHIFT per testare solo spostamento a sinistra o solo spostamento a destra.


EDIT:

BTW: come utilizzare get_pressed()

  • è necessario utilizzare K_LSHIFT e K_LSHIFT per controllare entrambi i turni.
  • esso stampa "premuto: SHIFT +A" ancora e ancora se si mantiene SHIFT +A premuto.

.

import pygame 
import pygame.locals 

pygame.init() 

screen = pygame.display.set_mode((300,200)) 

running = True 

while running: 

    # 
    # events 
    # 

    for event in pygame.event.get(): 
     if event.type == pygame.QUIT: 
      running = False 
     elif event.type == pygame.KEYDOWN: 
      if event.key == pygame.K_ESCAPE: 
       running = False 

    # 
    # others 
    # 

    all_keys = pygame.key.get_pressed() 

    #print 'shift:', all_keys[pygame.K_LSHIFT], all_keys[pygame.K_RSHIFT] 

    if all_keys[pygame.K_a] and (all_keys[pygame.K_LSHIFT] or all_keys[pygame.K_RSHIFT]): 
     print "pressed: SHIFT + A" 

pygame.quit() 

BTW:get_pressed() e get_mods() dare informazioni reali solo se pygame.event.get() era uso prima.


EDIT:

Come riconoscere A, CTRL + A SHIFT, + A, ALT + A, CTRL + SHI FT + A, CTRL + ALT + A, SHIFT + ALT + A,, CTRL + SHIFT + ALT + Un

import pygame 
import pygame.locals 

pygame.init() 

screen = pygame.display.set_mode((300,200)) 

running = True 

while running: 
    for event in pygame.event.get(): 
     if event.type == pygame.QUIT: 
      running = False 
     elif event.type == pygame.KEYDOWN: 
      if event.key == pygame.K_ESCAPE: 
       running = False 

      elif event.key == pygame.K_a: 

       mods = pygame.key.get_mods() 

       if mods & pygame.KMOD_CTRL and mods & pygame.KMOD_SHIFT and mods & pygame.KMOD_ALT: 
        print "pressed: CTRL+SHIFT+ALT + A" 
       elif mods & pygame.KMOD_CTRL and mods & pygame.KMOD_SHIFT: 
        print "pressed: CTRL+SHIFT + A" 
       elif mods & pygame.KMOD_CTRL and mods & pygame.KMOD_ALT: 
        print "pressed: CTRL+ALT + A" 
       elif mods & pygame.KMOD_SHIFT and mods & pygame.KMOD_ALT: 
        print "pressed: SHIFT+ALT + A" 
       elif mods & pygame.KMOD_SHIFT: 
        print "pressed: SHIFT + A" 
       elif mods & pygame.KMOD_CTRL: 
        print "pressed: CTRL + A" 
       elif mods & pygame.KMOD_ALT: 
        print "pressed: ALT + A" 
       else: 
        print "pressed: A" 


pygame.quit() 

BTW: Sul mio computer è un problema con Alt. Destro perché è utilizzato per i caratteri nativi. Non funziona con KMOD_ALT e KMOD_RALT.

+0

@ZenLogic Aggiungo un esempio funzionante su come usarlo. – furas

+0

Questo non sembra funzionare, puoi vedere cosa c'è di sbagliato qui nel mio codice? Dovrebbe suonare la versione nitida della nota, ma continua a suonare come se fosse premuto solo 'a'. 'current_played_sine = { 'a #': MakeSineWave (277,18)} elif event.key == pygame.K_a e pygame.key.get_mods() e pygame.KMOD_SHIFT: stampa current_type, 277,18 current_played_sine [ 'a # '] .play (-1) ' – ZenLogic

+0

Probabilmente hai' elif event.key == pygame.K_a: 'prima' elif event.key == pygame.K_a e pygame.key.get_mods() '- primo è vero anche per 'SHIFT + A'. Hai un ordine di modifica o fai 'se pygame.key.get_mods() ...:' dentro 'elif event.key == pygame.K_a:' Ho fatto presto un esempio. – furas

1

Se questo è per una GUI.

from Tkinter import * 

class Application(Frame): 
    def __init__(self, parent): 
     Frame.__init__(self,parent) 
     self.grid() 
     self.create_widgets() 

    def create_widgets(self): 
     widg = Text(self) 
     widg.grid(row=0,column=0) 

     self.bind_all("<Control-a>", self.check) #This checks if lower case a is pressed 
     self.bind_all("<Control-A>", self.check) #This checks if upper case a is pressed 

    def check(self, event): #Make sure to have event inside the function 
     print("Control-a pressed") 

root = Tk() 

app = Application(root) 

root.mainloop() 
+0

Oh sì, scusa, avrei dovuto dire che era per pygame, se questo fa la differenza ... – ZenLogic

+0

@ZenLogic se è per pygame non ho idea scusa. –

1

Per Pygame si dovrebbe essere alla ricerca di get_pressed invece di keydown, cuz keydown accade solo una volta, premuto il tasto accade fino al rilascio chiave.

per due tasti premuti basta fare un if-stament.

# store the result of the get_pressed() in those variables. 
if key_ctrl_is_down and key_a_is_down:  
    dowhatever()