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): Event e = Event.current; if (e.type == EventType.keyUp) { //Render if (e.keyCode == KeyCode.Space) { ... } //TakeSnap if (e.control e.keyCode == KeyCode.Space) { ... } } Problem: if i press "control + space", it will fire both functions. Solution could be this way... Code (csharp): Event e = Event.current; if (e.type == EventType.keyUp) { //Render if (e.keyCode == KeyCode.Space !e.control !e.alt !e.command !e.shift) { ... } //TakeSnap if (e.control e.keyCode == KeyCode.Space !e.alt !e.command !e.shift) { ... } } 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 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!
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
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"
You could do something like this: Code (csharp): struct CombinedInput { private KeyCode primary; private bool ctrl; private bool shift; //etc. public CombinedInput(KeyCode primary, bool ctrl, bool shift) //etc. { this.primary = primary; this.ctrl = ctrl; this.shift = shift; //etc. } public bool IsPressed() { Event e = Event.current; return e.keyCode == primary e.control == ctrl e.shift ==shift; //etc. } } and then use it like this: Code (csharp): if(new CombinedInput(KeyCode.Space, true, false).IsPressed()) //etc. { //things } or store all CombinedInputs in an Array and iterate it.
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