问题
I am trying to create a program similar to Microsoft word, only heavily simplified. 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).
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.
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.
Is there any way to detect when the shift key is pressed and automatically update what the output would be then? 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?
回答1:
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.
<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.
回答2:
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)]
来源:https://stackoverflow.com/questions/57529603/pygame-knowing-when-shiftother-key-is-pressed