Pygame 知道何时按下 shift+other 键

Pygame knowing when shift+other key is pressed

我正在尝试创建一个类似于 Microsoft word 的程序,只是大大简化了。我需要能够知道何时按下 shift+(任意键),并显示所按下内容的正确值(通过 shift 从普通键更改)。

目前,我最好的方法是简单地查找是否按下了 shift 键,然后每当按下另一个键时将按下的键输出的数字更改为一个新值,我可以调用 chr() 将其更改为该键的移位值。

import pygame as py
import sys

py.init()

screen = py.display.set_mode((400,400))

clock = py.time.Clock()

font = py.font.match_font('Ariel')


def display_text(font,text,size,color,x,y):
    #just a way to display the text to the screen
    fontS = py.font.Font(font,size)
    rendering = fontS.render(text,True,color)
    text_rect = rendering.get_rect()
    text_rect.topleft = (x,y)
    screen.blit(rendering,text_rect)


going = True
display = ''

while going:
    screen.fill((0,0,0))

    for event in py.event.get():
        if event.type == py.QUIT:
            py.quit()
            sys.exit()
        if event.type == py.KEYDOWN:

            #event keys from 32-126 are the ones that actually have a
            #character that you would type (ie that will prevent a SHIFT
            #from being displayed

            if event.key in range(32,127):

                pressedKeys = py.key.get_pressed()
                #find if a shift key is pressed
                if pressedKeys[py.K_RSHIFT] or pressedKeys[py.K_LSHIFT]:

                    #all below is handling for changing what the event key
                    #was to what it should be for when shift is pressed

                    if event.key in range(97,123):
                        event.key -= 32

                    if event.key in range(51,54):
                        event.key -= 16

                    if event.key == 50:
                        event.key = 64

                    if event.key == 49:
                        event.key = 33

                    if event.key == 54:
                        event.key = 94

                    if event.key == 55:
                        event.key = 38

                    if event.key == 56:
                        event.key = 42

                    if event.key == 57:
                        event.key -= 17

                display = chr(event.key)#change the number from event.key
                                        #into text

    display_text(font,display,40,(255,255,255),100,100)
    py.display.flip()
    clock.tick(60)

问题是有一堆不同的键,处理它们所需的语句数量太多,打字起来很烦人,而且太长了。每个字符,例如 .更改为 > 当按下 shift 时,所有这些都必须考虑在内。

有什么方法可以检测何时按下 shift 键并自动更新输出结果吗?有没有其他图书馆可以做到这一点?或者我是否必须继续使用当前的方法根据是否按下 shift 来更改输出?

如果您查看 'a' 和 'A' 的事件,而两者都有 key 97,则可以使用 unicode(如果严格可打印字符)或 mod 属性来区分。

<Event(2-KeyDown {'scancode': 0, 'window': None, 'key': 97, 'unicode': u'a', 'mod': 0})>

<Event(2-KeyDown {'scancode': 0, 'window': None, 'key': 97, 'unicode': u'A', 'mod': 1})>

注意:mod 对于 shiftcaps lock 字符事件是不同的,尽管 unicode 是相同的。

src = r"`1234567890-=qwertyuiop[]\asdfghjkl;\'zxcvbnm,./"
dest = r'~!@#$%^&*()_+QWERTYUIOP{}|ASDFGHJKL:"ZXCVBNM<>?'
ch = chr(event.key)
pressed = py.key.get_pressed()
if pressed[py.K_RSHIFT] or pressed[py.K_LSHIFT] and ch in src:
    ch = dest[src.index(ch)]