使用远程桌面后键盘挂钩停止工作

发布于 2024-10-18 04:12:44 字数 3350 浏览 0 评论 0原文

我遇到以下问题:

在启动任何远程桌面会话后,我的全局键盘挂钩停止工作,即使会话已关闭,挂钩也根本不起作用。

这是我的钩子代码

public class KeyboardHooker : IDisposable
    {
        private IntPtr _hhook;
        private static User32.LowLevelKeyboardProc _delegate;

        public KeyboardHooker()
        {
            _delegate = new User32.LowLevelKeyboardProc(HookCallback);
        }

        public void SetHook()
        {
            if (IsHookSetted)
                return;

            using (Process curProcess = Process.GetCurrentProcess())
            using (ProcessModule curModule = curProcess.MainModule)
            {
                _hhook = User32.SetWindowsHookEx(User32.WH_KEYBOARD_LL, _delegate, Kernel32.GetModuleHandle(curModule.ModuleName), 0);
            }
        }

        public void ReleaseHook()
        {
            if (IsHookSetted)
            {
                User32.UnhookWindowsHookEx(_hhook);
                _hhook = IntPtr.Zero;
            }
        }

        public bool IsHookSetted
        {
            get { return _hhook != IntPtr.Zero; }
        }

        private IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
        {
            if (nCode >= 0)
            {
                bool handled = false;

                switch (wParam.ToInt32())
                {
                    case Messages.WM_SYSKEYDOWN:
                    case Messages.WM_KEYDOWN:
                        int vkCode = Marshal.ReadInt32(lParam);
                        Keys keys = ExtractKey(vkCode);
                        RaiseKeyDown(keys, ref handled);
                        break;
                    case Messages.WM_SYSKEYUP:
                    case Messages.WM_KEYUP:
                        vkCode = Marshal.ReadInt32(lParam);
                        keys = ExtractKey(vkCode);
                        RaiseKeyUp(keys, ref handled);
                        break;
                }
                if (handled)
                    return IntPtr.Add(IntPtr.Zero, 1);
            }
            GC.KeepAlive(this);
            return User32.CallNextHookEx(_hhook, nCode, wParam, lParam);
        }

        private Keys ExtractKey(int wParam)
        {
            Keys key = (Keys)wParam;
            return key;
        }
        #region Events

        public event KeyEventHandler KeyDown;
        public event KeyEventHandler KeyUp;

        private void RaiseKeyUp(Keys keys, ref bool handled)
        {
            if (KeyUp != null)
            {
                KeyEventArgs e = new KeyEventArgs(keys);
                KeyUp(this, e);
                handled = e.Handled;
            }
        }


        private void RaiseKeyDown(Keys keys, ref bool handled)
        {
            if (KeyDown != null)
            {
                KeyEventArgs e = new KeyEventArgs(keys);
                KeyDown(this, e);
                handled = e.Handled;
            }
        }
        #endregion

        #region Implementation of IDisposable

        /// <summary>
        /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
        /// </summary>
        /// <filterpriority>2</filterpriority>
        public void Dispose()
        {
            ReleaseHook();
        }

        #endregion
    }

有人知道如何解决这个问题吗?

I've the following problem:

My global keyboard hook stops working after i start any Remote Desktop session, even if session is closed hook doesn't work at all.

Here is code of my hook

public class KeyboardHooker : IDisposable
    {
        private IntPtr _hhook;
        private static User32.LowLevelKeyboardProc _delegate;

        public KeyboardHooker()
        {
            _delegate = new User32.LowLevelKeyboardProc(HookCallback);
        }

        public void SetHook()
        {
            if (IsHookSetted)
                return;

            using (Process curProcess = Process.GetCurrentProcess())
            using (ProcessModule curModule = curProcess.MainModule)
            {
                _hhook = User32.SetWindowsHookEx(User32.WH_KEYBOARD_LL, _delegate, Kernel32.GetModuleHandle(curModule.ModuleName), 0);
            }
        }

        public void ReleaseHook()
        {
            if (IsHookSetted)
            {
                User32.UnhookWindowsHookEx(_hhook);
                _hhook = IntPtr.Zero;
            }
        }

        public bool IsHookSetted
        {
            get { return _hhook != IntPtr.Zero; }
        }

        private IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
        {
            if (nCode >= 0)
            {
                bool handled = false;

                switch (wParam.ToInt32())
                {
                    case Messages.WM_SYSKEYDOWN:
                    case Messages.WM_KEYDOWN:
                        int vkCode = Marshal.ReadInt32(lParam);
                        Keys keys = ExtractKey(vkCode);
                        RaiseKeyDown(keys, ref handled);
                        break;
                    case Messages.WM_SYSKEYUP:
                    case Messages.WM_KEYUP:
                        vkCode = Marshal.ReadInt32(lParam);
                        keys = ExtractKey(vkCode);
                        RaiseKeyUp(keys, ref handled);
                        break;
                }
                if (handled)
                    return IntPtr.Add(IntPtr.Zero, 1);
            }
            GC.KeepAlive(this);
            return User32.CallNextHookEx(_hhook, nCode, wParam, lParam);
        }

        private Keys ExtractKey(int wParam)
        {
            Keys key = (Keys)wParam;
            return key;
        }
        #region Events

        public event KeyEventHandler KeyDown;
        public event KeyEventHandler KeyUp;

        private void RaiseKeyUp(Keys keys, ref bool handled)
        {
            if (KeyUp != null)
            {
                KeyEventArgs e = new KeyEventArgs(keys);
                KeyUp(this, e);
                handled = e.Handled;
            }
        }


        private void RaiseKeyDown(Keys keys, ref bool handled)
        {
            if (KeyDown != null)
            {
                KeyEventArgs e = new KeyEventArgs(keys);
                KeyDown(this, e);
                handled = e.Handled;
            }
        }
        #endregion

        #region Implementation of IDisposable

        /// <summary>
        /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
        /// </summary>
        /// <filterpriority>2</filterpriority>
        public void Dispose()
        {
            ReleaseHook();
        }

        #endregion
    }

Does anybody know how to solve this problem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

金兰素衣 2024-10-25 04:12:44

我找到了解决方案。当我的代码长时间处理按键输入并且 Windows 通过超时分离我的钩子时,就会发生这种情况。现在我在单独的线程中处理每个操作,并且钩子工作正常。

I've found a solution. It happens when my code process keys input for a long time and Windows detached my hook by timeout. Now I process each operation in separate thread and hook works fine.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文