Search Unity

Keyboard Input Challenge

Discussion in 'Scripting' started by VICTOM, Oct 12, 2006.

  1. VICTOM

    VICTOM

    Joined:
    Aug 28, 2005
    Posts:
    233
    The keyboard has a physical limitation of accepting up to 3 key inputs. Example: Ctrl+Alt+Delete on a PC

    I need code in Unity that will accept a 3 key input combination. The input combination MUST use [down arrow]+[left arrow]+[any other key].

    I have code that will do any 3 key combos except for this challenge. I need to know if this is a bug in my code or Unity.

    Thanks for taking a look.
    Cheers,
     
  2. CoherentInk

    CoherentInk

    Joined:
    Jul 16, 2006
    Posts:
    216
    The 3 key limit only applies to non-USB keyboards; USB keyboards do not have such a limit.

    You could just check for the keys using if statements:

    Code (csharp):
    1. if (Input.GetButtonDown("Left")  Input.GetButtonDown("Down")  (Input.GetButtonDown("Other")) {
    2.     // Do something when those keys are pressed
    3. }
    Replace "Left", "Down" and "Other" as necessary.

    You could nest the if statements as well:

    Code (csharp):
    1. if (Input.GetButtonDown("Left")) {
    2.     if (Input.GetButtonDown("Down")) {
    3.         if (Input.GetButtonDown("Other")) {
    4.             // Do something
    5.         }
    6.     }
    7. }
    If you want the third key to be any key on the keyboard, I'm not sure how you would do that. I doubt that Input.anyKeyDown would ever work, since it would already be returning true since "Left" and "Down" were already pressed.
     
  3. NCarter

    NCarter

    Joined:
    Sep 3, 2005
    Posts:
    686
    Not necessarily true.... My Apple USB keyboard has a limit for the maximum number of concurrent keypresses, but the number varies depending on which keys you press. For example, if I hold multiple letters on the same row, I can get up to six to register. However, if I press certain clusters of keys, it can be as few as two. In particular, I can't get all four arrow keys to register at once; only two or three register, depending on the combination (the assumption seems to be that you won't want to press up and down simultaneously while pressing left or right).

    You can check this out by adding the Keyboard Viewer palette to the Input Menu, then holding keys while watching the display (preferably while using an application which doesn't mind random keypresses!).

    The worst thing is that the precise behaviour of the keyboard is hardware specific, so some keyboards might work better than others in this respect.
     
  4. VICTOM

    VICTOM

    Joined:
    Aug 28, 2005
    Posts:
    233
    Thanks for the replies.
    It looks like it is the keyboard.
    [down]+
    +[alpha keys] DO NOT WORK
    [down]+
    +[ctrl or shift] WORKS

    Cheers,
    -VICTOM​
     
  5. NCarter

    NCarter

    Joined:
    Sep 3, 2005
    Posts:
    686
    I can apparently hold down, left and most alpha keys, except for a stripe in the middle of the keyboard. Looks like it's not going to work reliably.

    Incidentally, the modifier keys (shift, option, control, command and caps lock) are not subject to this limitation. They always work, even all at once in conjunction with any other keys. This makes them pretty good for use as action buttons in games.