- Home /
Platform independent keyboard modifiers for shortcuts
I'm implementing keyboard shortcuts in an editor window and was wondering if there's a standard way to get platform independent modifier keys. For example, keyboard shortcuts use Control on a PC and Command on a Mac.
Right now I use a small utility function:
public static bool CommandKeyDown()
{
if (Application.platform == RuntimePlatform.OSXEditor)
{
return Event.current.command;
}
return Event.current.control;
}
But it seems like Unity would have some built in way to handle this kind of thing...? Obviously not urgent since I have a workaround, but would like to tidy this up if possible.
Answer by Alex-Chouls · Jun 14, 2011 at 05:46 PM
Actually found the exact thing I was looking for: EditorGUI.actionKey
"Is the platform-dependent "action" modifier key held down? (Read Only)
The key is Command on Mac OS X, Control on Windows."
http://unity3d.com/support/documentation/ScriptReference/EditorGUI-actionKey.html
Answer by Bunny83 · May 26, 2011 at 02:28 AM
Ok it's quite common that the equivalent on mac for the control key is the command key but that's not really a rule or standard. Event.current.command also works on windows machines but it's the windows key. I don't see any reason why unity should provide a function that will do exactly what you did.
Actually you aren't talking to unity here ;) We are the community that is using Unity. If you have a feature request for Unity post it on feedback.unity3d.com
Thanks. Unity itself establishes some conventions (e.g., keyboard shortcuts like copy/paste use ctrl on windows and command on mac) so was hoping there might be some internal abstraction for tool developers looking for the "platform dependent keyboard shortcut modifier key"!
There are enough little differences in the way the unity editor code works on mac and pc to make it a bit of a pain to standardize the behavior, so personally I'd welcome anything Unity can do to abstract away platform differences. It also seems in line with their design philosophy, given Unity's custom platform independent GUI.
Answer by Eric5h5 · May 26, 2011 at 02:28 AM
There isn't any built-in way, no. After all, Event.current.control exists on OS X as well, whereas there is no real equivalent to the Command key on Windows (I understand the Windows key can't really be used in the same way). I don't see anything untidy about your function.
Thanks Eric5h5. The thing I don't like about the function is that I have to remember to use it! ;) Something like EventType.shortcut$$anonymous$$odifier$$anonymous$$ey which mapped to Ctrl on pc and Command on mac would be less error prone I$$anonymous$$O.