<small id='118b9'></small><noframes id='118b9'>

      <legend id='118b9'><style id='118b9'><dir id='118b9'><q id='118b9'></q></dir></style></legend>

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

        <tfoot id='118b9'></tfoot>

        在控制台中捕获没有焦点的击键

        时间:2023-08-27

        <small id='5IBQd'></small><noframes id='5IBQd'>

          <tbody id='5IBQd'></tbody>

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

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

                • 本文介绍了在控制台中捕获没有焦点的击键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我知道有一个关于 Windows 窗体的问题,但它在控制台中不起作用,或者至少我无法让它起作用.即使控制台没有焦点,我也需要捕获按键.

                  I know there is a question for Windows Forms but it doesn't work in the console, or at least I couldn't get it to work. I need to capture key presses even though the console doesn't have focus.

                  推荐答案

                  您也可以在控制台应用程序中创建全局键盘挂钩.

                  You can create a global keyboard hook in a console application, too.

                  这是完整的工作代码:
                  https://docs.microsoft.com/en-us/archive/blogs/toub/low-level-keyboard-hook-in-c

                  您创建了一个控制台应用程序,但必须添加对System.Windows.Forms 的引用才能使其工作.控制台应用程序没有理由不能引用该 dll.

                  You create a console application, but must add a reference to System.Windows.Forms for this to work. There's no reason a console app can't reference that dll.

                  我刚刚使用此代码创建了一个控制台应用程序,并验证了它是否按下了每个键,无论控制台应用程序是否具有焦点.

                  I just created a console app using this code and verified that it gets each key pressed, whether or not the console app has the focus.

                  编辑

                  主线程将运行 Application.Run() 直到应用程序退出,例如通过调用 Application.Exit().完成其他工作的最简单方法是启动一个新任务来执行该工作.这是执行此操作的链接代码中 Main() 的修改版本

                  The main thread will run Application.Run() until the application exits, e.g. via a call to Application.Exit(). The simplest way to do other work is to start a new Task to perform that work. Here's a modified version of Main() from the linked code that does this

                  public static void Main()
                  {
                      var doWork = Task.Run(() =>
                          {
                              for (int i = 0; i < 20; i++)
                              {
                                  Console.WriteLine(i);
                                  Thread.Sleep(1000);
                              }
                              Application.Exit(); // Quick exit for demonstration only.  
                          });
                  
                      _hookID = SetHook(_proc);
                  
                      Application.Run();
                  
                      UnhookWindowsHookEx(_hookID);
                  }
                  

                  注意

                  可能提供退出控制台应用程序的方法,例如根据您的特定需求按下特殊组合键时.在

                  Possibly provide a means to exit the Console app, e.g. when a special key combo is pressed depending on your specific needs. In the

                  这篇关于在控制台中捕获没有焦点的击键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:为什么一个基本的单线程 C# 控制台应用程序需要 下一篇:从控制台应用程序返回字符串

                  相关文章

                  最新文章

                  1. <tfoot id='8jljD'></tfoot>
                    1. <small id='8jljD'></small><noframes id='8jljD'>

                        <bdo id='8jljD'></bdo><ul id='8jljD'></ul>
                    2. <legend id='8jljD'><style id='8jljD'><dir id='8jljD'><q id='8jljD'></q></dir></style></legend>
                    3. <i id='8jljD'><tr id='8jljD'><dt id='8jljD'><q id='8jljD'><span id='8jljD'><b id='8jljD'><form id='8jljD'><ins id='8jljD'></ins><ul id='8jljD'></ul><sub id='8jljD'></sub></form><legend id='8jljD'></legend><bdo id='8jljD'><pre id='8jljD'><center id='8jljD'></center></pre></bdo></b><th id='8jljD'></th></span></q></dt></tr></i><div id='8jljD'><tfoot id='8jljD'></tfoot><dl id='8jljD'><fieldset id='8jljD'></fieldset></dl></div>