I bet you can’t type an A!

by AngryAnt on September 7th, 2009

Keys got special meaning and shouldn’t be fed into parts of the GUI? This handy little snippet takes care of that job:

private string text = "I bet you can't type an A!"; 

static void DisableKeys( KeyCode[] keys )
{
    if( !Event.current.isKey )
    {
        return;
    }

    foreach( KeyCode key in keys )
    {
        if( Event.current.keyCode == key )
        {
            Event.current.Use();
        }
    }
}

static void DisableKey( KeyCode key )
{
    DisableKeys( new KeyCode[]{ key } );
}

void OnGUI()
{
    DisableKey( KeyCode.A );
    text = GUILayout.TextArea( text );
}
1 Comment
  1. I tried this code but seems not works in Unity2.6.1 , still can type ‘A’ there.

Leave a Reply

Note: XHTML is allowed. Your email address will never be published.

Subscribe to this comment feed via RSS

Spam protection by WP Captcha-Free