0

To record the key taps I install the hook as :

BOOL WINAPI installHook(HINSTANCE hinstDLL, DWORD fwdReason, LPVOID lpvReserved) {
handleKeyboardHook = SetWindowsHookEx(WH_KEYBOARD_LL, LowLevelKeyboardProc, hinstDLL, 0);
MSG msg;

while(GetMessage(&msg, NULL, 0, 0)){
  TranslateMessage(&msg);
  DispatchMessage(&msg);
}
return msg.wParam;
}

static LRESULT CALLBACK LowLevelKeyboardProc(int nCode, WPARAM wParam, LPARAM lParam) {
  // write here
}

Is there any way I can know the application name where the keys are currently being tapped ? Like I have opened notepad an writing something , can I get the name of the application which is notepad along with the key taps ? Same goes for some other application like mozilla firefox.

4

1 回答 1

2

你的钩子内部应该是这样的:

static LRESULT CALLBACK LowLevelKeyboardProc(int nCode, WPARAM wParam, LPARAM lParam)
{
    // if it is not a keydown event, continue the chain
    if(HC_ACTION != nCode || WM_KEYDOWN != wParam)
        return CallNextHookEx(0, nCode, wParam, lParam);

    const KBDLLHOOKSTRUCT* messageInfo = reinterpret_cast<const KBDLLHOOKSTRUCT*>(lParam);

    // add more code here...

    // tell Windows we processed the hook
    return 1;
}

messageinfo.vkCode将包含您正在寻找的关键代码。这些代码的官方列表位于:http: //msdn.microsoft.com/en-us/library/windows/desktop/dd375731%28v=vs.85%29.aspx

键通常被输入到前台窗口中(尽管有时会发生奇怪的窗口布局)。您可以像这样获取前景窗口的标题:

TCHAR title[100]; // increase size for longer titles
GetWindowText(GetForegroundWindow(), title, 100);

如果您想获取程序的名称,请使用:

TCHAR title[100]; // increase size for longer program names
GetWindowModuleFileName(GetForegroundWindow(), title, 100);

并且,请记住添加错误检查并检查文档。

于 2012-10-13T05:13:19.360 回答