简体   繁体   English

C#-如何禁用热键(Ctrl + Alt + Delete除外)

[英]c# - how to disable hotkeys (except ctrl+alt+delete)

I have a winform application and I'd like to disable some key combinations during the execution of this application. 我有一个Winform应用程序,我想在执行此应用程序期间禁用一些组合键。

I want to disable all the key combinations ( Ctrl+Esc , Alt+Tab , WinKeys , etc), except Ctrl+Alt+Delete . 我想禁用除Ctrl+Alt+Delete之外的所有组合键( Ctrl+EscAlt+TabWinKeys等)。

I use c# to program. 我使用c#进行编程。

Thank you. 谢谢。

The correct way to do this is not using C# (or any programming language) at all. 正确的方法根本不使用C#(或任何编程语言)。

Instead, you need to take advantage of Windows's built-in support for limited user accounts and other security-related provisions. 相反,您需要利用Windows对有限用户帐户和其他与安全相关的规定的内置支持。 You can lock down a computer so tightly that a user can't do anything. 您可以将计算机锁定得太紧,以致用户无法执行任何操作。 This type of thing is controlled by Group Policies, rather than something that an individual application should have any sort of control over. 这种事情是由组策略控制的,而不是单个应用程序应该对其进行任何控制的事物。

You can ask more questions about Group Policy configuration and locking down a Windows machine on Server Fault , another Stack Exchange site that deals with this type of questions. 您可以询问有关组策略配置和锁定Windows Fault的其他问题, Server Fault是另一个处理此类问题的Stack Exchange网站。 But I recommend doing a search first to see if your question has already been answered there! 但我建议先进行搜索,看看您的问题是否已在此处得到解答!


If you absolutely must do this in C#, your only solution is going to be a low-level keyboard hook. 如果您绝对必须在C#中执行此操作,则唯一的解决方案是使用低级键盘挂钩。 First because that's the only type of hook you can create in C#, and second because you need to be able to intercept and discard the key presses before any other application (including the OS itself) can receive and process them. 首先,因为这是您可以在C#中创建的唯一挂钩类型;其次,因为您需要能够在任何其他应用程序(包括OS本身)接收和处理按键之前拦截并丢弃按键。

It's a good thing that you specifically excluded Ctrl+Alt+Delete in the question, because you're not going to be able to restrict that one, even with a low-level keyboard hook. 在问题中明确排除Ctrl + Alt + Delete是一件好事,因为即使使用低级键盘钩子,您也将无法限制该键。

You'll find a sample implementation here on Stephen Toub's blog . 您可以在Stephen Toub的博客上找到一个示例实现。

The trick is that you won't call the CallNextHookEx() function for the key presses that you wish to discard, which prevents the event information from being passed to the next application in the hook chain. 诀窍在于,您不会为希望放弃的按键调用CallNextHookEx()函数,从而防止了事件信息被传递到挂钩链中的下一个应用程序。 As per the documentation for CallNextHookEx() : 根据CallNextHookEx()文档

Calling CallNextHookEx is optional, but it is highly recommended; 调用CallNextHookEx是可选的,但强烈建议您使用; otherwise, other applications that have installed hooks will not receive hook notifications and may behave incorrectly as a result. 否则,其他已安装钩子的应用程序将不会收到钩子通知,因此可能会出现不正确的行为。 You should call CallNextHookEx unless you absolutely need to prevent the notification from being seen by other applications. 除非绝对需要防止其他应用程序看到该通知,否则应调用CallNextHookEx。

So you'll need to make sure that you do call the function for normal keyboard input, but not for that you wish to throw away. 因此,您需要确保确实为正常的键盘输入调用了该函数,但是并没有为您希望扔掉的函数而调用。 Instead of CallNextHookEx , you'll want to return 1— new IntPtr(1) . 您将要返回1- new IntPtr(1)而不是CallNextHookEx

In the sample code, all of that will happen in the HookCallback method, which is called each time a keyboard event is received. 在示例代码中,所有这些都将在HookCallback方法中发生,该方法在每次接收到键盘事件时都会调用。 Use a switch statement on the vkCode value (which gives you the virtual key code of the key that was pressed) to determine which input you want to reject. vkCode值上使用switch语句(可为您提供所按下键的虚拟键代码 ),以确定要拒绝的输入。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM