Search Unity

Find out which key was pressed.

Discussion in 'Scripting' started by Compressed, Feb 10, 2016.

  1. Compressed

    Compressed

    Joined:
    Sep 20, 2014
    Posts:
    59
    Hello, I am trying to find out which key the player pressed.
    So something like Input.inputString, except for all keys, not just for letters and numbers. So for example if the player presses F1 i want the key number, or some unique hash or something.

    And i'm NOT trying to find out if he pressed some specific key like R or something, i want to get the number (or something) of any key he pressed.

    Thanks
     
  2. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,457
  3. Compressed

    Compressed

    Joined:
    Sep 20, 2014
    Posts:
    59
    Damn, that is pretty unfortunate. I tried it and the OnGui gets executed twice for every update and not only that, in half of them the keycode is returned as None even though i am holding the key down. Luckily i can work easily around that, but still it is pretty hacky and i can't believe they don't give us Input.keyCode
     
    technofeeliak likes this.
  4. roojerry

    roojerry

    Joined:
    Mar 18, 2013
    Posts:
    68
    Not efficient code, but, this will get you the current KeyCode pressed

    Code (CSharp):
    1. foreach(KeyCode kcode in Enum.GetValues(typeof(KeyCode)))
    2. {
    3.     if (Input.GetKey(kcode))
    4.         Debug.Log("KeyCode down: " + kcode);
    5. }
    EDIT: by "not efficient", I mean this should not be run within the game loop to detect input. There are 321 KeyCodes to loop through. If you were maybe doing user control remapping in an options menu, this could be useable. If we knew your use case, there may be a more elegant solution to what you are trying to do
     
    Last edited: Feb 10, 2016
    Ryiah, Maloke and Wolfram like this.
  5. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,457
    Yeah I'd definitely not go that route.

    You'll probably have to combine it with checks to make sure the current event is actually a keyboard event
    Code (csharp):
    1.  
    2. void OnGUI()
    3. {
    4.     if (Event.current.isKey && Event.current.type == EventType.KeyDown)
    5.     {
    6.         Debug.Log(Event.current.keyCode);
    7.     }
    8. }
    9.  
     
  6. guavaman

    guavaman

    Joined:
    Nov 20, 2009
    Posts:
    3,803
    Actually, if you do some testing, looping through all the keycodes is far more efficient than the OnGUI route. Just having an OnGUI call in your scripts will cause memory allocations every frame which will eventually need to be collected by the garbage collector causing periodic slowdowns. Checking 321 keycodes takes next to no time in comparison.

    But do not get your keycode list every update. That will generate allocations as well. Call Enum.GetValues(typeof(KeyCode)) once on start and store those values for the duratiton.
     
    Ryiah, Pacosmico and stonstad like this.
  7. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,457
    Except there are keycodes that can map to the same key so you have the potential for multiple "true" scenarios. And you could pick up mouse/joystick events.

    I doubt this very much. How did you prove this and what's your definition of "far more efficient"? :)
     
  8. guavaman

    guavaman

    Joined:
    Nov 20, 2009
    Posts:
    3,803
    Simply filter them out with your own keycode list that excludes these.

    From another post of mine from last year:

    Just for fun I profiled it:

    Code (csharp):
    1. // Keyboard polling: 9.504352E-06 s / frame
    2. // (as timed, avg of 1000 frames, profiler reports 0.00 ms)
    3.  
    4. private int[] values;
    5. private bool[] keys;
    6.  
    7. void Awake() {
    8.     values = (int[])System.Enum.GetValues(typeof(KeyCode));
    9.     keys = new bool[values.Length];
    10. }
    11.  
    12. void Update() {
    13.     for(int i = 0; i < values.Length; i++) {
    14.         keys[i] = Input.GetKey((KeyCode)values[i]);
    15.     }
    16. }
    Code (csharp):
    1. // OnGUI: 0.02 ms (as reported by profiler), 336 bytes GC / frame
    2. void OnGUI() {
    3.     // there is no code in here
    4. }
    Keyboard polling is more efficient.
     
  9. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,457
    Interesting - and certainly a concern if you're not targeting desktop platforms.
     
  10. daneshvar1998

    daneshvar1998

    Joined:
    May 10, 2018
    Posts:
    1
    if you want to know what character key is pressed
    you can go for a textinput
    an invisible textinput that detects wich key you pressed
    you simply add a on value changed mathod
    then you read whats in textfield and empty it again
    but you should make sure that you keep the input text selected and focused
     
  11. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    6,528
    Please don't necropost a 3-year-old thread just to post a solution that's significantly inferior to @guavaman 's solution that was posted up above.
     
    Ryiah likes this.
  12. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,457
    Literally the worst possible solution to that problem.....
     
    AlexanderTeaH and Ryiah like this.
  13. Bidule200

    Bidule200

    Joined:
    Apr 26, 2014
    Posts:
    12
    Far from ideal, but a very clever dirty hack indeed ^^