我怎样才能只挂钩隐藏键盘来像键盘记录器一样读取按键? (条码扫描器)

How can i hook only hid keyboard to read keys like a keylogger ? (barcode-scanner)

我正在尝试制作一个用于读取来自我的条形码的数据的侦听器 scanner.I 制作了一个键盘记录器,但它捕获了来自我的笔记本电脑键盘和条形码扫描仪的按键。我想捕获仅来自我的条形码扫描仪的数据。我不知道该怎么做,所以任何建议都可以帮助我。

这是我的 SetHook 功能,也许有人知道我如何通过编辑该功能来专注于隐藏。

private static IntPtr SetHook(LowLevelKeyboardProc proc)
{
  using (Process curProcess = Process.GetCurrentProcess())
    using (ProcessModule curModule = curProcess.MainModule)
      {
        return SetWindowsHookEx(WH_KEYBOARD_LL, proc, GetModuleHandle(curModule.ModuleName), 0);
      }
}

下面是我为同样的问题编写的代码。代码的工作方式如下,

在 HookHandleProc 中,它正在等待以下键在 50 毫秒内被关闭

private const Keys HK_PRE = Keys.F5;  //0x0007;
private const Keys HK_PRE_VALID = Keys.OemOpenBrackets;

如果完成,那么我们设置 _hookFlag = true,所以再次在 HookHandleProc 中,我们将键吞入 _buffer 直到

private const Keys HK_SUFF = Keys.Oemtilde; //0x000A;

因此,如果您 对条形码 reader 进行编程以包含 这些前缀和后缀值 ,您可以知道数据在哪里来自

这是代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Diagnostics;
using Keys = System.Windows.Forms.Keys;

namespace BarcodeScanner
{
public class CouldntHookException : Exception
{
    public override string Message
    {
        get
        {
            return "barcode_input_handler couldnt add itself to the hook chain.";
        }
    }

    public override string ToString()
    {
        return Message;
    }
}


public class BarcodeHandler : IDisposable
{
    private delegate long delLowLevelKeyboardProc(int nCode, IntPtr wParam, IntPtr lParam);

    public delegate void delInput(string scanCode);

    public event delInput InputEvent;

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

    [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    [return: MarshalAs(UnmanagedType.Bool)]
    private static extern bool UnhookWindowsHookEx(long hhk);

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

    [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    private static extern IntPtr GetModuleHandle(string lpModuleName);



    private const int WH_KEYBOARD_LL = 13;
    private const int WM_KEYDOWN = 0x0100;
    private const int WM_KEYUP = 0x0101;
    private const Keys HK_PRE = Keys.F5;  //0x0007;
    private const Keys HK_PRE_VALID = Keys.OemOpenBrackets;
    private const Keys HK_SUFF = Keys.Oemtilde; //0x000A;
    private long _hHook = 0;
    private StringBuilder _buffer;
    private bool _hookFlag;
    private bool _hookValid;
    delLowLevelKeyboardProc _procHook;
    taglParam _lastKey;

    struct taglParam
    {
        public int _vkCode, _scanCode, _flags, _time;

        public taglParam(int vkCode, int scanCode, int flags, int time)
        { _vkCode = vkCode; _scanCode = scanCode; _flags = flags; _time = time; }
    };

    public BarcodeHandler()
    {
        _buffer = new StringBuilder(128);
        _hookFlag = false;
        _hookValid = false;

        _procHook = new delLowLevelKeyboardProc(HookHandleProc);
        _hHook = SetHook();

        if (_hHook == 0)
            throw new CouldntHookException();
    }

    ~BarcodeHandler()
    {
        Dispose();
    }

    private long SetHook()
    {

        using (Process cp = Process.GetCurrentProcess())
        using (ProcessModule cm = cp.MainModule)
        {
            return SetWindowsHookEx(WH_KEYBOARD_LL, _procHook,
                GetModuleHandle(cm.ModuleName), 0);
        }
    }

    private long HookHandleProc(int nCode, IntPtr wParam, IntPtr lParam)
    {
        if (nCode < 0 || wParam != (IntPtr)WM_KEYDOWN)
            return CallNextHookEx(_hHook, nCode, wParam, lParam);

        taglParam lp = (taglParam)Marshal.PtrToStructure(lParam, typeof(taglParam));

        if (_hookFlag)
        {
            if ((Keys)lp._vkCode == HK_SUFF)
            {
                _hookFlag = false;
                _hookValid = false;
                //trigger event here
                //use begininvoke instead of invoke  ;)
                InputEvent.Invoke(_buffer.ToString());
                return -1;
            }

            _buffer.Append((char)lp._vkCode);
            return -1;
        }
        else if ((Keys)lp._vkCode == HK_PRE)
        {
            _hookValid = true;
            _lastKey = lp;
        }
        else if (_hookValid && (Keys)lp._vkCode == HK_PRE_VALID)
        {
            if (lp._time - _lastKey._time < 50)
            {
                _buffer.Clear();
                _hookFlag = true;
                return -1;
            }
            else
                _hookValid = false;
        }


        return CallNextHookEx(_hHook, nCode, wParam, lParam);
    }

    private bool DeleteHook()
    {
        return UnhookWindowsHookEx(_hHook);
    }

    public void Dispose()
    {
        DeleteHook();

        GC.SuppressFinalize(this);
    }
}
}