無焦點獲取條碼槍返回值示例
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Reflection;
namespace BookLibraryManagement.CommonTools
{
class BarCodeHook
{
public delegate void BarCodeDelegate(BarCodes barCode);
public event BarCodeDelegate BarCodeEvent;
public struct BarCodes
{
public int VirtKey; //虛擬碼
public int ScanCode; //掃描碼
public string BarCode; //條碼信息
public bool IsValid; //條碼是否有效
public DateTime Time; //掃描時間
}
private struct EventMsg
{
public int message;
public int paramL;
public int paramH;
public int Time;
public int hwnd;
}
[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
private static extern int SetWindowsHookEx(int idHook, HookProc lpfn, IntPtr hInstance, int threadId);
[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
private static extern bool UnhookWindowsHookEx(int idHook);
[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
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)Marshal.PtrToStructure(lParam, typeof(EventMsg));
if (wParam == 0x100) //WM_KEYDOWN = 0x100
{
barCode.VirtKey = msg.message & 0xff; //虛擬碼
barCode.ScanCode = msg.paramL & 0xff; //掃描碼
if (DateTime.Now.Subtract(barCode.Time).TotalMilliseconds > 100)
{
_barcode.Clear();
}
else
{
if ((msg.message & 0xff) == 13 && _barcode.Count > 0) //回車
{
barCode.BarCode = new String(_barcode.ToArray());
barCode.IsValid = true;
_barcode.Clear();
}
}
barCode.Time = DateTime.Now;
if (BarCodeEvent != null) BarCodeEvent(barCode); //觸發(fā)事件
barCode.IsValid = false;
_barcode.Add(Convert.ToChar(msg.message & 0xff));
}
}
return CallNextHookEx(hKeyboardHook, nCode, wParam, lParam);
}
private static HookProc hookproc;
// 安裝鉤子
public bool Start()
{
if (hKeyboardHook == 0)
{
hookproc = new HookProc(KeyboardHookProc);
//WH_KEYBOARD_LL = 13
hKeyboardHook = SetWindowsHookEx(13, hookproc, Marshal.GetHINSTANCE(Assembly.GetExecutingAssembly().GetModules()[0]), 0);
}
return (hKeyboardHook != 0);
}
// 卸載鉤子
public bool Stop()
{
if (hKeyboardHook != 0)
{
return UnhookWindowsHookEx(hKeyboardHook);
}
return true;
}
}
}
相關(guān)文章
C# Double轉(zhuǎn)化為String時的保留位數(shù)及格式方式
這篇文章主要介紹了C# Double轉(zhuǎn)化為String時的保留位數(shù)及格式方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-02-02C#中把FastReport.Net報表控件的數(shù)據(jù)保存到數(shù)據(jù)庫
這篇文章介紹了在數(shù)據(jù)庫中保存FastReport.Net報表的方法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-06-06詳解如何將.NET應(yīng)用轉(zhuǎn)換成Window服務(wù)
這篇文章主要為大家詳細(xì)介紹了如何將.NET8.0應(yīng)用程序轉(zhuǎn)換成Windows服務(wù),文中的示例代碼講解詳細(xì),有需要的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-01-01C#實現(xiàn)將字符串轉(zhuǎn)化為日期格式的方法詳解
這篇文章主要為大家詳細(xì)介紹了C#如何使用DateTime結(jié)構(gòu)的ParseExact方法和Parse方法分別將字符串轉(zhuǎn)化為日期格式,有需要的小伙伴可以了解一下2024-01-01