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

KeyCode and button clicks

Discussion in 'Immediate Mode GUI (IMGUI)' started by FeastSC2, Jun 23, 2017.

  1. FeastSC2

    FeastSC2

    Joined:
    Sep 30, 2016
    Posts:
    978
    In my EditorWindow, I would like that whenever the user clicks a GUILayout.Button I know which of the alpha keyboard keys the user pressed while clicking the button.

    I can't make it work with:
    Code (CSharp):
    1.  
    2. private void OnGUI()
    3.         {
    4.             if (GUILayout.Button("Test"))
    5.             {
    6.                 Event e = Event.current;
    7.  
    8.                 if (e.shift)
    9.                 {
    10.                     Debug.Log("This works!");
    11.                 }
    12.                 if (e.keyCode == KeyCode.Alpha1)
    13.                 {
    14.                     Debug.Log("This doesn't work, Alpha1 is not detected when triggering the button");
    15.                 }
    16.             }
    17.         }
    18.  
    19.  
     
    tigerleapgorge likes this.
  2. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,670
    OnGUI gets called separately for each UI event type, such as KeyDown and KeyUp. Event.keyCode is only valid for specific event types, such as KeyDown and KeyUp. In OnGUI, you're going to have to check the Event.current.type. If it's KeyDown, record that the key (e.g., Alpha1) is pressed down. If it's KeyUp, record that the key is up.
     
    FeastSC2 likes this.
  3. FeastSC2

    FeastSC2

    Joined:
    Sep 30, 2016
    Posts:
    978

    I tried what you suggested, it does not work when clicking on a button. Maybe I misunderstood something?
    Code (CSharp):
    1. private void OnGUI()
    2.         {
    3.             var e = Event.current;
    4.  
    5.             if (GUILayout.Button("Test")) // commenting this out displays what key has been pressed.
    6.             {
    7.                 if (e.alt)
    8.                 {
    9.                     if (e.type == EventType.KeyDown)
    10.                     {
    11.                         var k = e.keyCode;
    12.                         Debug.Log("keyCode: " + k);
    13.                     }
    14.                 }
    15.             }
     
    tigerleapgorge likes this.
  4. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,670
    OnGUI gets called separately for each UI event type, possibly multiple times per frame.
    • If the player presses a key down, Unity will call OnGUI with Event.current.type == KeyDown, and Event.current.keyCode == keycode of the pressed key.

    • If the player clicks and releases on the button, Unity will call OnGUI with Event.current.type == MouseUp. During this invocation of OnGUI, Event.current.keyCode will not be valid because it's only valid on invocations where Event.current.type is a key-related event.

    • OnGUI could be called many other times during the frame, too, such as when the player moves the mouse, or spins the scrollwheel, or when the GUI system wants to do a relayout or repaint.
    Try this script:

    Code (csharp):
    1. using UnityEngine;
    2.  
    3. public class GUIButtonTest : MonoBehaviour
    4. {
    5.  
    6.     private bool isAlpha1Down = false;
    7.  
    8.     private void OnGUI()
    9.     {
    10.         var e = Event.current;
    11.  
    12.         // Check if the event type is KeyDown or KeyUp on Alpha1:
    13.         if (e.type == EventType.KeyDown && e.keyCode == KeyCode.Alpha1)
    14.         {
    15.             isAlpha1Down = true; // Remember that Alpha1 is down.
    16.         }
    17.         else if (e.type == EventType.KeyUp && e.keyCode == KeyCode.Alpha1)
    18.         {
    19.             isAlpha1Down = false; // Remember that Alpha1 is up.
    20.         }
    21.  
    22.         // Always draw the button. It will only register a click if e.type is a mouse click.
    23.         if (GUILayout.Button("Test"))
    24.         {
    25.             Debug.Log("Button clicked. Is Alpha1 down? " + isAlpha1Down);
    26.         }
    27.     }
    28. }
     
    FeastSC2 likes this.
  5. FeastSC2

    FeastSC2

    Joined:
    Sep 30, 2016
    Posts:
    978
    Ah yes, thanks for the explanations TonyLi. Much appreciated!
     
  6. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,670
    Happy to help!