none
软件通过代码注册了快捷键后。就拦截了整个系统。其他软件怎样继续使用呢。 RRS feed

  • 问题

  • 软件通过代码注册了快捷键后。就拦截了整个系统。其他软件怎样继续使用呢。

      public class Win32
        {
            [DllImport("User32.Dll")]
            public static extern void SetWindowText(int h, String s);
    
            /// <summary>
            /// 如果函数执行成功,返回值不为0。
            /// 如果函数执行失败,返回值为0。要得到扩展错误信息,调用GetLastError。.NET方法:Marshal.GetLastWin32Error()
            /// </summary>
            /// <param name="hWnd">要定义热键的窗口的句柄</param>
            /// <param name="id">定义热键ID(不能与其它ID重复)  </param>
            /// <param name="fsModifiers">标识热键是否在按Alt、Ctrl、Shift、Windows等键时才会生效</param>
            /// <param name="vk">定义热键的内容,WinForm中可以使用Keys枚举转换,
            /// WPF中Key枚举是不正确的,应该使用System.Windows.Forms.Keys枚举,或者自定义正确的枚举或int常量</param>
            /// <returns></returns>
            [DllImport("user32.dll", SetLastError = true)]
            public static extern bool RegisterHotKey(
                IntPtr hWnd,
                int id,
                KeyModifiers fsModifiers,
                int vk
                );
    
            /// <summary>
            /// 取消注册热键
            /// </summary>
            /// <param name="hWnd">要取消热键的窗口的句柄</param>
            /// <param name="id">要取消热键的ID</param>
            /// <returns></returns>
            [DllImport("user32.dll", SetLastError = true)]
            public static extern bool UnregisterHotKey(
                IntPtr hWnd,
                int id
                );
    
            /// <summary>
            /// 向全局原子表添加一个字符串,并返回这个字符串的唯一标识符,成功则返回值为新创建的原子ID,失败返回0
            /// </summary>
            /// <param name="lpString"></param>
            /// <returns></returns>
            [DllImport("kernel32", SetLastError = true)]
            public static extern short GlobalAddAtom(string lpString);
    
            [DllImport("kernel32", SetLastError = true)]
            public static extern short GlobalDeleteAtom(short nAtom);
    
            /// <summary>
            /// 定义了辅助键的名称(将数字转变为字符以便于记忆,也可去除此枚举而直接使用数值)
            /// </summary>
            [Flags()]
            public enum KeyModifiers
            {
                None = 0,
                Alt = 1,
                Ctrl = 2,
                Shift = 4,
                WindowsKey = 8
            }
            /// <summary>
            /// 热键的对应的消息ID
            /// </summary>
            public const int WmHotkey = 0x312;
        }

    我希望的是快捷键能获取到,但是其他软件也能继续使用

    2020年7月6日 3:13

