0

I am coding in C#, if it is relevant.

I am trying to disable system hotkeys for a kiosk application. The code used here has come from: https://www.codeproject.com/kb/cs/kiosk_cs.aspx?display=print

This individual: How to disable the pressing/holding down of the Alt key, Control Key, and Shift Key when the left mouse button is clicked has appeared to have successfully used this method to disable Alt + F4 on Windows. However she didn't explicitly specify which version of Windows she was using other than saying one of the commands didn't work in W8.

Tutorial for those who don't understand the code: https://www.youtube.com/watch?v=qQWqGOaZiFI


I tried it.

RegisterHotKey(this.Handle, int 1, (int)USE_ALT, (int)Keys.F4);

^ keeps failing though.

Marshal.GetLastWin32Error().ToString()

When ^ runs, it returns the error code "1400".

Enum of errors: https://msdn.microsoft.com/en-us/library/windows/desktop/ms681385(v=vs.85).aspx. This suggests this is a result of an "Invalid window handle.". I don't know why this is because "Alt + F4" closes the current selected window. I'm pretty sure that's what FindWindow(string cls, string wndwText) returns.

I added "SetLastError = true" in:

[DllImport("user32.dll", SetLastError = true)] private static extern int RegisterHotKey(IntPtr hwnd, int id, int fsModifiers, int vk);

Now, registering "Alt + F4" returns "1409", which means "Hot key is already registered". Which is exactly the point. It's already registered and for this kiosk application, I need to disable that temporarily. My research on this has shown that I need to use user32.dll to create hooks. Which is exactly what I am doing or trying to do.

Have I missed something?

I still need a sure-proof way to block Alt + F4 as well as other system hotkeys. However the other hotkeys: CTRL+W, CTRL+N, CTRL+S, CTRL+A, CTRL+C, CTRL+X, CTRL+V, CTRL+B, CTRL+F and CTRL+H have successfully been blocked on Windows 10 and 8.1.


Useful Information

  • It's worth noting this code was compiled (Build > Publish), installed and run on W8.1. Alt + F4 was successfully blocked... somehow. However, the other two system hotkeys Alt + Tab and Home + Up fail to register for the same reason: "1409" - "Hot key is already registered".
  • Running in W8 compatibility mode on W10 does not give me the same result as W8.1.
  • I am running the app in Administrator's mode.
  • Alt + F4 is just one of the many system hotkeys I want to disable. It's not the only one.
Community
  • 1
  • 1
Rehaan
  • 115
  • 1
  • 9
  • 1
    I don't think this is the right approach to disable ALT+F4 behavior. Your application's window should instead listen for the `WM_CLOSE` message and just chuck the message. Windows doesn't like it if you try to mess with built-in hotkeys. – vcsjones Apr 06 '17 at 16:35
  • 1
    In .NET the way to do this is handle the [`FormClosing`](https://msdn.microsoft.com/en-us/library/system.windows.forms.form.closing(v=vs.110).aspx) event and then set the `CAncelEventArgs.Cancel` to true to cancel the close. – vcsjones Apr 06 '17 at 16:37
  • Your method wouldn't work. This is a wrapper which controls another application. So the other app would be 'the' app. This is just designed to enforce a kiosk environment. Not to mention that other hotkeys like Alt + Tab would still work. user32.dll is designed for this purpose. .NET does not allow disabling system features like this for security reasons. So actually my question would be more accurate if directed towards user32.dll and not C#. – Rehaan Apr 07 '17 at 08:53
  • Why the downvotes and no feedback? That's not helpful. – Rehaan Apr 07 '17 at 14:18
  • I just solved this....https://stackoverflow.com/questions/43268643/how-to-disable-override-windows-10-hotkeys-with-c-sharp/43302972#43302972 – Michael Z. Apr 09 '17 at 04:25
  • @MichaelZ. Thanks for the reply! If I'm understanding this correctly, that method can be used to disable the hotkeys on all windows? So the current window does not need to be focused? – Rehaan Apr 10 '17 at 09:50
  • Also, I'm having some difficulty understanding this code. It's mostly as a result of the project at 'CodeProject'. When opened in VS, a pop up asks me to perform a one-way upgrade to the project because it is "not supported"; the project supports .NET v2 while I have v4.5.2 - leaving me suspicious that some of the code is not supported? After the upgrade, the code does not run so I cannot debug and I have no idea what the code is doing. The 'gma.UserActivityMonitor' class is confusing. Is there any material you'd recommend I read to get me on the right path? I'm fairly new to c# still. – Rehaan Apr 10 '17 at 13:26
  • @Rahul that is correct. It can catch all keys before they are sent to a window. I just took the code from the code project as is. I added the .cs files to my c# project then used the code in my answer. I can get you going. I'll be back after I drop my son off at school. – Michael Z. Apr 10 '17 at 13:52
  • Don't open the code project in VS. just use file explorer to get the .cs files into your project. – Michael Z. Apr 10 '17 at 13:53
  • You don't need code from any test app in that project – Michael Z. Apr 10 '17 at 13:53
  • 1
    I know it works because I'm using it to disable and override several default hotkeys – Michael Z. Apr 10 '17 at 13:55
  • Is there not a way to achieve this using user32.dll? I would love to use the way in your link but this method currently makes no sense to me and I don't even know how to get started on it... I can't make sense of the class HookManager and why it's saved as a Project and not as a class. Again, I'm fairly new. Unless the Project is merely an attempt to organise the class? I'm going to assume you don't have the time to explain how this method works so I'll keep looking for another method, thanks. EDIT: Just saw your replies... I'll try for a little longer ;) – Rehaan Apr 10 '17 at 13:59
  • No, if you want to override default system keys you need a keyboard hook. You cannot see which keys are pressed at the system level without a keyboard hook. If you only need to prevent ALT+F4 in your app then a keyboard hook is way overboard. You need to check if ALT+F4 was pressed in your Form.Closing event. You can check which keys were sent to your window by handling the Form.KeyDown event. – Michael Z. Apr 10 '17 at 16:11
  • 1
    I can't believe @Martijn Pieters deleted my answer. A perfectly good answer to your question and would help others. Maybe he should go delete bad answers. – Michael Z. Apr 10 '17 at 16:17
  • That was it for me. I'm done with this site, thanks @MartijnPieters – Michael Z. Apr 10 '17 at 16:18
  • What the hell. Why did your answer get deleted? Lobby to get it un-deleted. They better be able to do that -.- – Rehaan Apr 10 '17 at 16:34
  • @Rahul The answer was deleted because it was reposted. Instead of duplicating the answer, they should have either flagged the question as a duplicate or, if the questions are different, written an answer tailored specifically to this question. – Baum mit Augen Apr 10 '17 at 16:36

0 Answers0