How to trap the keyboard strokes on a c# win forms application (CTRl + alt +Del)

跟風遠走 提交于 2019-11-28 07:06:41

Based on other answers, it seems that this is possible to do. Although I highly discourage this. Take for instance that your program should for some reason hang (god forbid...). Then you would have the situation that the only thing the user can do is to turn off the computer with the power button (or pull the plug...).

It is for a good reason that this is difficult to do, and the methods are poorly documented...

The only way this looks like the way to go, is the comment from Pierre-Alain Vigeant if this is a kiosk computer or something. Then it would actually make sense to do this!

I don't think this is a good approach.

You are developing an application for the user and should not try to hinder his general actions.

For Alt+Ctrl+Del key combination read this article.

AFAIK, Ctrl+Alt+Del generates a hardware interrupt and cannot be handled through software applications. Probably this can be handled through system-level keyboard hooks but I am not so sure about that either.

Have a look here:

http://www.thescarms.com/vbasic/StopReBoot.aspx

Essentially for Win9x we trick system to think that the screensaver is running (which disables Ctrl-Alt-Delete sequence) and for WinNT we remap keyboard.

Hmm, old topic without a real answer.

Short version:

  • You need to use drivers to trap system key strokes
  • To do so use the Interception Api which provides signed drivers for that purpose.
  • To be able to use Interception you need to download and install the WDK (Windows Driver Kid)
  • To test your program use a virtual machine with a test system. You do not want to test drivers in your running productive system.

Long version:

Yes, it is possible. To be able to intercept those key combination you need to provide a keyboard driver in the kernel layer. To explain why: In general there are two different types of key strokes in Windows. There are the system keystrokes (WM_SYSKEYDOWN or WM_SYSKEYUP) and the non-system keystrokes (WM_KEYDOWN or WM_KEYUP). Only the non-system keystrokes can be interrupted by a hook. Those keystroke messages are generated in the driver and then passed into the system-message-queue. If a WM_SYSKEYDOWN or WM_SYSKEYUP is inside this queue there is no possiblity on removing it before windows can handle it itself.

What can I do to prevent pushing System Key Strokes into the SMQ? Provide a signed driver to filter those. To create and to sign a driver yourself is not the easiest thing you can do. But there are APIs we can use. For example: Interception from oblita. That api provides signed driver to interact with the keyboards or lower drivers.

Reference for key events under windows: https://msdn.microsoft.com/en-us/library/windows/desktop/ms646267(v=vs.85).aspx

Set Form.TopMost to true, call Form.Activate() every millisecond and raise the process and entry thread priorities.

(Lo and behold the poor user which your application crashes on.)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!