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

Keyboard events from any key pressed

Discussion in 'Scripting' started by LukeB, Mar 9, 2011.

  1. LukeB

    LukeB

    Joined:
    Aug 8, 2009
    Posts:
    88
    Hey guys :D

    I'd like to be able to pick up when someone presses/ releases any key on the keyboard, just spamming Input.GetKey for every key seems a little extreme, and Event.current in OnGUI only ever runs once for a particular key then just ignores it after that (this might be a bug!)

    Code (csharp):
    1.  
    2. function OnGUI() {
    3. var e : Event = Event.current;
    4. if (e.isKey){
    5. print("detected "+e.keyCode);
    6. }
    7. }
    8.  
    If the same key is pressed more than once this doesnt pick it up at all, making it not too useful!


    If anyone could give any advice on where to go I would be very grateful!

    All the best,
    Luke Briggs
    KuleStar
     
  2. LukeB

    LukeB

    Joined:
    Aug 8, 2009
    Posts:
    88
    Update: Input.AnyKey ignores the same button more than once, so I'm really thinking this is a bug!

    Code (csharp):
    1.  
    2. function Update(){
    3. if(Input.anyKeyDown){
    4.         Debug.Log("You have pressed "+Input.inputString);
    5.     }
    6. }
    7.  
    If a key is pressed twice or more this only displays 1 log (must restart the game for it to log that key again!)
     
  3. teammetablast

    teammetablast

    Joined:
    Nov 22, 2010
    Posts:
    13
  4. LukeB

    LukeB

    Joined:
    Aug 8, 2009
    Posts:
    88
    Hey! Thanks for the reply, just the problem is that they only pick up a key being pressed once then totally pack up! I've got a feeling it's a bug, so I'll report it later if it really isn't suppost to work the way it is :)
     
  5. bahamapascal

    bahamapascal

    Joined:
    Feb 16, 2011
    Posts:
    23
    Hello there,
    here is with what I came up with:
    Code (csharp):
    1.  
    2. function Update () {
    3.      if(Input.GetKey(KeyCode.P)||Input.GetKey(KeyCode.O)||Input.GetKey(KeyCode.U)||Input.GetKey(KeyCode.Y)||Input.GetKey(KeyCode.T)||Input.GetKey(KeyCode.R)||Input.GetKey(KeyCode.E)){
    4.      transform.Rotate(0, 0,35 * Time.deltaTime);
    5.     }
    6.     else{
    7.      transform.Rotate(0, 0,0 * Time.deltaTime);
    8.     }
    9. }
    I only used the transform.Rotate as a test.
    You just have to put in all off the keys,but wit copy and paste that should take less then 1-2 minut(s):)
    I guess this is not the correct way,but it works just fine;)
    Please let me know...
     
  6. LukeB

    LukeB

    Joined:
    Aug 8, 2009
    Posts:
    88
    Hey! Thanks for the reply :D

    I was thinking of doing something like that just I wasn't too sure how performance-ish it is! It would have to check about 40 keys for up or down every frame, which sounds like it could take quite long and drop out the frame rate. If worst comes to worst then I suppose it's the only way that at least works!