Numpad keys input

How would I go on about using Numpad keys (the numbers) in my Input? If I try “Input.GetKeyDown(“8”)”, it only works for the number keys which are above the letters, but not the ones on the right side of the keyboard.

thanks

Nevermind, got it…
For anyone wondering, you have to add for numpad buttons, e.g.:
“Input.GetKeyDown(”[7]“)”

Thanks

8 Likes

You can also use the KeyCode enumeration:

Input.GetKeyDown(KeyCode.Keypad7)

IMO, using the KeyCode overload is superior to the string representation for the increased compile-time safety.

1 Like

Neither of these suggestions work anymore. No matter what I do, the keyCode is (in this case), ‘Home’, not ‘7’ or ‘KeyPad7’. This causes an issue if I want to differentiate between, for example, ‘keypad 2’ and ‘down arrow’, which is no longer possible. This occurs whether the number lock is on or off.

if(Input.GetKeyDown(KeyCode.Keypad7)) print(“Key pad 7”);
if(Input.GetKeyDown(“[7]”)) print(“Key pad 7”);

Compare to:
if(Input.GetKeyDown(KeyCode.Home)) print(“Key pad 7”);

Also,

if(Event.current.isKey) inputKey = Event.current.keyCode.ToString();

1 Like

Are you sure this is not simply modal depending on if NUMLOCK is active?

tone

3 Likes

I’m having the same problem - pressing the digits on the keypad produces codes e.g. KeyCode.End, not KeyCode.Keypad1, regardless the setting of NumLock. The keypad is working correctly when tested in a text editor. Unlike tamariamontgomery, I am new to Unity so cannot confirm that something has changed.

        if (Input.GetKeyDown(KeyCode.Keypad1))
        {
            Debug.Log("Keypad1");
        }

        if (Input.GetKeyDown(KeyCode.End))
        {
            Debug.Log("End");
        }

EDIT: By the way, “[1]” doesn’t work either, though I imagine that just maps to Keypad1.

EDIT: I’ve just used xev (Linux) to check the keycodes being received by the system - these are below, but the TLDR is that the keyboard sends KP_1 or KP_END, depending on the NumLock state, as expected.

KeyPress event, serial 38, synthetic NO, window 0x7400001,
    root 0x1e0, subw 0x0, time 5052218, (440,159), root:(1621,793),
    state 0x10, keycode 87 (keysym 0xffb1, KP_1), same_screen YES,
    XLookupString gives 1 bytes: (31) "1"
    XmbLookupString gives 1 bytes: (31) "1"
    XFilterEvent returns: False

KeyRelease event, serial 38, synthetic NO, window 0x7400001,
    root 0x1e0, subw 0x0, time 5052338, (440,159), root:(1621,793),
    state 0x10, keycode 87 (keysym 0xffb1, KP_1), same_screen YES,
    XLookupString gives 1 bytes: (31) "1"
    XFilterEvent returns: False

KeyPress event, serial 38, synthetic NO, window 0x7400001,
    root 0x1e0, subw 0x0, time 5057498, (440,159), root:(1621,793),
    state 0x10, keycode 77 (keysym 0xff7f, Num_Lock), same_screen YES,
    XLookupString gives 0 bytes:
    XmbLookupString gives 0 bytes:
    XFilterEvent returns: False

KeyRelease event, serial 38, synthetic NO, window 0x7400001,
    root 0x1e0, subw 0x0, time 5057594, (440,159), root:(1621,793),
    state 0x10, keycode 77 (keysym 0xff7f, Num_Lock), same_screen YES,
    XLookupString gives 0 bytes:
    XFilterEvent returns: False

PropertyNotify event, serial 38, synthetic NO, window 0x7400001,
    atom 0x16b (XKLAVIER_STATE), time 5057595, state PropertyNewValue

KeyPress event, serial 38, synthetic NO, window 0x7400001,
    root 0x1e0, subw 0x0, time 5058546, (440,159), root:(1621,793),
    state 0x0, keycode 87 (keysym 0xff9c, KP_End), same_screen YES,
    XLookupString gives 0 bytes:
    XmbLookupString gives 0 bytes:
    XFilterEvent returns: False

KeyRelease event, serial 38, synthetic NO, window 0x7400001,
    root 0x1e0, subw 0x0, time 5058650, (440,159), root:(1621,793),
    state 0x0, keycode 87 (keysym 0xff9c, KP_End), same_screen YES,
    XLookupString gives 0 bytes:
    XFilterEvent returns: False

For me Input.GetKey(“[insert whatever number]”) still works. Idk maybe they fixed it.

I hate that this is exactly correct. I didn’t even know what num lock did until reading this! Thank you by the way.

Sumup as below, take 7 as example:

  1. Per 7 on numpad, Input.GetKeyDown(KeyCode.Keypad7) = Input.GetKeyDown(“[7]”)
  2. Per number key 7 on the top of the alphanumeric keyboard: Input.GetKeyDown(KeyCode.Alpha7)

Reference: Unity - Scripting API: KeyCode

Guys, you might think this is really dumb, but for me only this worked.
I will add : my laptop has no numpad. Virtual keyboard has no numpad. Using external keyboard and for it to be detected i had to turn off numpad by numlock.

for (int i = 0; i <= 9; i++){
   if (Input.GetKeyDown("["+i.ToString()+"]"))
       {
           //Do your thing
        }
}