可以获取带大写字母的键盘输入的 Pygame 程序

发布于 2024-08-29 10:49:10 字数 372 浏览 3 评论 0原文

我有一个需要文本输入的 Pygame 程序。它执行此操作的方式是获取键盘输入,当按下某个键时,它会呈现该键,以便将其添加到屏幕上。本质上它的作用就像一个文本字段。问题是,当你按住shift时它不会做任何事情。我意识到这是因为程序会忽略 Shift 输入,而是在数字低于 128 时写入文本。我曾想过在按下 Shift 时设置一个变量,然后将其大写(如果为真),但字符串大写仅适用于字母,而不是事物例如数字或分号。是否可以在按下 Shift 或其他操作时将一个数字添加到键入的 ASCII 数字中以对其进行修改?
编辑:
本质上,我只是想知道是否有一个数字可以添加到 ascii 字符中,以使它们看起来像是在按住 Shift 的情况下键入的。读完我原来的问题后,它似乎有点晦涩难懂。

I have a Pygame program that needs text input. The way it does this is to get keyboard input and when a key is pressed it renders that key so it is added to the screen. Essentially it acts like a text field. The problem is, when you hold shift it doesn't do anything. I realize this is because the program ignores shift input and instead writes the text if it's number is under 128. I have thought of setting a variable when shift is pressed then capitalizing if it was true, but string capitalization only woks on letters, not things like numbers or semicolons. Is there maybe a number I can add to the ASCII number typed to modify it if shift is pressed, or something else?
Edit:
Essentially, I just want to know if there is a number to add to ascii characters to make it seem like they were typed with shift held down. After reading over my original question it seemed slightly obscure.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

望她远 2024-09-05 10:49:10

我可以使用“event.unicode”属性来获取键入的键的值。

I can use the 'event.unicode' attribute to get the value of the key typed.

甜味拾荒者 2024-09-05 10:49:10

将此类添加到您的代码中应该可以解决问题。要获取用户按下的字符,请调用类中的 getCharacter 函数。您可以更改 if keyPress >= 32 和 keyPress <= 126: 语句以允许非字母字符与 shift 一起使用。

# The pygame module itself...
import pygame

class controls:

    def getKeyPress(self):
      for event in pygame.event.get():
         if event.type == KEYDOWN:    
             return event.key
         else:
             return False


    def getCharacter(self):

      # Check to see if the player has inputed a command
      keyinput = pygame.key.get_pressed()  

      character = "NULL"

      # Get all "Events" that have occurred.
      pygame.event.pump()
      keyPress = self.getKeyPress()

      #If the user presses a key on the keyboard then get the character
      if keyPress >= 32 and keyPress <= 126:
      #If the user presses the shift key while pressing another character then capitalise it
          if keyinput[K_LSHIFT]: 
              keyPress -= 32

          character = chr(keyPress)

      return character 

Adding this class to your code should do the trick. To get the character the user presses call the getCharacter function from the class. You can alter the if keyPress >= 32 and keyPress <= 126: statement to allow non letter characters to work with shift.

# The pygame module itself...
import pygame

class controls:

    def getKeyPress(self):
      for event in pygame.event.get():
         if event.type == KEYDOWN:    
             return event.key
         else:
             return False


    def getCharacter(self):

      # Check to see if the player has inputed a command
      keyinput = pygame.key.get_pressed()  

      character = "NULL"

      # Get all "Events" that have occurred.
      pygame.event.pump()
      keyPress = self.getKeyPress()

      #If the user presses a key on the keyboard then get the character
      if keyPress >= 32 and keyPress <= 126:
      #If the user presses the shift key while pressing another character then capitalise it
          if keyinput[K_LSHIFT]: 
              keyPress -= 32

          character = chr(keyPress)

      return character 
绝情姑娘 2024-09-05 10:49:10

我深入探讨这个问题。 pygame 中的每个键盘事件不仅有扫描码,还有 unicode 表示。它不仅允许输入大写字母,而且还支持带有语言切换的多语言键盘。

这是 pygame 的简单“输入/打印”示例:

#!/usr/bin/python

import pygame

pygame.init()
screen_size=(800,60)
disp=pygame.display.set_mode(screen_size, pygame.DOUBLEBUF)
msg=u""
clock=pygame.time.Clock()
default_font=pygame.font.get_default_font()
font=pygame.font.SysFont(default_font,16)

disp.fill((240,240,240,255))
pygame.display.flip()
while(not pygame.event.pump()):
    for event in pygame.event.get():
        print event
        if event.type == pygame.QUIT:
            pygame.quit()
            break
        if event.type == pygame.KEYDOWN:
            msg+=event.unicode
            disp.fill((240,240,240,255))
            disp.blit(font.render(msg,True,(30,30,30,255)),(0,0))
            pygame.display.flip()
    clock.tick(24)

I dig in this question. Every keyboard event in pygame has not only scancode, but the unicode representation. Which not only allow input of capital letters, but also respects multilingual keyboards with language switch.

Here simple 'input/print' example for pygame:

#!/usr/bin/python

import pygame

pygame.init()
screen_size=(800,60)
disp=pygame.display.set_mode(screen_size, pygame.DOUBLEBUF)
msg=u""
clock=pygame.time.Clock()
default_font=pygame.font.get_default_font()
font=pygame.font.SysFont(default_font,16)

disp.fill((240,240,240,255))
pygame.display.flip()
while(not pygame.event.pump()):
    for event in pygame.event.get():
        print event
        if event.type == pygame.QUIT:
            pygame.quit()
            break
        if event.type == pygame.KEYDOWN:
            msg+=event.unicode
            disp.fill((240,240,240,255))
            disp.blit(font.render(msg,True,(30,30,30,255)),(0,0))
            pygame.display.flip()
    clock.tick(24)
浪漫人生路 2024-09-05 10:49:10

查看 ASCII 表,看起来减去 32 应该可以得到你想要的结果。确保你首先处理的是小写字母,否则你会得到一些奇怪的字符。我不确定你的意思是大写仅适用于字母:

>>> '1234;!@#abcd'.upper()
'1234;!@#ABCD'

It looks like subtracting 32 should get you what you want, looking at the ASCII table. Make sure you're really dealing with a lowercase letter first though, or you'll get some weird characters. I'm not sure what you mean that capitalization works only on letters:

>>> '1234;!@#abcd'.upper()
'1234;!@#ABCD'
柒七 2024-09-05 10:49:10

我编写了一个函数,在没有得到足够的帮助后转换字符串。它手动转换所有内容。

I wrote a function that converts the strings after getting not enough help. It converts everything manually.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文