Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Numpad keys input

Discussion in 'Scripting' started by Deleted User, Sep 20, 2010.

  1. Deleted User

    Deleted User

    Guest

    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
     
  2. Deleted User

    Deleted User

    Guest

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

    Thanks
     
  3. Chris-Sinclair

    Chris-Sinclair

    Joined:
    Jun 14, 2010
    Posts:
    1,326
    Bunny83 likes this.
  4. tamariamontgomery

    tamariamontgomery

    Joined:
    Jan 16, 2020
    Posts:
    2
    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();
     
    krotovar likes this.
  5. Tony-Lovell

    Tony-Lovell

    Joined:
    Jul 14, 2014
    Posts:
    127
    Are you sure this is not simply modal depending on if NUMLOCK is active?

    tone
     
  6. benjmitch

    benjmitch

    Joined:
    Nov 27, 2019
    Posts:
    17
    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.

    Code (CSharp):
    1.         if (Input.GetKeyDown(KeyCode.Keypad1))
    2.         {
    3.             Debug.Log("Keypad1");
    4.         }
    5.  
    6.         if (Input.GetKeyDown(KeyCode.End))
    7.         {
    8.             Debug.Log("End");
    9.         }
    10.  
    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.

    Code (CSharp):
    1. KeyPress event, serial 38, synthetic NO, window 0x7400001,
    2.     root 0x1e0, subw 0x0, time 5052218, (440,159), root:(1621,793),
    3.     state 0x10, keycode 87 (keysym 0xffb1, KP_1), same_screen YES,
    4.     XLookupString gives 1 bytes: (31) "1"
    5.     XmbLookupString gives 1 bytes: (31) "1"
    6.     XFilterEvent returns: False
    7.  
    8. KeyRelease event, serial 38, synthetic NO, window 0x7400001,
    9.     root 0x1e0, subw 0x0, time 5052338, (440,159), root:(1621,793),
    10.     state 0x10, keycode 87 (keysym 0xffb1, KP_1), same_screen YES,
    11.     XLookupString gives 1 bytes: (31) "1"
    12.     XFilterEvent returns: False
    13.  
    14. KeyPress event, serial 38, synthetic NO, window 0x7400001,
    15.     root 0x1e0, subw 0x0, time 5057498, (440,159), root:(1621,793),
    16.     state 0x10, keycode 77 (keysym 0xff7f, Num_Lock), same_screen YES,
    17.     XLookupString gives 0 bytes:
    18.     XmbLookupString gives 0 bytes:
    19.     XFilterEvent returns: False
    20.  
    21. KeyRelease event, serial 38, synthetic NO, window 0x7400001,
    22.     root 0x1e0, subw 0x0, time 5057594, (440,159), root:(1621,793),
    23.     state 0x10, keycode 77 (keysym 0xff7f, Num_Lock), same_screen YES,
    24.     XLookupString gives 0 bytes:
    25.     XFilterEvent returns: False
    26.  
    27. PropertyNotify event, serial 38, synthetic NO, window 0x7400001,
    28.     atom 0x16b (XKLAVIER_STATE), time 5057595, state PropertyNewValue
    29.  
    30. KeyPress event, serial 38, synthetic NO, window 0x7400001,
    31.     root 0x1e0, subw 0x0, time 5058546, (440,159), root:(1621,793),
    32.     state 0x0, keycode 87 (keysym 0xff9c, KP_End), same_screen YES,
    33.     XLookupString gives 0 bytes:
    34.     XmbLookupString gives 0 bytes:
    35.     XFilterEvent returns: False
    36.  
    37. KeyRelease event, serial 38, synthetic NO, window 0x7400001,
    38.     root 0x1e0, subw 0x0, time 5058650, (440,159), root:(1621,793),
    39.     state 0x0, keycode 87 (keysym 0xff9c, KP_End), same_screen YES,
    40.     XLookupString gives 0 bytes:
    41.     XFilterEvent returns: False
    42.  
     
    Last edited: Sep 29, 2020
  7. unity_CJOJGT1T8FJ33g

    unity_CJOJGT1T8FJ33g

    Joined:
    Aug 14, 2021
    Posts:
    22
    For me Input.GetKey("[insert whatever number]") still works. Idk maybe they fixed it.
     
  8. jacobclm1510

    jacobclm1510

    Joined:
    Jan 23, 2023
    Posts:
    1
    I hate that this is exactly correct. I didn't even know what num lock did until reading this! Thank you by the way.
     
  9. TealcWu

    TealcWu

    Joined:
    Nov 26, 2013
    Posts:
    7
    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: https://docs.unity3d.com/ScriptReference/KeyCode.html
     
  10. olteansilvan

    olteansilvan

    Joined:
    Jul 14, 2018
    Posts:
    2
    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.

    Code (CSharp):
    1. for (int i = 0; i <= 9; i++){
    2.    if (Input.GetKeyDown("["+i.ToString()+"]"))
    3.        {
    4.            //Do your thing
    5.         }
    6. }