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 relize 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.
views:
223answers:
4
A:
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'
jrdioko
2010-04-20 01:02:26
In your example only the abcd characters are changed. That's what I meant.
None
2010-04-20 01:15:03
I tried this and it worked for letters, but it is not what I wanted, for it came up with weird characters when anything else was typed. I want a conversion that acts like the shift key when it changes letters. Is there any way to get this?
None
2010-04-20 02:04:50
For example, here's some pseudo-code:if key <= 127: if not shift: text += chr(key) else: text += chr(shift(key))
None
2010-04-20 03:18:06
I'm still not sure I understand. If you want something that capitalizes only letters, why doesn't upper() work? What would the string in the example I gave need to be converted to in your scenario?
jrdioko
2010-04-20 06:02:33
I don't want something that only capitalizes letters. I want something that acts like the shift key.
None
2010-04-20 21:13:30
The conversion I want would change the string to "!@##:123ABCD"
None
2010-04-20 22:42:57
sorry, there was a typo. "1234;!@#abcd" should be converted to "!@#$:123ABCD"
None
2010-04-21 14:19:12
Ah now I understand. I'm not sure how you could do that, since it's dependent on keyboard layout and mapping.
jrdioko
2010-04-21 18:06:30
OK. thanks. I figured it out and wrote my own function.
None
2010-04-21 23:27:57
A:
I wrote a function that converts the strings after getting not enough help. It converts everything manually.
None
2010-04-21 14:28:40
I don't need this function, as I now know about the unicode attribute of the pygame Event class.
None
2010-04-22 23:21:17
A:
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
apocolyp4
2010-04-27 23:59:44
A:
I can use the 'event.unicode' attribute to get the value of the key typed.
None
2010-05-07 02:11:38