0

我在谷歌上找不到我必须使用什么参考才能使用 RegisterHotKey。它是什么?

在这个主题上,如果我试图创建一个在后台监听组合键的应用程序,我应该使用 RegisterHotKey 吗?

4

2 回答 2

3

你需要一个DllImport,而不仅仅是一个参考。您可以在 pinvoke.net找到更多信息。

简而言之,如果您添加:

[DllImport("user32.dll")]
private static extern bool RegisterHotKey(IntPtr hWnd, int id, int fsModifiers, int vlc);

在您的程序中的某个地方,唯一剩下的棘手部分是hWnd提出注册以处理密钥。上面 pinvoke.net 上链接的示例代码应该可以帮助您使用DllImport.

于 2012-01-07T06:09:54.923 回答
1

以下是使用 C# 中的 RegisterHotKey 函数所需的内容:

/// <summary> The RegisterHotKey function defines a system-wide hot key </summary>
/// <param name="hwnd">Handle to the window that will receive WM_HOTKEY messages generated by the hot key.</param>
/// <param name="id">Specifies the identifier of the hot key.</param>
/// <param name="fsModifiers">Specifies keys that must be pressed in combination with the key specified by the 'vk' parameter in order to generate the WM_HOTKEY message.</param>
/// <param name="vk">Specifies the virtual-key code of the hot key</param>
/// <returns><c>true</c> if the function succeeds, otherwise <c>false</c></returns>
/// <seealso cref="http://msdn.microsoft.com/en-us/library/ms646309(VS.85).aspx"/>
[DllImport("user32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool RegisterHotKey(IntPtr hWnd, int id, uint fsModifiers, uint vk);
于 2012-01-07T06:12:01.197 回答