在Windows中添加全局键盘快捷键的最简单方法是什么?

时间:2021-12-18 17:35:43

I would like for my app to detect keyboard shortcuts when it isn't running, but as there doesn't appear facility to do this in Windows a reasonable fallback would be to have a background process without any UI that listens for keypresses. Is it possible to monitor keypresses when an app isn't in the foreground in .Net?

我希望我的应用程序在未运行时检测键盘快捷键,但由于在Windows中似乎无法执行此操作,因此合理的后备操作是在没有任何UI监听按键的情况下进行后台处理。当应用程序不在.Net的前台时,是否可以监控按键?

Looks like this CodeProject article has what I need.

看起来这个CodeProject文章有我需要的东西。

4 个解决方案

#1


This library is pretty good too

这个库也很不错

EDIT: I recently created this library, which uses a different approach: NHotkey

编辑:我最近创建了这个库,它使用了不同的方法:NHotkey

#2


A Windows shortcut can have an associated hotkey - right-click on any program in your Start menu, go Properties, and you'll see a "Shortcut key" box. Windows will then look for that hotkey on your behalf and launch your program when it sees it. (I use it to make Ctrl+Alt+C launch Calculator.)

Windows快捷方式可以有一个关联的热键 - 右键单击​​“开始”菜单中的任何程序,转到“属性”,然后您将看到“快捷键”框。然后,Windows将代表您查找该热键,并在看到该程序时启动您的程序。 (我用它来制作Ctrl + Alt + C启动计算器。)

To set this up programmatically, use IShellLink.

要以编程方式设置它,请使用IShellLink。

#3


By calling this API:

通过调用此API:

RegisterHotKey

http://msdn.microsoft.com/en-us/library/ms646309.aspx

http://www.pinvoke.net/default.aspx/user32/RegisterHotKey.html

Then override this method "WndProc" to monitor keypresses.

然后重写此方法“WndProc”以监视按键。

protected override void WndProc(ref System.Windows.Forms.Message m)
    {
        // let the base class process the message
        base.WndProc(ref m);

        // if this is a WM_HOTKEY message, notify the parent object
        const int WM_HOTKEY = 0x312;
        if (m.Msg == WM_HOTKEY) ...............

http://msdn.microsoft.com/en-us/library/system.windows.forms.control.wndproc(VS.71).aspx

#4


There's no way to make it non intrusive.

没有办法让它不干扰。

It is always intrusive to add global keyboard shortcut. If you have to do it anyways, make sure that it is an opt-in, not opt-out feature.

添加全局键盘快捷键总是有侵入性的。如果您还是必须这样做,请确保它是一个选择加入,而不是选择退出功能。

#1


This library is pretty good too

这个库也很不错

EDIT: I recently created this library, which uses a different approach: NHotkey

编辑:我最近创建了这个库,它使用了不同的方法:NHotkey

#2


A Windows shortcut can have an associated hotkey - right-click on any program in your Start menu, go Properties, and you'll see a "Shortcut key" box. Windows will then look for that hotkey on your behalf and launch your program when it sees it. (I use it to make Ctrl+Alt+C launch Calculator.)

Windows快捷方式可以有一个关联的热键 - 右键单击​​“开始”菜单中的任何程序,转到“属性”,然后您将看到“快捷键”框。然后,Windows将代表您查找该热键,并在看到该程序时启动您的程序。 (我用它来制作Ctrl + Alt + C启动计算器。)

To set this up programmatically, use IShellLink.

要以编程方式设置它,请使用IShellLink。

#3


By calling this API:

通过调用此API:

RegisterHotKey

http://msdn.microsoft.com/en-us/library/ms646309.aspx

http://www.pinvoke.net/default.aspx/user32/RegisterHotKey.html

Then override this method "WndProc" to monitor keypresses.

然后重写此方法“WndProc”以监视按键。

protected override void WndProc(ref System.Windows.Forms.Message m)
    {
        // let the base class process the message
        base.WndProc(ref m);

        // if this is a WM_HOTKEY message, notify the parent object
        const int WM_HOTKEY = 0x312;
        if (m.Msg == WM_HOTKEY) ...............

http://msdn.microsoft.com/en-us/library/system.windows.forms.control.wndproc(VS.71).aspx

#4


There's no way to make it non intrusive.

没有办法让它不干扰。

It is always intrusive to add global keyboard shortcut. If you have to do it anyways, make sure that it is an opt-in, not opt-out feature.

添加全局键盘快捷键总是有侵入性的。如果您还是必须这样做,请确保它是一个选择加入,而不是选择退出功能。