I want my game have shortcuts.
But I don’t want it to work when the input is mean to be system hotkey.
Example?
This should be of no concern since OS level shortcuts aren’t propagating to the application, such as Win+D or Alt+Enter. But others may be, for instance Ctrl+C is not an operating system shortcut although most applications implement it to do the same thing.
In my opinion, when I set Alt to some behaviour, it should be executed when user pressed Alt+Tab.
I want to detect that and cancel the behaviour assigned to Alt when it happens.
It’s for developers like you that it is a good thing we can’t just override the behaviour of system-wide OS shortcuts.
These exist for a reason, the user expects them to work the same way in any and all situations. If you don’t use Unity it’s a possibility to override these but … have you noticed how practically no application ever overrides those OS shortcuts?
At most a game sometimes prevents Alt+F4 likely as a anti-greaving measure in online games.
read below
Well, when you are on windows, you can use RegisterHotKey / UnregisterHotKey from the user32 dll. Though the usage in Unity is a bit more complicated than in a normal C# Forms application for example. Those hotkeys will trigger a WM_HOTKEY message for the window you registrate the hotkey for. In your case that would need to be the window of your game. Furthermore you need to hook into the message loop of the main window.
Unfortunately you don’t know your own window handle as Unity creates the native window for you. So you need to use something like I did here for the file drag and drop support. You would actually need the same hook (WH_GETMESSAGE) that allows you to intercept any window message that the OS is sending to the window. instead of WM_DROPFILES you would look for WM_HOTKEY.
Currently I don’t have time to elaborate on this. I have once written a small set of classes for a normal C# Forms application to install system wide hotkeys. Though currently they depend on System.Windows.Forms, but mostly due to the Message struct and the “Keys” enum I think. Maybe I can port my implementation for Unity.
Note that when you mess with native OS stuff, you really need to make sure that you unregister your hotkeys when your application closes. Also, just like my drag and drop solution, you most likely don’t want to use it in the editor. I disabled the functionality in the editor since it would affect the Unity editor. You also can easily run into issues due to dynamic recompilation. This makes cleaning up that native stuff quite tricky and can lead to crashes of the editor. So you really should only use it in a build.
Though in the end I’m with CodeSmile here. I needed the systemwide hotkeys because I needed my application to react to input when a different application has focus. Trying to take over the control over the PC / OS is certainly not a good idea. You get a few reports and your “game” would be on literally every maleware list, since in some sense it is. In my application I actually needed several keys but I choose one to enable / disable the others dynamically.
Ohh, I think I get it now. You don’t want to install system wide hotkeys or prevent system hotkeys but you want your game to not react when a system hotkey like ALT+TAB is pressed.
Well, this can not be done in a generic way since there are many hidden system hotkeys and some are hardcoded in different places in the OS. (Like CTRL+SHIFT+ESC to open the taskmanager). Apart from that, pressing down ALT happens before pressing TAB. So in Unity when you react to the release of the ALT key, you can check if your application lost focus, if that’s your main concern. Personally I would never use ALT or CTRL for anything in a game. In the past this was common due to keyboard ghosting issues and ALT / CTRL / SHIFT were usually on separate scanlines since they are meant to be pressed together with other keys. Nowadays we rarely run into such issues. Players expect to remap their keys anyways because different keyboard layouts have different issues with certain keys. I have a german keyboard and to the left of 1 where you usually find the tilde “~” key, we have the carret “^” key. I remember the time playing quake and when opening the console you would always write a “^” that you had to remove, otherwise you write in “colors”
So I wouldn’t bother with that too much. Just don’t use the ALT key. Windows has so many secret (and stupid) hotkeys like “ALT + SHIFT” that switches the keyboard layout when you have more than one.
You might think that’s a good idea but most players are going to hate you for it. System hotkeys exist to assist with the process of using the computer. Bypassing them means you are taking away the user’s ability to use their own computer which can be considered a malicious behavior.
Note that I also first thought that’s what he wanted to do. However I think, as I said in the second half of my post, he wants to detect when a system hotkey is pressed in order to prevent his own action which was using the ALT key. So I don’t think he want to prevent the system hotkey but his own action in case the user presses ALT+TAB
Yes you’re right, Sorry for misleading