admin管理员组

文章数量:1123342

I am trying to detect keypress from my MAUI C# desktop app which is minimized in background. Any help.

I have tried GlobalKeyboardHook I have tried using this `class GlobalKeyboardHook { private const int WH_KEYBOARD_LL = 13; private const int WM_KEYUP = 0x0101;

        private delegate int LowLevelKeyboardProc(int nCode, IntPtr wParam, IntPtr lParam);

        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        private static extern IntPtr SetWindowsHookEx(int idHook, LowLevelKeyboardProc lpfn, IntPtr hMod, uint dwThreadId);

        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        private static extern bool UnhookWindowsHookEx(IntPtr hhk);

        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        private static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode, IntPtr wParam, IntPtr lParam);

        private IntPtr _hookID = IntPtr.Zero;
        private LowLevelKeyboardProc _proc;

        public GlobalKeyboardHook()
        {
            _proc = HookCallback;
        }

        public void Hook()
        {
            _hookID = SetWindowsHookEx(WH_KEYBOARD_LL, _proc, IntPtr.Zero, 0);
        }

        public void Unhook()
        {
            UnhookWindowsHookEx(_hookID);
        }

        private int HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
        {
            if (nCode >= 0 && wParam == (IntPtr)WM_KEYUP)
            {
                int vkCode = Marshal.ReadInt32(lParam);

                //MessageBox.Show($"Key released: {vkCode}");
                if (vkCode == 9)
                {
                        Global.Initiate_server?.SendMessage(Global.TrepCserver.Takescreenshot);
                }
                // Handle key release here
            }

            return (int)CallNextHookEx(_hookID, nCode, wParam, lParam);
        }
}`

This works for wpf application but not maui targeting windows. Help. Is there 3rd party that could work or developing a class that could work for this function?

本文标签: Is there a way to hook keypress in background in C MAUI desktopStack Overflow