<i id='UFHjL'><tr id='UFHjL'><dt id='UFHjL'><q id='UFHjL'><span id='UFHjL'><b id='UFHjL'><form id='UFHjL'><ins id='UFHjL'></ins><ul id='UFHjL'></ul><sub id='UFHjL'></sub></form><legend id='UFHjL'></legend><bdo id='UFHjL'><pre id='UFHjL'><center id='UFHjL'></center></pre></bdo></b><th id='UFHjL'></th></span></q></dt></tr></i><div id='UFHjL'><tfoot id='UFHjL'></tfoot><dl id='UFHjL'><fieldset id='UFHjL'></fieldset></dl></div>

<legend id='UFHjL'><style id='UFHjL'><dir id='UFHjL'><q id='UFHjL'></q></dir></style></legend>

<small id='UFHjL'></small><noframes id='UFHjL'>

  • <tfoot id='UFHjL'></tfoot>

      • <bdo id='UFHjL'></bdo><ul id='UFHjL'></ul>

      1. C++ Win32 键盘事件

        时间:2023-08-01
          <tbody id='ByK9L'></tbody>

          <small id='ByK9L'></small><noframes id='ByK9L'>

          <legend id='ByK9L'><style id='ByK9L'><dir id='ByK9L'><q id='ByK9L'></q></dir></style></legend>
            <bdo id='ByK9L'></bdo><ul id='ByK9L'></ul>
              <tfoot id='ByK9L'></tfoot>
              • <i id='ByK9L'><tr id='ByK9L'><dt id='ByK9L'><q id='ByK9L'><span id='ByK9L'><b id='ByK9L'><form id='ByK9L'><ins id='ByK9L'></ins><ul id='ByK9L'></ul><sub id='ByK9L'></sub></form><legend id='ByK9L'></legend><bdo id='ByK9L'><pre id='ByK9L'><center id='ByK9L'></center></pre></bdo></b><th id='ByK9L'></th></span></q></dt></tr></i><div id='ByK9L'><tfoot id='ByK9L'></tfoot><dl id='ByK9L'><fieldset id='ByK9L'></fieldset></dl></div>

                  本文介绍了C++ Win32 键盘事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

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

                  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 使用率,人们建议让输入基于键事件.

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

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

                  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.

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

                  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.

                  我的两个主要问题是:

                  • 我可以使用 Win32 API 编写基于事件的控制台按键记录器吗?如果没有,我有哪些选择?

                  • 有没有人有任何参考网站可以帮助我了解如何基于事件捕获击键?

                  如果需要更多信息,我在 Windows XP 下使用代码块和 GCC 编译器.

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

                  推荐答案

                  按键记录器应用程序使用诸如 Win32 钩子.具体来说,您需要设置一个 WH_KEYBOARD 钩子.

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

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

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

                  为了了解钩子过程的样子,我发布了我个人实用程序中的一个片段.

                  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_USERControl PanelDesktop 
                  
                      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;
                  }
                  

                  这篇关于C++ Win32 键盘事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:如何枚举/列出 Windows XP 中所有已安装的应用程序 下一篇:Win32 C/C++ 从内存缓冲区加载图像

                  相关文章

                  最新文章

                  <tfoot id='5roOg'></tfoot>

                  1. <i id='5roOg'><tr id='5roOg'><dt id='5roOg'><q id='5roOg'><span id='5roOg'><b id='5roOg'><form id='5roOg'><ins id='5roOg'></ins><ul id='5roOg'></ul><sub id='5roOg'></sub></form><legend id='5roOg'></legend><bdo id='5roOg'><pre id='5roOg'><center id='5roOg'></center></pre></bdo></b><th id='5roOg'></th></span></q></dt></tr></i><div id='5roOg'><tfoot id='5roOg'></tfoot><dl id='5roOg'><fieldset id='5roOg'></fieldset></dl></div>

                    1. <small id='5roOg'></small><noframes id='5roOg'>

                      <legend id='5roOg'><style id='5roOg'><dir id='5roOg'><q id='5roOg'></q></dir></style></legend>
                        <bdo id='5roOg'></bdo><ul id='5roOg'></ul>