缺少对RegisterHotKey的引用

时间:2012-01-07 06:04:17

标签: c# winforms registerhotkey

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

关于这个话题,如果我想创建一个在后台监听组合键的应用程序,我应该使用RegisterHotKey吗?

2 个答案:

答案 0 :(得分:3)

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

简而言之,如果你添加:

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

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

答案 1 :(得分: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);