RegisterHotKey FunctionThe RegisterHotKey function defines a system-wide hot key.Syntax BOOL RegisterHotKey(
HWND hWnd,
int id,
UINT fsModifiers,
UINT vk
);Parameters hWnd
[in] Handle to the window that will receive WM_HOTKEY messages generated by the hot key. If this parameter is NULL, WM_HOTKEY messages are posted to the message queue of the calling thread and must be processed in the message loop.
id
[in] Specifies the identifier of the hot key. If a hot key already exists with the same hWnd and id parameters, it is replaced by the new hot key. If the hWnd parameter is NULL, then the hot key is associated with the current thread rather than with a particular window.
fsModifiers
[in] Specifies keys that must be pressed in combination with the key specified by the uVirtKey parameter in order to generate the WM_HOTKEY message. The fsModifiers parameter can be a combination of the following values. MOD_ALT
Either ALT key must be held down.
MOD_CONTROL
Either CTRL key must be held down.
MOD_SHIFT
Either SHIFT key must be held down.
MOD_WIN
Either WINDOWS key was held down. These keys are labeled with the Microsoft Windows logo. Keyboard shortcuts that involve the WINDOWS key are reserved for use by the operating system. vk
[in] Specifies the virtual-key code of the hot key.Return Value If the function succeeds, the return value is nonzero. If the function fails, the return value is zero. To get extended error information, call GetLastError.Res When a key is pressed, the system looks for a match against all hot keys. Upon finding a match, the system posts the WM_HOTKEYmessage to the message queue of the window with which the hot key is associated. If the hot key is not associated with a window, then the WM_HOTKEY message is posted to the thread associated with the hot key. This function cannot associate a hot key with a window created by another thread. RegisterHotKey fails if the keystrokes specified for the hot key have already been registered by another hot key. If the window identified by the hWnd parameter already registered a hot key with the same identifier as that specified by the id parameter, the new values for the fsModifiers and vk parameters replace the previously specified values for these parameters. Windows NT4 and Windows 2000/XP: The F12 key is reserved for use by the debugger at all times, so it should not be registered as a hot key. Even when you are not debugging an application, F12 is reserved in case a kernel-mode debugger or a just-in-time debugger is resident. An application must specify an id value in the range 0x0000 through 0xBFFF. A shared DLL must specify a value in the range 0xC000 through 0xFFFF (the range returned by the GlobalAddAtom function). To avoid conflicts with hot-key identifiers defined by other shared DLLs, a DLL should use the GlobalAddAtom function to obtain the hot-key identifier.Function Information Minimum DLL Version user32.dll
Header Declared in Winuser.h, include Windows.h
Import library User32.lib
Minimum operating systems Windows 95, Windows NT 3.1
Unicode Implemented as Unicode version.See Also Keyboard Input, GlobalAddAtom, UnregisterHotKey, WM_HOTKEY------------------------------------
红字部分到底什么意思?新值代替旧值?还是反过来?根据我的测试,好像不是谁替代谁,而是2个都有效,
比如我先将ALT+F4注册成当前窗口的id为0x1234的热键,然后再将ALT+F3注册成当前窗口的id为0x1234的热键,这时,我按ALT+F3和ALT+F4均触发了WM_HOTKEY消息。
不知道是msdn的说明有bug,还是我的理解有bug?
HWND hWnd,
int id,
UINT fsModifiers,
UINT vk
);Parameters hWnd
[in] Handle to the window that will receive WM_HOTKEY messages generated by the hot key. If this parameter is NULL, WM_HOTKEY messages are posted to the message queue of the calling thread and must be processed in the message loop.
id
[in] Specifies the identifier of the hot key. If a hot key already exists with the same hWnd and id parameters, it is replaced by the new hot key. If the hWnd parameter is NULL, then the hot key is associated with the current thread rather than with a particular window.
fsModifiers
[in] Specifies keys that must be pressed in combination with the key specified by the uVirtKey parameter in order to generate the WM_HOTKEY message. The fsModifiers parameter can be a combination of the following values. MOD_ALT
Either ALT key must be held down.
MOD_CONTROL
Either CTRL key must be held down.
MOD_SHIFT
Either SHIFT key must be held down.
MOD_WIN
Either WINDOWS key was held down. These keys are labeled with the Microsoft Windows logo. Keyboard shortcuts that involve the WINDOWS key are reserved for use by the operating system. vk
[in] Specifies the virtual-key code of the hot key.Return Value If the function succeeds, the return value is nonzero. If the function fails, the return value is zero. To get extended error information, call GetLastError.Res When a key is pressed, the system looks for a match against all hot keys. Upon finding a match, the system posts the WM_HOTKEYmessage to the message queue of the window with which the hot key is associated. If the hot key is not associated with a window, then the WM_HOTKEY message is posted to the thread associated with the hot key. This function cannot associate a hot key with a window created by another thread. RegisterHotKey fails if the keystrokes specified for the hot key have already been registered by another hot key. If the window identified by the hWnd parameter already registered a hot key with the same identifier as that specified by the id parameter, the new values for the fsModifiers and vk parameters replace the previously specified values for these parameters. Windows NT4 and Windows 2000/XP: The F12 key is reserved for use by the debugger at all times, so it should not be registered as a hot key. Even when you are not debugging an application, F12 is reserved in case a kernel-mode debugger or a just-in-time debugger is resident. An application must specify an id value in the range 0x0000 through 0xBFFF. A shared DLL must specify a value in the range 0xC000 through 0xFFFF (the range returned by the GlobalAddAtom function). To avoid conflicts with hot-key identifiers defined by other shared DLLs, a DLL should use the GlobalAddAtom function to obtain the hot-key identifier.Function Information Minimum DLL Version user32.dll
Header Declared in Winuser.h, include Windows.h
Import library User32.lib
Minimum operating systems Windows 95, Windows NT 3.1
Unicode Implemented as Unicode version.See Also Keyboard Input, GlobalAddAtom, UnregisterHotKey, WM_HOTKEY------------------------------------
红字部分到底什么意思?新值代替旧值?还是反过来?根据我的测试,好像不是谁替代谁,而是2个都有效,
比如我先将ALT+F4注册成当前窗口的id为0x1234的热键,然后再将ALT+F3注册成当前窗口的id为0x1234的热键,这时,我按ALT+F3和ALT+F4均触发了WM_HOTKEY消息。
不知道是msdn的说明有bug,还是我的理解有bug?
{
switch (m.Msg)
{
case 0x0312://WM_HOTKEY
if (m.WParam.ToInt32() == 0x1234)
{
MessageBox.Show("");
}
break;
}
base.WndProc(ref m);
}
//这个是注册热键的代码
RegisterHotKey(Handle, 0x1234, 1, 0x73);//ALT+F4
RegisterHotKey(Handle, 0x1234, 1, 0x72);//ALT+F3
public partial class Form1 : Form
{
[DllImport("User32.dll")]
public static extern bool RegisterHotKey(IntPtr hWnd, int id, uint Modifiers, uint vk);
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
RegisterHotKey(Handle, 0x1234, 1, 0x73);//ALT+F4
RegisterHotKey(Handle, 0x1234, 1, 0x72);//ALT+F3
}
protected override void WndProc(ref Message m)
{
switch (m.Msg)
{
case 0x0312://WM_HOTKEY
if (m.WParam.ToInt32() == 0x1234)
{
MessageBox.Show("");
}
break;
}
base.WndProc(ref m);
}
}
我的是vista ultimate(32位) + vs2008(2.0框架),再检查了下,还是2个都弹出MessageBox,奇怪了。
{
switch (m.Msg)
{
case 0x0312: //这个是window消息定义的 注册的热键消息
if(m.WParam.ToString().Equals("888")) //如果是我们注册的那個热键
MessageBox.Show("");
break;
}
base.WndProc (ref m);
}