相关疑难解决方法(0)

在WPF/C#中使用全局键盘钩子(WH_KEYBOARD_LL)

我从我在互联网上发现的代码中找到了自己的WH_KEYBOARD_LL帮助类:

将以下代码放到一些utils库中,让它成为YourUtils.cs:

using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Runtime.CompilerServices;
using System.Windows.Input;

namespace MYCOMPANYHERE.WPF.KeyboardHelper
{
    public class KeyboardListener : IDisposable
    {
        private static IntPtr hookId = IntPtr.Zero;

        [MethodImpl(MethodImplOptions.NoInlining)]
        private IntPtr HookCallback(
            int nCode, IntPtr wParam, IntPtr lParam)
        {
            try
            {
                return HookCallbackInner(nCode, wParam, lParam);
            }
            catch
            {
                Console.WriteLine("There was some error somewhere...");
            }
            return InterceptKeys.CallNextHookEx(hookId, nCode, wParam, lParam);
        }

        private IntPtr HookCallbackInner(int nCode, IntPtr wParam, IntPtr lParam)
        {
            if (nCode >= 0)
            {
                if (wParam == …
Run Code Online (Sandbox Code Playgroud)

c# wpf winapi

56
推荐指数
1
解决办法
5万
查看次数

标签 统计

c# ×1

winapi ×1

wpf ×1