Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

GUI.TextField focus kills Input.GetKeyDown on Mac but not on PC

Discussion in 'Editor & General Support' started by bem13, Nov 10, 2010.

  1. bem13

    bem13

    Joined:
    Jul 1, 2010
    Posts:
    64
    Hi,
    The code below behaves differently on a Mac than on a PC. I think this might be since Unity3.

    The problem is that on a Mac, I do not get Input.anyKeyDown or Input.GetKeyDown once the Textfield has focus.

    The reason I want to do this is so that I can detect if the enter or arrow keys are pressed while a chat window is open and automatically send or close the window, and return to navigation mode.

    I had some look at events. I think I could detect a return this way but not arrows.

    Is there another way to do this and is this a bug that I should report?

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class KeyFocusTest : MonoBehaviour {
    6.  
    7.     // Use this for initialization
    8.     public string myText = "Set focus here";
    9.  
    10.     void OnGUI()
    11.     {
    12.         myText = GUI.TextField( new Rect( 0,0, 300, 300 ), myText );
    13.  
    14.     }
    15.    
    16.    
    17.     // Update is called once per frame
    18.     void Update ()
    19.     {
    20.         if (Input.anyKeyDown)
    21.         {
    22.             Debug.Log("anyKeyDown!");
    23.         }
    24.  
    25.         if (Input.GetKeyDown(KeyCode.Return))
    26.         {
    27.             Debug.Log("GetKeyDown:Return!");
    28.         }
    29.  
    30.         if (Input.GetKeyDown(KeyCode.LeftArrow))
    31.         {
    32.             Debug.Log("GetKeyDown:LeftArrow!");
    33.         }
    34.    
    35.     }
    36. }
     
  2. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    There have been other similar issues with MonoDevelop but it appears this particular problem doesn't have an entry in the bug database. Please can you file a bug report for it (menu: Help > Report A Bug).
     
  3. CrystalVisions

    CrystalVisions

    Joined:
    Sep 11, 2007
    Posts:
    61
    I am having the same problem. Is there a workaround for this?
     
  4. Thunder0ne

    Thunder0ne

    Joined:
    Mar 8, 2009
    Posts:
    23
    Same issue for me with Unity 3.1:
    TextFields on focus override the feed of the input from the keyboard.
    Actually I am trying only with Mac OSX, currently I do not know about this behaviour under Windows.
    Thanks.
     
  5. NicholasFrancis

    NicholasFrancis

    Joined:
    Apr 8, 2005
    Posts:
    1,587
    In 3.4, Unity will always eat the keypress if focus is textfield. To check for a specific keypress, use

    Code (csharp):
    1.  
    2. if (Event.current.type == EventType.KeyDown  Event.current.character == '\n')
    3.    DoWhatEverNewLineShouldDo ();
    4.  
    Inside your OnGUI
     
    Last edited by a moderator: May 24, 2011
  6. ztan

    ztan

    Joined:
    Feb 28, 2013
    Posts:
    2
    Is there a way to use this to check if the ESC key has been pressed?
     
  7. ztan

    ztan

    Joined:
    Feb 28, 2013
    Posts:
    2
    okay nm.. i found the answer: Event.current.keyCode
     
  8. carmine

    carmine

    Joined:
    Jan 4, 2012
    Posts:
    394
    Has anyone had this problem with Linux. I have a Linux user telling me textfields are not eating keypress. (when he is using the in game chat, he's still running and things)