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

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

          <bdo id='uRVHH'></bdo><ul id='uRVHH'></ul>
      1. 在没有焦点的情况下捕获击键

        时间:2023-09-27

          1. <tfoot id='bVVXY'></tfoot>
                <bdo id='bVVXY'></bdo><ul id='bVVXY'></ul>

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

                    <tbody id='bVVXY'></tbody>
                  <i id='bVVXY'><tr id='bVVXY'><dt id='bVVXY'><q id='bVVXY'><span id='bVVXY'><b id='bVVXY'><form id='bVVXY'><ins id='bVVXY'></ins><ul id='bVVXY'></ul><sub id='bVVXY'></sub></form><legend id='bVVXY'></legend><bdo id='bVVXY'><pre id='bVVXY'><center id='bVVXY'></center></pre></bdo></b><th id='bVVXY'></th></span></q></dt></tr></i><div id='bVVXY'><tfoot id='bVVXY'></tfoot><dl id='bVVXY'><fieldset id='bVVXY'></fieldset></dl></div>
                  <legend id='bVVXY'><style id='bVVXY'><dir id='bVVXY'><q id='bVVXY'></q></dir></style></legend>
                  本文介绍了在没有焦点的情况下捕获击键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  例如使用 winamp(至少在 Windows 上),您可以在后台使用 winamp 全屏播放游戏,并使用媒体按钮* 来控制声音.Winamp 不需要获得焦点,让游戏继续全屏.

                  E.g. with winamp (on Windows at least), you can play a game fullscreen with winamp in the background, and use the media buttons* to control the sound. Winamp doesn't need to get focus, allowing the game to continue fullscreen.

                  我更喜欢用 Java 编写它,但这可能行不通(在 Java afaik 中捕获没有焦点的击键已经很困难了),所以任何 C# 解决方案也可以.

                  I'd prefer to write this in Java but that probably isn't going to work (capturing keystrokes without focus is already difficult in Java afaik), so any C# solution is also fine.

                  所以基本问题是:如何捕捉没有焦点的击键?

                  So the basic question is: how to capture keystrokes without focus?

                  *) 我相信后退/前进/停止/邮件/搜索/收藏夹/web/home"按钮被称为媒体按钮,但欢迎使用更好的名称:).

                  *) I believe the 'back/forward/stop/mail/search/favorites/web/home' buttons are called media buttons, but a better name would be welcome :).

                  推荐答案

                  低级 windows hooks 是一种方法.这是一篇 文章,这里是来自 MSDN.

                  Low-Level windows hooks is one way to do it. Here is one article and here is a little more info from MSDN.

                  这是该代码的部分视图:

                  This is a partial view of what that code can look like:

                      private IntPtr LowLevelKeyboardHook(int nCode, WindowsMessages wParam, [In] KBDLLHOOKSTRUCT lParam)
                      {
                          bool callNext = true;
                  
                          bool isKeyDown = (wParam == WindowsMessages.KEYDOWN || wParam == WindowsMessages.SYSKEYDOWN);
                          bool isKeyUp = (wParam == WindowsMessages.KEYUP || wParam == WindowsMessages.SYSKEYUP);
                  
                          if ((nCode >= 0) && (isKeyDown || isKeyUp))
                          {
                              // the virtual key codes and the winforms Keys have the same enumeration
                              // so we can freely cast back and forth between them
                              Keys key = (Keys)lParam.vkCode;
                  
                              // Do your other processing here...
                          }
                  
                          // if any handler returned false, trap the message
                          return (callNext) ? User32.CallNextHookEx(_mainHook, nCode, wParam, lParam) : _nullNext;
                      }
                  
                  
                      /// <summary>
                      /// Registers the user's LowLevelKeyboardProc with the system in order to
                      /// intercept any keyboard events before processed in the regular fashion.
                      /// This can be used to log all keyboard events or ignore them.
                      /// </summary>
                      /// <param name="hook">Callback function to call whenever a keyboard event occurs.</param>
                      /// <returns>The IntPtr assigned by the Windows's sytem that defines the callback.</returns>
                      private IntPtr RegisterLowLevelHook(LowLevelKeyboardProc hook)
                      {
                          IntPtr handle = IntPtr.Zero;
                  
                          using (Process currentProcess = Process.GetCurrentProcess())
                          using (ProcessModule currentModule = currentProcess.MainModule)
                          {
                              IntPtr module = Kernel32.GetModuleHandle(currentModule.ModuleName);
                              handle = User32.SetWindowsHookEx(HookType.KEYBOARD_LL, hook, module, 0);
                          }
                  
                          return handle;
                      }
                  
                      /// <summary>
                      /// Unregisters a previously registered callback from the low-level chain.
                      /// </summary>
                      /// <param name="hook">IntPtr previously assigned to the low-level chain.
                      /// Users should have stored the value given by 
                      /// <see cref="Drs.Interop.Win32.LowLevelKeyboard.RegisterLowLevelHook"/>,
                      /// and use that value as the parameter into this function.</param>
                      /// <returns>True if the hook was removed, false otherwise.</returns>
                      private bool UnregisterLowLevelHook(IntPtr hook)
                      {
                          return User32.UnhookWindowsHookEx(hook);
                      }
                  

                  只需实现所需的所有 P/Invoke 声明,它就可以工作.我在我的应用程序中使用了这种方法,并且效果很好.

                  Just implement all the P/Invoke declarations needed, and it should work. I use this approach in my application and it works fine.

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

                  上一篇:如何将布局放置在 android 屏幕键盘的正上方? 下一篇:使用键盘滚动其弹出列表时如何使 JComboBox 所选项

                  相关文章

                  最新文章

                  1. <legend id='ouq7N'><style id='ouq7N'><dir id='ouq7N'><q id='ouq7N'></q></dir></style></legend>

                      <tfoot id='ouq7N'></tfoot>

                      • <bdo id='ouq7N'></bdo><ul id='ouq7N'></ul>
                    1. <small id='ouq7N'></small><noframes id='ouq7N'>

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