Pygame program that can get keyboard input with caps [duplicate]

最后都变了- 提交于 2019-12-23 20:00:26

问题


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.


回答1:


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




回答2:


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 



回答3:


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)



回答4:


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'



回答5:


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



来源:https://stackoverflow.com/questions/2671589/pygame-program-that-can-get-keyboard-input-with-caps

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