全部回复

  • #region 响应快捷键事件
            /// <summary>
            /// 响应快捷键事件
            /// </summary>
            /// <param name="hwnd"></param>
            /// <param name="msg"></param>
            /// <param name="wParam"></param>
            /// <param name="lParam"></param>
            /// <param name="handled"></param>
            /// <returns></returns>
            private IntPtr MainWindowProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
            {
              
                switch (msg)
                {
                    case Win32.WmHotkey:
                        {
                            int sid = wParam.ToInt32();
                            if (sid==hotKeyDic["A"])
                            {
                                Messenger.Default.Send<Key>(Key.A, "KeyDownButton");
                            }
                          else if (sid==hotKeyDic["Space"])
                            {
                                Messenger.Default.Send<Key>(Key.Space, "KeyDownButton");
                            }
                           else if (sid==hotKeyDic["Q"])
                            {
                                Messenger.Default.Send<Key>(Key.Q, "KeyDownButton");
                            }
                           else if (sid==hotKeyDic["P"])
                            {
                                Messenger.Default.Send<Key>(Key.P, "KeyDownButton");
                            }
                            else if (sid == hotKeyDic["1"])
                            {
                                Messenger.Default.Send<Key>(Key.D1, "KeyDownButton");
                            }
                            else if (sid == hotKeyDic["2"])
                            {
                                Messenger.Default.Send<Key>(Key.D2, "KeyDownButton");
                            }
                            else if (sid == hotKeyDic["3"])
                            {
                                Messenger.Default.Send<Key>(Key.D3, "KeyDownButton");
                            }
                            else if (sid == hotKeyDic["0"])
                            {
                                Messenger.Default.Send<Key>(Key.D0, "KeyDownButton");
                            }
                            else if (sid == hotKeyDic["4"])
                            {
                                Messenger.Default.Send<Key>(Key.D4, "KeyDownButton");
                            }
                            else if (sid == hotKeyDic["5"])
                            {
                                Messenger.Default.Send<Key>(Key.D5, "KeyDownButton");
                            }
                            else if (sid == hotKeyDic["6"])
                            {
                                Messenger.Default.Send<Key>(Key.D6, "KeyDownButton");
                            }
                            else if (sid == hotKeyDic["Z"])
                            {
                                Messenger.Default.Send<Key>(Key.Z, "KeyDownButton");
                            }
                            else if (sid == hotKeyDic["M"])
                            {
                                Messenger.Default.Send<Key>(Key.M, "KeyDownButton");
                            }
                            else if (sid == hotKeyDic["Add"])
                            {
                                Messenger.Default.Send<Key>(Key.Add, "KeyDownButton");
                            }
                            else if (sid == hotKeyDic["Subtract"])
                            {
                                Messenger.Default.Send<Key>(Key.Subtract, "KeyDownButton");
                            }
                            else if (sid == hotKeyDic["Oemplus"])
                            {
                                Messenger.Default.Send<Key>(Key.OemPlus, "KeyDownButton");
                            }
                            else if (sid == hotKeyDic["OemMinus"])
                            {
                                Messenger.Default.Send<Key>(Key.OemMinus, "KeyDownButton");
                            }
                            else if (sid == hotKeyDic["K"])
                            {
                                Messenger.Default.Send<Key>(Key.K, "KeyDownButton");
                            }
                            else if (sid == hotKeyDic["S"])
                            {
                                Messenger.Default.Send<Key>(Key.S, "KeyDownButton");
                            }
                            else if (sid == hotKeyDic["7"])
                            {
                                Messenger.Default.Send<Key>(Key.D7, "KeyDownButton");
                            }
                            else if (sid == hotKeyDic["8"])
                            {
                                Messenger.Default.Send<Key>(Key.D8, "KeyDownButton");
                            }
                            else if (sid == hotKeyDic["L"])
                            {
                                Messenger.Default.Send<Key>(Key.L, "KeyDownButton");
                            }
                            else  if (sid==hotKeyDic["Left"])
                            {
                                Messenger.Default.Send<Key>(Key.Left, "KeyDownButton");
                            }
                            else if (sid==hotKeyDic["Right"])
                            {
                                Messenger.Default.Send<Key>(Key.Right, "KeyDownButton");
                            }
                            handled = true;
                            break;
                        }
                }
               
                return IntPtr.Zero;
            } 
            #endregion
    就是这个地方拦截后,整个系统其他软件就没法获取到键位了,有没有什么办法,我的软件能取到,但是别的软件也可以。不然其他软件打字都没有办法打了。
    2020年7月6日 3:14
  • 这个是注册的代码

    #region 注册热键
            /// <summary>
            /// 注册热键
            /// </summary>
            /// <param name="obj"></param>
            private void RegisterHotKey(TestFunction obj)
            {
                try
                {
                    switch (obj)
                    {
                        case TestFunction.Stroop注意力集中:
                            Win32.RegisterHotKey(wpfHwnd, hotKeyDic["Left"], Win32.KeyModifiers.None, (int)System.Windows.Forms.Keys.Left);
                            Win32.RegisterHotKey(wpfHwnd, hotKeyDic["Right"], Win32.KeyModifiers.None, (int)System.Windows.Forms.Keys.Right);
                            break;
                    }
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            } 
            #endregion

    这个是取消注册的代码

    IntPtr wpfHwnd;
            /// <summary>
            /// 注册快捷集合
            /// </summary>
            readonly Dictionary<string, short> hotKeyDic = new Dictionary<string, short>();
            #region 取消注册热键
            /// <summary>
            /// 取消注册热键
            /// </summary>
            /// <param name="obj"></param>
            private void UnregisterHotKey(TestFunction obj)
            {
                 switch (obj)
                {
                    case TestFunction.Stroop注意力集中:
                        Win32.UnregisterHotKey(wpfHwnd, hotKeyDic["Left"]);
                        Win32.UnregisterHotKey(wpfHwnd, hotKeyDic["Right"]);
                        break;
                }
            } 
            #endregion

    2020年7月6日 3:16
  • 你好,

    你是在你的WPF程序中用以上代码注册此应用程序的快捷键后,导致电脑其他应用程序无法使用?如果是的话,你可以提供一个可以重现问题的demo吗?这样便于我们更快得分析问题。

    谢谢

    Daisy Tian


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    2020年7月7日 8:41
  • 使用钩子,自己用完之后传递给系统

    https://www.cnblogs.com/Johness/archive/2012/12/28/2837977.html

    2020年7月12日 12:45
  • 你好,

    你是在你的WPF程序中用以上代码注册此应用程序的快捷键后,导致电脑其他应用程序无法使用?如果是的话,你可以提供一个可以重现问题的demo吗?这样便于我们更快得分析问题。

    谢谢

    Daisy Tian


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    n你好,代码已放在github上

    https://github.com/pengxiaobo123/kuaijiejian

    2020年7月12日 16:54