我有一些pyday代码发生了keydown事件,我基本上想知道是否有可能一次按下两个键,比如ctrl + a或类似的东西。这是可能的,还是我必须找到解决方法?
答案 0 :(得分:11)
使用pygame.key.get_mods()获取特殊键的状态,例如 Control 或 Shift 。
get_mods()
给出一个整数,您必须使用按位运算符将其与KMOD_SHIFT
等常量进行比较
请参阅文档:pygame.key
编辑:示例
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:您可以使用KMOD_LSHIFT
或KMOD_RSHIFT
仅测试左移或仅右移。
编辑:
BTW:示例如何使用get_pressed()
K_LSHIFT
和K_LSHIFT
来检查两个班次。
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()
和get_mods()
仅在以前使用pygame.event.get()
时提供实际信息。
修改强>
如何识别 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:在我的电脑上出现 Right Alt 的问题,因为它用于本地字符。它不适用于KMOD_ALT
和KMOD_RALT
。
答案 1 :(得分:1)
如果这是用于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()
答案 2 :(得分:1)
对于Pygame,你应该寻找get_pressed而不是keydown,cuz keydown只发生一次,按键发生直到key被释放。
按下两个按键只需要做一个if-stament。# store the result of the get_pressed() in those variables.
if key_ctrl_is_down and key_a_is_down:
dowhatever()