我正在使用pygame为gui在python中创建一个messenger应用程序。我试图检测按键,但我使用的代码很多,我不喜欢它,我想知道是否有更好的方法来检查哪些键按下然后发送那是一个字符串变量。
我的代码:
##------>>>__ Letters __<<<------##
if event.key == pygame.K_q:
self.CharKeyPressed = "q"
if event.key == pygame.K_w:
self.CharKeyPressed = "w"
if event.key == pygame.K_e:
self.CharKeyPressed = "e"
if event.key == pygame.K_r:
self.CharKeyPressed = "r"
if event.key == pygame.K_t:
self.CharKeyPressed = "t"
if event.key == pygame.K_y:
self.CharKeyPressed = "y"
if event.key == pygame.K_u:
self.CharKeyPressed = "u"
if event.key == pygame.K_i:
self.CharKeyPressed = "i"
if event.key == pygame.K_o:
self.CharKeyPressed = "o"
if event.key == pygame.K_p:
self.CharKeyPressed = "p"
if event.key == pygame.K_a:
self.CharKeyPressed = "a"
if event.key == pygame.K_s:
self.CharKeyPressed = "s"
if event.key == pygame.K_d:
self.CharKeyPressed = "d"
if event.key == pygame.K_f:
self.CharKeyPressed = "f"
if event.key == pygame.K_g:
self.CharKeyPressed = "g"
if event.key == pygame.K_h:
self.CharKeyPressed = "h"
if event.key == pygame.K_j:
self.CharKeyPressed = "j"
if event.key == pygame.K_k:
self.CharKeyPressed = "k"
if event.key == pygame.K_l:
self.CharKeyPressed = "l"
if event.key == pygame.K_z:
self.CharKeyPressed = "z"
if event.key == pygame.K_x:
self.CharKeyPressed = "x"
if event.key == pygame.K_c:
self.CharKeyPressed = "c"
if event.key == pygame.K_v:
self.CharKeyPressed = "v"
if event.key == pygame.K_b:
self.CharKeyPressed = "b"
if event.key == pygame.K_n:
self.CharKeyPressed = "n"
if event.key == pygame.K_m:
self.CharKeyPressed = "m"
##------>>>__ Numbers __<<<------##
if event.key == pygame.K_1:
if self.BlnShift:
self.CharKeyPressed = "!"
else:
self.CharKeyPressed = "1"
if event.key == pygame.K_2:
if self.BlnShift:
self.CharKeyPressed = '"'
else:
self.CharKeyPressed = "2"
if event.key == pygame.K_3:
if self.BlnShift:
self.CharKeyPressed = "£"
else:
self.CharKeyPressed = "3"
if event.key == pygame.K_4:
if self.BlnShift:
self.CharKeyPressed = "$"
else:
self.CharKeyPressed = "4"
if event.key == pygame.K_5:
if self.BlnShift:
self.CharKeyPressed = "%"
else:
self.CharKeyPressed = "5"
if event.key == pygame.K_6:
if self.BlnShift:
self.CharKeyPressed = "^"
else:
self.CharKeyPressed = "6"
if event.key == pygame.K_7:
if self.BlnShift:
self.CharKeyPressed = "&"
else:
self.CharKeyPressed = "7"
if event.key == pygame.K_8:
if self.BlnShift:
self.CharKeyPressed = "*"
else:
self.CharKeyPressed = "8"
if event.key == pygame.K_9:
if self.BlnShift:
self.CharKeyPressed = "("
else:
self.CharKeyPressed = "9"
if event.key == pygame.K_0:
if self.BlnShift:
self.CharKeyPressed = ")"
else:
self.CharKeyPressed = "0"
##------>>>__ Specials __<<<------##
if event.key == pygame.K_SPACE:
self.CharKeyPressed = " "
if event.key == pygame.K_PERIOD:
if self.BlnShift:
self.CharKeyPressed = "@"
else:
self.CharKeyPressed = ". "
if event.key == pygame.K_COMMA:
if self.BlnShift:
self.CharKeyPressed = "'"
else:
self.CharKeyPressed = ", "
if event.key == pygame.K_SLASH:
self.CharKeyPressed = "/"
if event.key == pygame.K_SEMICOLON:
if self.BlnShift:
self.CharKeyPressed = ":"
else:
self.CharKeyPressed = ";"
答案 0 :(得分:1)
对于大多数情况,您只需使用unicode
事件的KEYDOWN
属性:
self.CharKeyPressed = event.unicode