简体   繁体   English

Pygame 知道何时按下 shift+other 键

[英]Pygame knowing when shift+other key is pressed

I am trying to create a program similar to Microsoft word, only heavily simplified.我正在尝试创建一个类似于 Microsoft Word 的程序,只是大大简化了。 I need to be able to know when shift+(any key) is pressed, and display the proper value for what was pressed (changed from just the normal key by the shift).我需要能够知道何时按下 shift+(任意键),并显示按下的正确值(通过 shift 从普通键更改)。

Currently, my best method is simply to find if a shift key is pressed, then whenever another key is pressed altering the number output from the pressed key to a new value that I can call chr() to change it into the shifted value for that key.目前,我最好的方法是简单地查找是否按下了 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)

The problem is that there are a bunch of different keys, and the number of statements needed to handle them all would be annoying to type out and just too long.问题是有一堆不同的键,并且处理它们所需的语句数量会很烦人,而且太长了。 Every character, such as .每个字符,例如 . changing to > when shift is pressed all have to be accounted for.更改为 > 当按下 shift 时,所有都必须考虑在内。

Is there any way to detect when the shift key is pressed and automatically update what the output would be then?有什么方法可以检测何时按下 shift 键并自动更新输出结果? Is there another library that can do that?有没有其他图书馆可以做到这一点? Or will I have to continue with my current method of changing the output depending on if shift was pressed?或者我是否必须继续使用当前更改输出的方法,具体取决于是否按下了 shift?

If you look at the events for 'a' and 'A' while both have the key 97, you can use either the unicode (if strictly printable characters) or mod attribute to tell the difference.如果您查看 '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})>

Note: the mod is different for shift and caps lock characters event though the unicode will be the same.注意:虽然unicode是相同的,但shiftcaps lock字符事件的mod是不同的。

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)]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM