代码如下, build成一个console application运行没问题:
#include "stdafx.h"
#include
#include
int _tmain(int argc, _TCHAR* argv[])
{
MSG msg = { 0 };
LPVOID lpMsgBuf;
DWORD dw;
ATOM idHotkey = GlobalAddAtom(L"MyHotkey");
if (RegisterHotKey(NULL, idHotkey, MOD_CONTROL | MOD_ALT | MOD_NOREPEAT, 0x44)) //0x44 is 'd'
{
printf("Hotkey 'CTRL+ALT+d' registered.\n");
}
else
{
dw = GetLastError();
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
dw,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR)&lpMsgBuf,
0, NULL);
printf("Hotkey 'CTRL+ALT+d' register failed. Error msg: %s\n", lpMsgBuf);
return 1;
}
while (GetMessage(&msg, NULL, 0, 0) != 0)
{
if (msg.message == WM_HOTKEY)
{
UINT fuModifiers = (UINT)LOWORD(msg.lParam);
UINT uVirtKey = (UINT)HIWORD(msg.lParam);
switch (fuModifiers)
{
case MOD_CONTROL | MOD_ALT:
switch (uVirtKey)
{
case 0x44: // Ctrl+Alt+d
printf("Ctrl+Alt+d\n");
break;
}
break;
}
}
}
UnregisterHotKey(NULL, idHotkey);
printf("Hotkey 'CTRL+ALT+d' unregistered.\n");
GlobalDeleteAtom(idHotkey);
return 0;
}
但同样的代码放在一个ATL的service的线程里, 注册就会失败, 提示需要interactive windows station, 这问题有办法解决吗? 求各位大牛指教!!!