简体   繁体   English

我如何只钩住隐藏键盘来读取键盘记录键? (条形码扫描器)

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

I'm trying to make a listener for reading data which comes from my barcode scanner.I made a keylogger but it catches keys that come from my laptop keyboard and barcode-scanner . 我正在试图让听众读取来自我的条形码扫描仪的数据。我制作了一个键盘记录器,但它可以捕获来自我的笔记本键盘和条形码扫描仪的键。 I want to catch data which come only my barcode scanner. 我想要捕获只有我的条形码扫描仪的数据。 I don't know how can i do that so any suggestion can help me. 我不知道我怎么能这样做,所以任何建议都可以帮助我。

Here is my SetHook fonction maybe someone knows how can i focus to hid with editing that fonction. 这是我的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);
      }
}

Below is the code i have written for the same problem. 下面是我为同一个问题编写的代码。 The code works like, 代码就像,

In HookHandleProc, it is waiting the following keys to be downed in less than 50 ms 在HookHandleProc中,它正在等待以下键在不到50毫秒内被击落

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

If it's done, then we set _hookFlag = true, so again in HookHandleProc, we swallow keys into _buffer till 如果已经完成,那么我们设置_hookFlag = true,所以再次在HookHandleProc中,我们将键吞入_buffer直到

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

So if you program your barcode reader to include these prefix and suffix values , you can know where the data comes from 因此,如果您对条形码阅读器进行编程以包含这些前缀和后缀值 ,则可以知道数据的来源

Here is the code 这是代码

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);
    }
}
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我如何从条码扫描仪模块读取条码前缀 - How i can read Barcode Prefix from Barcode Scanner Module 如何限制Windows Form文本框仅读取USB条码扫描器的输入? - How can i restrict windows form textbox to read input only from my USB barcode scanner? 如何检查我的条形码扫描仪是否符合USB HID POS扫描仪规范? - How do I check if my barcode scanner adheres to the USB HID POS Scanner specification? 条码扫描器 | 只读取条形码的第一部分 - Barcode scanner | Read the first part of the barcode only 如何使用不模拟键盘的条码扫描仪读取条码? - How to read barcodes with a barcode scanner that does not emulate keyboard? 仅允许条码扫描仪并消除键盘输入 - Allow Only Barcode Scanner and Eliminate Keyboard Input 如何指定可以使用C#在文本框中进行书写的书写对象(键盘/条形码扫描仪)? - How to specify which writing object (keyboard/barcode scanner) can be used to write in textbox using C#? 我如何将海牛作品条形码扫描仪集成到堆叠布局中? - How can I integrate manatee works barcode scanner in a stacklayout? 如何检查用户输入是来自条形码扫描仪还是键盘? - How to Check if User input is from Barcode Scanner or Keyboard? 有什么办法可以像我钩住键盘键一样全局钩住鼠标动作? - Is there any way to global hook the mouse actions like i'm hooking the keyboard keys?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM