1

I need to enter numbers through a numeric usb keyboard into a box in a pygame script. If I use the normal keyboard I can enter the numbers in upper zone of the keyboard. If I try to use the numpad I get letters like "à" for number 1. If I try to use only a numpad (Lindy) I can't get anything. My script, actually, is getting keys with this:

def get_key():
  while 1:
    event = pygame.event.poll()
    if event.type == KEYDOWN:
      return event.key

    else:
      pass

Where am I wrong, why can't get numbers from numpad? Thanks for the help

user3072083
  • 85
  • 1
  • 3
  • 12
  • Did you read [the docs](http://www.pygame.org/docs/ref/key.html)? try: `if event.key == pygame.key.K_KP0` – ThinkChaos Dec 29 '13 at 18:28
  • i have a similar problem, could you help me please? https://stackoverflow.com/questions/63464407/linux-phyton-numpad-key-cant-be-assigned-to-program – Valac Aug 19 '20 at 08:15
  • I have a similar problem, could u help me please? https://stackoverflow.com/questions/63464407/linux-phyton-numpad-key-cant-be-assigned-to-program – Valac Aug 19 '20 at 08:47

1 Answers1

3

The Pygame KEYDOWN event stores the alphanumeric zone numbers in the range 48 - 57 (0x30 - 0x39), as it should. However, numpad presses are reported in the range 256 - 265 (0x100 - 0x109), regardless of whether numlock is toggled.

The reason the printout of the value of Numpad 1 is displayed as "à" is because the key value for Numpad 1 is 257, which in Unicode is à (U+0101).

Here is a quick snippet that should convert numpad presses to their ASCII key value equivalent (does not convert other numpad keys, like Enter, +, or Delete, which also have different values than their equivalents elsewhere on the keyboard):

if (event.key >= 0x100 and event.key <= 0x109):
  return event.key - 0xD0
else:
  return event.key
  • Sorry but it doesn't work. I added your lines to my code and now when I digit numbers nothing happens. Yesterday I added in the while code, for each number, the condition: if inkey == K_KP(1-0): string.append("1-0"). this worked on the full keyboard but with numeric pad of Lindy, nothing happens. – user3072083 Dec 30 '13 at 13:18
  • Try running [this example program](http://pastebin.com/3Haxp9UV) and let me know what values you get in the console when Keyboard 1 and Numpad 1 are pressed. – Kevan Robinson Dec 30 '13 at 21:20
  • When I press 1 on full keyboard I get: Key received: 49 'Translated' value: 49. When I press 1 on numpad I get: Key received: 300 'Translated' value: 300 Key received: 257 'Translated' value: 49. For numpad "2": Key received: 300 'Translated' value: 300 Key received: 258 'Translated' value: 50. I tried all the 10 numbers but I do not put here now. I would only show the "enter" numpad: Key received: 300 'Translated' value: 300 Key received: 271 'Translated' value: 271 – – user3072083 Dec 31 '13 at 11:22
  • The keycode '300' is the constant for activated Num Lock, which I assume Lindy sends every time a numkey is pressed. Try this re-write of your def_key() function and let me know if it works: http://pastebin.com/Y0xwrc4L – Kevan Robinson Jan 01 '14 at 01:17
  • It worked! therefore I use the numpad to enter time (hh and mm) in a clock alarm. As I put the hours (es. 09) I need to confirm with enter and then pass to the minutes. Now, I was able to put the hours but the enter didn't worked. Any idea? should I extend the 0x109? thank a lot! – user3072083 Jan 01 '14 at 10:09
  • Try modifying your function with [this snippet](http://pastebin.com/7pbyvH8j). There's an `elif` section to convert the numpad 'enter' to the standard 'enter.' It's a bit of a kludge, but should work. If you wanted this to be more elegant, you could use a `set` for mapping numpad keys to equivalents. – Kevan Robinson Jan 01 '14 at 13:21