Search Unity

Keyboard Input

Discussion in 'Scripting' started by TRuoss, Jan 11, 2013.

  1. TRuoss

    TRuoss

    Joined:
    Dec 5, 2012
    Posts:
    85
    Hi there,

    i´m working on a shortcut system for my program. The target is to have the possibility of shortcuts like pressing "Space" or "Control + Space" to fire different functions.

    i implemented it this way:

    Code (csharp):
    1.  
    2. Event e = Event.current;
    3.  
    4. if (e.type == EventType.keyUp)
    5. {
    6. //Render
    7. if (e.keyCode == KeyCode.Space)
    8.                 {
    9.                     ...
    10.                 }
    11.  
    12. //TakeSnap
    13. if (e.control  e.keyCode == KeyCode.Space)
    14.                 {
    15.                     ...
    16.                 }
    17. }
    18.  
    Problem: if i press "control + space", it will fire both functions. Solution could be this way...


    Code (csharp):
    1.  
    2. Event e = Event.current;
    3.  
    4. if (e.type == EventType.keyUp)
    5. {
    6. //Render
    7. if (e.keyCode == KeyCode.Space  !e.control  !e.alt  !e.command  !e.shift)
    8.                 {
    9.                     ...
    10.                 }
    11.  
    12. //TakeSnap
    13. if (e.control  e.keyCode == KeyCode.Space  !e.alt  !e.command  !e.shift)
    14.                 {
    15.                     ...
    16.                 }
    17. }
    18.  
    than there will be only a single Key allowed. But i don´t want to copy all these stuff to all my 30+ shortcuts. I tried it with a function, but i have shortcuts like "control + key", "command + key" and "shift + key" and its not handy.


    i hope someone get the point out of this :D and could suggest me a solution to get rid of all the copy paste stuff.

    edit: sry for my bad english. i will work on it!
     
    Last edited: Jan 11, 2013
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    could you not get there with "else if" entries?


    if ctrl + space
    <stuff>
    else if space
    <stuff>
    endif

    edit: the order of the statements is important
     
  3. TRuoss

    TRuoss

    Joined:
    Dec 5, 2012
    Posts:
    85
    i think this will make it even more complex, because it will also fire space if i press "alt + space", "shift + space", "command + space". At the moment i dont know if in future i will have different funktions for "control + space" or "shift + space"
     
  4. FlaSh-G

    FlaSh-G

    Joined:
    Apr 21, 2010
    Posts:
    212
    You could do something like this:
    Code (csharp):
    1. struct CombinedInput
    2. {
    3.   private KeyCode primary;
    4.   private bool ctrl;
    5.   private bool shift;
    6.   //etc.
    7.  
    8.   public CombinedInput(KeyCode primary, bool ctrl, bool shift) //etc.
    9.   {
    10.     this.primary = primary;
    11.     this.ctrl = ctrl;
    12.     this.shift = shift;
    13.     //etc.
    14.   }
    15.  
    16.   public bool IsPressed()
    17.   {
    18.     Event e = Event.current;
    19.     return e.keyCode == primary  e.control == ctrl  e.shift ==shift; //etc.
    20.   }
    21. }
    and then use it like this:

    Code (csharp):
    1. if(new CombinedInput(KeyCode.Space, true, false).IsPressed()) //etc.
    2. {
    3.   //things
    4. }
    or store all CombinedInputs in an Array and iterate it.
     
    Last edited: Jan 11, 2013
  5. TRuoss

    TRuoss

    Joined:
    Dec 5, 2012
    Posts:
    85
    i will try it looks good. thanks @ FlaSh.G
     
  6. Aldo

    Aldo

    Joined:
    Aug 10, 2012
    Posts:
    173
    I would do it like this

    first if 2 keys

    if(control AND space)
    else if (control)
    else if (space)

    this way it will only try control OR space when there is only 1 pressed

    and yes if control and space must be first since you cant ask if control and space on an if else statement when the first has been found correct