Pygame知道何时按下Shift +其他键 [英] Pygame knowing when shift+other key is pressed
问题描述
我正在尝试创建一个类似于Microsoft word的程序,只是大大简化了程序.我需要能够知道何时按下Shift +(任何键),并显示所按下内容的正确值(通过移位从普通键更改).
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).
当前,我最好的方法是简单地查找是否按下了Shift键,然后每当按下另一个键时,会将按下的键输出的数字更改为一个新值,我可以调用chr()将其更改为shifted该键的值.
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.
有什么方法可以检测何时按下Shift键并自动更新输出内容吗?是否有另一个图书馆可以做到这一点?还是我必须继续使用当前的更改输出的方法,具体取决于是否按了shift键?
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?
推荐答案
如果您同时查看'a'和'A'的事件,而它们都具有key
97,则可以使用unicode
(如果严格可打印的字符)或mod
属性来区别.
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})>
注意:mod
和caps lock
字符事件的mod
不同,尽管unicode
相同.
Note: the mod
is different for shift
and caps lock
characters event though the unicode
will be the same.
这篇关于Pygame知道何时按下Shift +其他键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!