Python keydown combinations (ctrl + key or shift + key)

こ雲淡風輕ζ 提交于 2019-12-08 16:24:34

问题


I have some python code with keydown events happening, I am basically wondering if it is possible to have two keys pressed at a time, like ctrl+a or something like that. Is this possible, or will I have to find a workaround?


回答1:


Use pygame.key.get_mods() to get state of special keys like Control or Shift.

get_mods() gives one integer and you have to use bitwise operators to compare it with constants like KMOD_SHIFT

See documentation: pygame.key


EDIT: example

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: you can use KMOD_LSHIFT or KMOD_RSHIFT to test only left shift or only right shift.


EDIT:

BTW: example how to use get_pressed()

  • you have to use K_LSHIFT and K_LSHIFT to check both shifts.
  • it print "pressed: SHIFT + A" again and again if you keep SHIFT+A pressed.

.

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() and get_mods() give actual information only if pygame.event.get() was use before.


EDIT:

How to recognize A, CTRL+A, SHIFT+A, ALT+A, CTRL+SHIFT+A, CTRL+ALT+A, SHIFT+ALT+A, , CTRL+SHIFT+ALT+A

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: On my computer is problem with Right Alt because it is used for native chars. It doesn't work with KMOD_ALT and KMOD_RALT.




回答2:


If this is for a 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()



回答3:


For Pygame you should be looking for get_pressed instead of keydown, cuz keydown happens only once, key down happens until key is released.

for two keys pressed just do a if-stament.

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


来源:https://stackoverflow.com/questions/24923078/python-keydown-combinations-ctrl-key-or-shift-key

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!