c# - 如何禁用热键(除了 ctrl+alt+delete)
我有一个 winform 应用程序,我想在执行该应用程序期间禁用某些组合键。
我想禁用所有组合键(Ctrl+Esc
、Alt+Tab
、WinKeys
等),Ctrl+Alt 除外+删除
。
我使用 c#
进行编程。
谢谢。
I have a winform application and I'd like to disable some key combinations during the execution of this application.
I want to disable all the key combinations (Ctrl+Esc
, Alt+Tab
, WinKeys
, etc), except Ctrl+Alt+Delete
.
I use c#
to program.
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正确的方法是根本不使用 C#(或任何编程语言)。
相反,您需要利用 Windows 对有限用户帐户的内置支持和其他与安全相关的规定。您可以将计算机锁定得如此之紧,以致用户无法执行任何操作。此类事物由组策略控制,而不是单个应用程序应具有任何控制权的事物。
您可以在服务器故障(另一个 Stack Exchange 站点)上询问有关组策略配置和锁定 Windows 计算机的更多问题。处理此类问题。但我建议您先进行搜索,看看您的问题是否已经得到解答!
如果您绝对必须在 C# 中执行此操作,那么您唯一的解决方案将是低级键盘挂钩。首先是因为这是您可以在 C# 中创建的唯一类型的挂钩,其次是因为您需要能够在任何其他应用程序(包括操作系统本身)接收和处理按键之前拦截并丢弃按键。
您在问题中明确排除了 Ctrl+Alt+Delete 是一件好事,因为即使使用低级键盘挂钩,您也无法限制该键。
您可以在 Stephen Toub 的博客上找到示例实现< /a>.
诀窍是您不会为您希望丢弃的按键调用
CallNextHookEx()
函数,这会阻止事件信息传递到下一个应用程序在钩链中。根据CallNextHookEx()
的 文档 :因此,您需要确保您确实为正常键盘输入调用该函数,但不为您希望丢弃的键盘输入调用该函数。您需要返回 1 —
new IntPtr(1)
,而不是CallNextHookEx
。在示例代码中,所有这些都将在
HookCallback
方法中发生,每次收到键盘事件时都会调用该方法。对vkCode
值使用switch
语句(这会为您提供 所按下按键的虚拟按键代码)来确定要拒绝哪个输入。The correct way to do this is not using C# (or any programming language) at all.
Instead, you need to take advantage of Windows's built-in support for limited user accounts and other security-related provisions. 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. 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. 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.
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.
You'll find a sample implementation here on Stephen Toub's blog.
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. As per the documentation forCallNextHookEx()
: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)
.In the sample code, all of that will happen in the
HookCallback
method, which is called each time a keyboard event is received. Use aswitch
statement on thevkCode
value (which gives you the virtual key code of the key that was pressed) to determine which input you want to reject.