Search Unity

how to disable a keyboard key

Discussion in 'Immediate Mode GUI (IMGUI)' started by davsed, Jun 16, 2009.

  1. davsed

    davsed

    Joined:
    May 4, 2009
    Posts:
    4
    hi everybody,

    can you explain or give me the script whitch can disable keyboard key?

    thank you for your response.
     
  2. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    you can not disable a key but you can just not react to it beeing pressed
     
  3. AngryAnt

    AngryAnt

    Keyboard Operator

    Joined:
    Oct 25, 2005
    Posts:
    3,045
    You could do something like this:
    Code (csharp):
    1.     private string text = "I bet you can't type an A!";
    2.  
    3.     void DisableKey( KeyCode key )
    4.     {
    5.         if( Event.current.keyCode == key  ( Event.current.type == EventType.KeyUp || Event.current.type == EventType.KeyDown ) )
    6.         {
    7.             Event.current.Use();
    8.         }
    9.     }
    10.  
    11.     void OnGUI()
    12.     {
    13.         DisableKey( KeyCode.A );
    14.         text = GUILayout.TextArea( text );
    15.     }
     
  4. hai_ok

    hai_ok

    Joined:
    Jun 20, 2007
    Posts:
    193
    can we do a javascript version of this?
    I'm working on it, but don't know much outside of javascript. :oops:
     
  5. AngryAnt

    AngryAnt

    Keyboard Operator

    Joined:
    Oct 25, 2005
    Posts:
    3,045
    Code (csharp):
    1.    private var text = "I bet you can't type an A!";
    2.  
    3.    function DisableKey( key : KeyCode )
    4.    {
    5.       if( Event.current.keyCode == key  ( Event.current.type == EventType.KeyUp || Event.current.type == EventType.KeyDown ) )
    6.       {
    7.          Event.current.Use();
    8.       }
    9.    }
    10.  
    11.    function OnGUI()
    12.    {
    13.       DisableKey( KeyCode.A );
    14.       text = GUILayout.TextArea( text );
    15.    }
    I also did a modified version for a blog post here (C# though): http://angryant.com/2009/09/07/i-bet-you-cant-type-an-a/
     
    Last edited: Oct 25, 2012
  6. hai_ok

    hai_ok

    Joined:
    Jun 20, 2007
    Posts:
    193
    thanks man, awesome stuff!
     
  7. CGDever

    CGDever

    Joined:
    Dec 17, 2014
    Posts:
    156
    Strange... It works, but only in the editor. Compiled code responds to the key :|