- Home /
How do i disable the system shortcut key after the game begins?
I want to find a solution that can disable some of the system's shortcut keys, such as Windows keys, Alt+Tab shortcuts, and so on. It seems that in the Unity project, only interactive buttons can be defined, and the shortcut keys of the system cannot be banned. If you have any good ideas, please let me know. Thank you very much!
Answer by Bunny83 · Aug 24, 2017 at 04:54 AM
system shortcuts should never be suppressed by a "normal application". They may be the only way to exit / terminate the application. Games really shouldn't even think about something like that. If you block things like Alt+Tab or Alt+F4 most users wouldn't be pleased. It's still the user whos operating the computer and not the game.
However if you really need to block certain hotkeys the only way is to install a lowlevel keyboard hook(WH_KEYBOARD_LL) with the win API. This has to be done in a seperate thread. The thread need to run a message pump (Call GetMessage in an infinite loop).
The actual hook method that is called for any keyboard event usually calls "CallNextHookEx" to pass the event on to other hook and the system. Here's where you can block "most" key events. Though certain hotkeys / key combinations can't be blocked since they are handled before the hook mechanic. One is CTRL+ALT+DEL
ps: If you're not familiar with thread safety, thread synchronisation and the win API you should be careful. There are many things which you can mess up. A wrongly implemented hook could block your whole system. Also keep in $$anonymous$$d that the low-level hook is a global hook. So you have to handle the installing / uninstalling dynamically based on if the game is currently in focus or not.
Obviously this solution only works on Windows. There might be a similar approach for $$anonymous$$ac and Linux but i've never tried something like that on those systems.
Thank you. $$anonymous$$y problem has been solved. Yes, I use the lowlevel keyboard hook, because my company is not a game development company, so these specific requirements is not a problem for users, on the contrary, it can help users to make better use of our products. It is worth mentioning that when using lowlevel keyboard hook, notice that the system is 32 bit or 64 bit, and it causes me to spend some important time on it. In a word, thank you for your help!
Well the only situation that would legitimate such a lock down would be when writing kiosk software or when presenting a product on a trade fair / expo. Though in the latter case it would make more sense to implement the lockdown with another software seperate. For example "AutoHot$$anonymous$$ey" can be used to block most keys / key combinations. AH$$anonymous$$ uses low level hooks. It's free software now if you're interested.
32 and 64 bit processes shouldn't matter for low level hooks. It's true that most globel hooks have to be implemented in a seperate DLL because the DLL gets injected into every process by the OS. Such a hook is actually executed inside the context of the foreign process. A DLL can only be injected when the architectures match.
However the LL hooks work different. They are actually executed inside the context of the thread that installed the hook. The OS does a context switch, executes the callback and then switches back. No code is injected into other applications in this case. Though that's only true for the two low level hooks (keyboard and mouse). Any other global hook need to be inside a DLL.
Global DLL hooks is actually a common way to "hack" into running applications. Another application can install a global message hook in a DLL and just send a message to the target application. This will invoke the hook callback inside the target application. From inside the target context you have access to everything in that process since you have full permissions inside that context.