Kyle Burns
Kyle Burns

Reputation: 167

creating a simple key press checker in python using pygame

I'm making a messenger application in python using pygame for the gui. I'm trying to detect key presses but the code I'm using is a large amount and i don't like it, i was wondering if there is a better way of checking which keys where pressed then sending that to a string variable.

My code:

##------>>>__ 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 = ";"

Upvotes: 0

Views: 256

Answers (1)

sloth
sloth

Reputation: 101072

For most cases, you can simply use the unicode attribute of the KEYDOWN event:

self.CharKeyPressed = event.unicode

Upvotes: 1

Related Questions