C++ Win32 键盘事件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1437158/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 19:58:04  来源:igfitidea点击:

C++ Win32 keyboard events

c++winapievent-handling

提问by Ferruccio

I am working on my keystroke logger for personal interest and asked a question related to this about yesterday; While loop using a lot of CPU.

为了个人兴趣,我正在研究我的击键记录器,昨天问了一个与此相关的问题;使用大量 CPU 的 while 循环

The issue with the program was that it took too much CPU Usage, and people have suggested to make the inputs key-event based.

该程序的问题在于它占用了太多的 CPU 使用率,人们建议使输入基于键事件。

Since I'm new to the Win32 API, I try to look for references and tutorials that will tell me how to create keyboard inputs as event-based, rather than poll based. But the problems is I could not found any solid examples or references, as it was quite difficult to understand for a complete newbie.

由于我是 Win32 API 的新手,因此我尝试寻找参考资料和教程,它们将告诉我如何将键盘输入创建为基于事件而不是基于轮询。但问题是我找不到任何可靠的例子或参考资料,因为对于一个完整的新手来说很难理解。

Most of them mentioned that the event-based programming resided in GUI application, yet I want this keystroke logger application to be a console application.

他们中的大多数人提到基于事件的编程驻留在 GUI 应用程序中,但我希望这个击键记录器应用程序是一个控制台应用程序。

My two main questions from all this is:

我对这一切的两个主要问题是:

  • Can I write a event-based console keystroke logger with the Win32 API? If not, what are my options?
  • 我可以使用 Win32 API 编写基于事件的控制台击键记录器吗?如果没有,我有什么选择?

and

  • Does anyone have any reference websites that will help me understand how to capture keystrokes event-based?
  • 有没有人有任何参考网站可以帮助我了解如何捕获基于事件的击键?

If additional information is needed, I am using Code Blocks under Windows XP with a GCC compiler.

如果需要其他信息,我将在 Windows XP 下使用带有 GCC 编译器的代码块。

回答by Nick Dandoulakis

Key logger applications use mechanisms such as Win32 Hooks. Specifically you need to set a WH_KEYBOARDhook.

按键记录器应用程序使用诸如Win32 Hooks 之类的机制。具体来说,你需要设置一个WH_KEYBOARD钩子。

There are move advanced techniques, like creating your own keyboard driver but for a start hooks are a good choice.

有一些高级技术,比如创建自己的键盘驱动程序,但对于开始钩子是一个不错的选择。

Edit:To get an idea of how a hook procedure looks like, I post a fragment from my personal utility.

编辑:为了了解钩子过程的样子,我从我的个人实用程序中发布了一个片段。

// ...
thehook = SetWindowsHookEx( WH_KEYBOARD_LL, hook_proc, hwnd, 0 );
// ...

/**
 *
 *  wParam, one of the: WM_KEYDOWN, WM_KEYUP, WM_SYSKEYDOWN, or WM_SYSKEYUP
    lParam: pointer to a KBDLLHOOKSTRUCT structure

    (*) "The hook procedure should process a message in less time than the
    data entry specified in the LowLevelHooksTimeout value in the following registry key: 
    HKEY_CURRENT_USER\Control Panel\Desktop 

    The value is in milliseconds. If the hook procedure does not 
    return during this interval, the system will pass the message to the next hook."

 *
 */
LRESULT CALLBACK
hook_proc( int code, WPARAM wParam, LPARAM lParam )
{
  static long ctrl_cnt = 0;
  static bool mmode = false;
  static DWORD time;

  KBDLLHOOKSTRUCT*  kbd = (KBDLLHOOKSTRUCT*)lParam;

  if (  code < 0
  ||   (kbd->flags & 0x10) // ignore injected events
     ) return CallNextHookEx( thehook, code, wParam, lParam );

  long ret = 1; // by default I swallow the keys
  if (  mmode  ) { // macro mode is ON
    if (  WM_KEYDOWN == wParam  )
      PostMessage(mainwnd, WM_MCR_ACCUM, kbd->vkCode, 0);

    if (  WM_KEYUP == wParam  )
      switch (kbd->vkCode) {
        case VK_ESCAPE:
          mmode = false;
          keys.removeall();
          PostMessage(mainwnd, WM_MCR_HIDE, 0, 0);
          break;

        case VK_RETURN:
          PostMessage(mainwnd, WM_MCR_EXEC, 0, 0);
          break;

        case VK_LCONTROL:
          mmode = false;
          PostMessage(mainwnd, WM_MCR_HIDE, 0, 0);
          PostMessage(mainwnd, WM_MCR_EXEC, 0, 0);
          break;
      }

    /* Which non printable keys allow passing? */
    switch( kbd->vkCode ) {
      case VK_LCONTROL:
      case VK_CAPITAL:
      case VK_LSHIFT:
      case VK_RSHIFT:
        ret = CallNextHookEx( thehook, code, wParam, lParam );
    }
  }
  else { // macro mode is OFF
    /* Ctrl pressed */
    if (  kbd->vkCode == VK_LCONTROL && WM_KEYDOWN == wParam  ) {
      ctrl_cnt = 1;
      time = kbd->time;
    }

    /* Prevent ctrl combinations to activate macro mode */
    if (  kbd->vkCode != VK_LCONTROL  )
      ctrl_cnt = 0;

    /* Ctrl released */
    if (  ctrl_cnt == 1 && WM_KEYUP == wParam  ) {
      if (  kbd->time - time > 40  ) {
        mmode = true;
        PostMessage(mainwnd, WM_MCR_SHOW, 0, 0);
      }
    }

    ret = CallNextHookEx( thehook, code, wParam, lParam ); // let it pass
  }

  return ret;
}

回答by Ferruccio

Take a look at the SetWindowsHookExAPI.

查看SetWindowsHookExAPI。

You can do this is a console app as well as a windowed app. You will need to put your keyboard hook in a DLL so that you can capture keystrokes in processes other than your own.

您可以在控制台应用程序和窗口应用程序中执行此操作。您需要将键盘挂钩放在 DLL 中,以便您可以在您自己的进程以外的进程中捕获击键。

There is some sample code here.

有一些示例代码在这里