SoFunction
Updated on 2025-03-07

Example of getting barcode gun return value without focus


using System;
using ;
using ;
using ;
using ;
using ;

namespace
{
    class BarCodeHook
    {
        public delegate void BarCodeDelegate(BarCodes barCode);
        public event BarCodeDelegate BarCodeEvent;

        public struct BarCodes
        {
public int VirtKey;       //Virtual Code
public int ScanCode;     //Scan code

public string BarCode;   //Barcode information
public bool IsValid;     //Is the barcode valid?
public DateTime Time;     //Scan time
        }

        private struct EventMsg
        {
            public int message;
            public int paramL;
            public int paramH;
            public int Time;
            public int hwnd;
        }

        [DllImport("", CharSet = , CallingConvention = )]
        private static extern int SetWindowsHookEx(int idHook, HookProc lpfn, IntPtr hInstance, int threadId);

        [DllImport("", CharSet = , CallingConvention = )]
        private static extern bool UnhookWindowsHookEx(int idHook);

        [DllImport("", CharSet = , CallingConvention = )]
        private static extern int CallNextHookEx(int idHook, int nCode, Int32 wParam, IntPtr lParam);

        delegate int HookProc(int nCode, Int32 wParam, IntPtr lParam);

        BarCodes barCode = new BarCodes();
        int hKeyboardHook = 0;
        List<char> _barcode = new List<char>(100);
        private int KeyboardHookProc(int nCode, Int32 wParam, IntPtr lParam)
        {
            if (nCode == 0)
            {
                EventMsg msg = (EventMsg)(lParam, typeof(EventMsg));

                if (wParam == 0x100)   //WM_KEYDOWN = 0x100 
                {
= & 0xff; //Virtual code
= & 0xff; //Scan code

                   
                    if (().TotalMilliseconds > 100)
                    {
                        _barcode.Clear();
                    }
                    else
                    {
if (( & 0xff) == 13 && _barcode.Count > 0)   //Enter
                        {
                            = new String(_barcode.ToArray());
                            = true;
                            _barcode.Clear();
                        }
                    }

                    = ;
if (BarCodeEvent != null) BarCodeEvent(barCode);     //Trigger event
                    = false;
                    _barcode.Add(( & 0xff));
                }
            }
            return CallNextHookEx(hKeyboardHook, nCode, wParam, lParam);
        }

        private static HookProc hookproc;
// Install hook
        public bool Start()
        {
            if (hKeyboardHook == 0)
            {
                hookproc = new HookProc(KeyboardHookProc);
                //WH_KEYBOARD_LL = 13 
                hKeyboardHook = SetWindowsHookEx(13, hookproc, (().GetModules()[0]), 0);
            }
            return (hKeyboardHook != 0);
        }

// Uninstall hook
        public bool Stop()
        {
            if (hKeyboardHook != 0)
            {
                return UnhookWindowsHookEx(hKeyboardHook);
            }
            return true;
        }
    }
}