如何为 Windows 应用程序注册自定义键盘快捷键

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/598837/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 12:04:52  来源:igfitidea点击:

How to register a custom keyboard shortcut for a windows application

windowsregistrykeyboard-shortcuts

提问by Federico Elles

I want to create a windows utility application, which can be called anytime from within any other application using a keyboard shortcut, e.g.:

我想创建一个 Windows 实用程序应用程序,它可以在任何其他应用程序中使用键盘快捷键随时调用,例如:

? Win+ T

? Win+T

? Ctrl+ T

? Ctrl+T

? Alt+ T

? Alt+T

? Ctrl+ Alt+ T

? Ctrl+ Alt+T

? Ctrl+ Shift+ T

? Ctrl+ Shift+T

What key combinations can I use and how to setup those in the windows registry?

我可以使用哪些组合键以及如何在 Windows 注册表中设置它们?

(If the shortcut is used by an other application, it should of course not work.)

(如果快捷方式被其他应用程序使用,它当然应该不起作用。)

采纳答案by Amr Elgarhy

An option for doing that programatically when your application start is calling this Windows API:

在您的应用程序启动时以编程方式执行此操作的一个选项是调用此 Windows API:

RegisterHotKey(IntPtr hwnd, int id, int fsModifiers, int vk);

And to unregister call this API:

并取消注册调用此 API:

UnregisterHotKey(IntPtr hwnd, int id);

Both exist in user32APIs

两者都存在于user32API 中

http://www.pinvoke.net/search.aspx?search=RegisterHotKey&namespace=[All]

http://www.pinvoke.net/search.aspx?search=RegisterHotKey&namespace=[全部]

回答by David Koelle

If your application (or a shortcut to it) is available on your desktop, you can right-click to get the context menu, select Properties, and enter the Shortcut Key there. Simply click in the Shortcut Key text field, and press the desired shortcut key.

如果您的应用程序(或它的快捷方式)在您的桌面上可用,您可以右键单击以获取上下文菜单,选择“属性”,然后在那里输入快捷键。只需单击快捷键文本字段,然后按所需的快捷键。

I've assigned WIN+ Cto my calculator, and WIN+ Vto my volume control.

我已将WIN+分配C给我的计算器,并将WIN+分配V给我的音量控制。

回答by Franci Penov

If you need more advanced scenario to what the shell shortcut offer, you should start with reading Win32 Hooksand Hooks Overview.

如果您需要更高级的 shell 快捷方式提供的方案,您应该从阅读Win32 HooksHooks 概述开始

More specifically, you need to add a WH_KEYBOARDhook using the SetWindowsHookExfunction. You also need to unhook through UnhookWindowsHookExwhen you are done.

更具体地说,您需要WH_KEYBOARD使用该SetWindowsHookEx函数添加一个钩子。完成后,您还需要脱钩UnhookWindowsHookEx

There's an old article from Dino Esposito how to do Windows Hooks in .NETthrough some Win32 interop.

有一篇来自 Dino Esposito 的旧文章 how to do Windows Hooks in .NETthrough some Win32 interop。

回答by Toby Allen

I'm afraid this isnt something you can do by simply setting values in the registry, it is as has been indicated in other answers necessary to call some windows API routines to achieve this.

恐怕这不是您可以通过简单地在注册表中设置值来完成的事情,正如其他答案中所指出的那样,调用一些 Windows API 例程来实现这一点是必要的。