单位因某些原因得搞个电子发票的自助打印程序,本着兴趣爱好,自己搞了一个。后来发现扫码到textbox经常有丢焦点或其他问题。改成了通过键盘钩子无焦点获取扫码枪输入。
通过调用windows函数SetWindowsHookEx 注册全局钩子获取键盘输入。
能识别英文大小写、数字和汉字。这部分是搬运的代码
public class BardCodeHooK
{
public delegate void BardCodeDeletegate(BarCodes barCode);
public event BardCodeDeletegate BarCodeEvent;
//定义成静态,这样不会抛出回收异常
private static HookProc hookproc;
public struct BarCodes
{
public int VirtKey;//虚拟吗
public int ScanCode;//扫描码
public string KeyName;//键名
public uint Ascll;//Ascll
public char Chr;//字符
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);
[DllImport("user32", EntryPoint = "GetKeyNameText")]
private static extern int GetKeyNameText(int IParam, StringBuilder lpBuffer, int nSize);
[DllImport("user32", EntryPoint = "GetKeyboardState")]
private static extern int GetKeyboardState(byte[] pbKeyState);
[DllImport("user32", EntryPoint = "ToAscii")]
private static extern bool ToAscii(int VirtualKey, int ScanCode, byte[] lpKeySate, ref uint lpChar, int uFlags);
[DllImport("kernel32.dll")]
public static extern IntPtr GetModuleHandle(string name);
delegate int HookProc(int nCode, Int32 wParam, IntPtr lParam);
BarCodes barCode = new BarCodes();
int hKeyboardHook = 0;
string strBarCode = "";
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;//扫描码
StringBuilder strKeyName = new StringBuilder(225);
if (GetKeyNameText(barCode.ScanCode * 65536, strKeyName, 255) > 0)
{
barCode.KeyName = strKeyName.ToString().Trim(new char[] { ' ', '\0' });
}
else
{
barCode.KeyName = "";
}
byte[] kbArray = new byte[256];
uint uKey = 0;
GetKeyboardState(kbArray);
if (ToAscii(barCode.VirtKey, barCode.ScanCode, kbArray, ref uKey, 0))
{
barCode.Ascll = uKey;
barCode.Chr = Convert.ToChar(uKey);
}
TimeSpan ts = DateTime.Now.Subtract(barCode.Time);
if (ts.TotalMilliseconds > 50)
{//时间戳,大于50 毫秒表示手动输入
strBarCode = barCode.Chr.ToString();
}
else
{
if ((msg.message & 0xff) == 13 && strBarCode.Length > 7)
{//回车
barCode.BarCode = strBarCode;
barCode.IsValid = true;
}
strBarCode += barCode.Chr.ToString();
}
barCode.Time = DateTime.Now;
if (BarCodeEvent != null)
BarCodeEvent(barCode);//触发事件
barCode.IsValid = false;
}
}
return CallNextHookEx(hKeyboardHook, nCode, wParam, lParam);
}
//安装钩子
public bool Start()
{
if (hKeyboardHook == 0)
{
hookproc = new HookProc(KeyboardHookProc);
//GetModuleHandle 函数 替代 Marshal.GetHINSTANCE
//防止在 framework4.0中 注册钩子不成功
IntPtr modulePtr = GetModuleHandle(Process.GetCurrentProcess().MainModule.ModuleName);
//WH_KEYBOARD_LL=13
//全局钩子 WH_KEYBOARD_LL
// hKeyboardHook = SetWindowsHookEx(13, hookproc, Marshal.GetHINSTANCE(Assembly.GetExecutingAssembly().GetModules()[0]), 0);
hKeyboardHook = SetWindowsHookEx(13, hookproc, modulePtr, 0);
}
return (hKeyboardHook != 0);
}
//卸载钩子
public bool Stop()
{
if (hKeyboardHook != 0)
{
return UnhookWindowsHookEx(hKeyboardHook);
}
return true;
}
}
参考使用c#捕获usb扫描枪扫描二维码、条形码结果(支持中文版)_c#获取usb扫码枪数据-CSDN博客
同时我发现运行一段时间后,键盘钩子会失效。后来手动修改windows注册表不行,然后用代码注册并使用invoke方法,解决了问题。程序稳定运行了几天,没有发现钩子失效的情况。
注册和调用方法
private ScanerHook listener = new ScanerHook();
private delegate void ShowInfoDelegate(ScanerHook.ScanerCodes codes);
RegistryKey hkcu = Registry.CurrentUser;
public Form1()
{
InitializeComponent();
initScannerHook();
}
private void initScannerHook()
{
RegistryKey hkSoftWare = hkcu.OpenSubKey(@"Control Panel\Desktop", true);
hkSoftWare.SetValue("LowLevelHooksTimeout", "5000", RegistryValueKind.DWord);
hkcu.Close();
hkSoftWare.Close();
listener.ScanerEvent += new ScanerHook.ScanerDelegate(Listener_ScanerEvent);
}
private void Listener_ScanerEvent(ScanerHook.ScanerCodes codes)
{
getSaomiaoInput(codes);
}
private void getSaomiaoInput(ScanerHook.ScanerCodes codes)
{
try
{
if (this.InvokeRequired)
{
this.BeginInvoke(new ShowInfoDelegate(getSaomiaoInput), new object[] { codes });
}
else
{
Console.WriteLine("程序获取值:" + codes.Result);
mylog.EntryLogQueue("程序获取值:" + codes.Result);
string url = codes.Result;
if (isPrinting)
{
//DrawText("正在打印中,请稍后。");
return;
}
if (url.StartsWith("HTTP"))
{
url = overturnUrl(url);
Console.WriteLine("url转码:" + url);
mylog.EntryLogQueue("url转码:" + url);
}
if (!(url.StartsWith("http://xxx.xxx") || url.StartsWith("https://xxx.xxx")))
{
DrawText("请扫描正确的二维码\r\n");
return;
}
if (url.Length<60)
{
DrawText("请扫描正确的二维码.\r\n");
return;
}
if (QrLists.Contains(url))
{
DrawText("请勿重复扫码\r\n");
return;
}
if (!GetInvoiceFlag(url))
{
DrawText("该发票已打印或不存在,\r\n请到人工窗口咨询。\r\n");
return;
}
QrLists.Enqueue(url);
}
}
catch (Exception e)
{
mylog.EntryLogQueue("getSaomiaoInput:" + e.ToString());
}
}
private void Form1_Load(object sender, EventArgs e)
{
listener.Start();
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
Console.WriteLine("卸载钩子:" + listener.Stop().ToString());
}