Combined keys action

Hello, i’m trying to make a key manager, and have done up to one key per action management. What i want to add now is managing a combination of keys (alt + a, ctrl + b, so and so).

By itself it is pretty easy to do, really. The problem lies in the managers behavour when combinations overlap, ie : combination 1 : alt + a; combination 2 : alt + ctrl + a. I’d like to think i’ll just check what keys are pressed and choose the longer combination of those i detected. What i don’t know how to do is how to avoid random key pressing of the user : to get alt + ctrl + a you can hardly get all the keys at same time, so you’ll get either combination of those 3 keys in the first frame then over the next frames you’ll get the rest of the keys.

Since i’m pretty sure i didn’t explain the problem in a comprehensive way, here’s an exemple of user interaction and what the manager is supposed to do :

1 - We have the following combinations :

  1. CTRL + A

  2. ALT + A

  3. CTRL + ALT + A

  4. CTRL + SHIFT + A

  5. ALT + SHIFT + A

  6. CTRL + ALT + SHIFT + A

(i used these for the example and keeping it simple, not because i like the letter ‘A’ :))
So, when trying to get the combination CTRL+SHIFT+ALT+A (the order doesn’t matter), a user can press (then = next frame; and = same frame) :

#1 - CTRL, then ALT, then SHIFT, then A.

#2 - CTRL and ALT, then SHIFT, then A.

#3 - CTRL and ALT, then SHIFT and A.

#4 - CTRL and ALT and SHIFT, then A.

#5 - CTRL and ALT and SHIFT and A.

#6 - Any other combination between those.

If we take #2, you have CTRL and ALT, but if instead of ALT the user presses A…To my knowledge, the manager should activate the action 1), then, when the user adds another modifier, it passes onto that action (3) if it’s ALT, 4) if it’s SHIFT) and then getting 6) when all 3 modifiers are pressed. Am i on the right track here?

The problems i’m having is :

  • How should i check what combinations to ignore in case i don’t have the combination 6) and the keys pressed are CTRL + ALT + SHIFT + A.

  • In case i have also 7) CTRL + ALT + SHIFT + B, and the user is pressing (intentionaly or not) CTRL + ALT + SHIFT + A + B, what combinations do i validate?

Any ideas for these 2 problems?

Thank you in advance.

2 Answers

2

Have booleans called ctrlDown, shiftDown, and altDown, and set them appropriately if the respective keys are pressed/released. Then you can have code like

if (Input.GetKeyDown(KeyCode.A)) {
    if (ctrlDown && !altDown && !shiftDown) {
        // do something
    }
    else if (altDown && !ctrlDown && !shiftDown) {
        // do something else
    }
    // etc.
}

Hello, sorry for not answering before (been a busy week end). I think i haven’t explained clearly what i want to do. I want to have a key manager so that all keys forming a combination can be checked dynamically (not only modifier keys like SHIFT or ALT, but also other keys). I’m trying to make something cross platform (no specific use, just able to manage all keys used by Unity, including mouse and joystick buttons). I somehow made a version that works with one key per action (no combinations), then another that uses GetKey, GetKeyDown and GetKeyUp from Unity. Now i’m trying to maje it work the same as in games : One action per pressed combination (at the moment it’s autorepeat as long as the user is holding down the keys).