Pygame program that can inject keyboard with caps

I have a Pygame program that needs text input. The way it is done is to receive keyboard input, and when a key is pressed, it displays that key for it to be added to the screen. It essentially acts like a text box. The problem is that when you do a shift it doesn't do anything. I understand that this is because the program ignores the shift input and instead writes the text if its number is less than 128. I thought about setting a variable when the shift is pressed and then capitalizing if that is true, but the string capitalization is only a letter. not on things like numbers or semicolons. Maybe a number that I can add to the ASCII number I entered to change it if I press shift or whatever?
Edit:
Basically I just want to know if there is a number appended to the ascii characters so that it looks like they were printed with the shift key pressed. After reading my initial question, it seemed a little unclear.

+2


a source to share


5 answers


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



+3


a source


Adding this class to your code should do the trick. To get the character the user clicks, the getCharacter function from the class is called. You can change the if statement keyPress> = 32 and keyPress <= 126: to allow non-letter characters to be offset.



# 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 

      

+1


a source


It looks like subtracting 32 should get you what you want looking at the ASCII table . Make sure you are really dealing with lowercase letters though, or you will end up with some strange characters. I'm not sure if you mean that capitalization only works for letters:

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

      

0


a source


I wrote a function that converts strings after getting insufficient help. It will convert everything manually.

0


a source


I am delving into this question. Every keyboard event in pygame has not only scancode but also unicode representation. This not only allows you to enter capital letters, but also applies to multilingual keyboards with a language switcher.

Here's a 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)

      

0


a source







All Articles