技术标签: usb扫描枪 .NET WinForm读取扫描枪从CO 二维码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
namespace Common
{
public class BardCodeHooK
{
public delegate void BardCodeDeletegate(BarCodes barCode);
public event BardCodeDeletegate BarCodeEvent;
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);
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)
{
strBarCode = barCode.Chr.ToString();
}
else
{
if ((msg.message & 0xff) == 13 && strBarCode.Length > 3)
{
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)
{
//WH_KEYBOARD_LL=13
hKeyboardHook = SetWindowsHookEx(13, new HookProc(KeyboardHookProc), Marshal.GetHINSTANCE(Assembly.GetExecutingAssembly().GetModules()[0]), 0);
}
return (hKeyboardHook != 0);
}
//卸载钩子
public bool Stop()
{
if (hKeyboardHook != 0)
{
return UnhookWindowsHookEx(hKeyboardHook);
}
return true;
}
}
}
页面中用法:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace Common
{
public partial class FrmMain : Form
{
BardCodeHooK BarCode = new BardCodeHooK();
public FrmMain()
{
InitializeComponent();
BarCode.BarCodeEvent += new BardCodeHooK.BardCodeDeletegate(BarCode_BarCodeEvent);
}
private delegate void ShowInfoDelegate(BardCodeHooK.BarCodes barCode);
private void ShowInfo(BardCodeHooK.BarCodes barCode)
{
if (this.InvokeRequired)
{
this.BeginInvoke(new ShowInfoDelegate(ShowInfo), new object[] { barCode });
}
else
{
textBox1.Text = barCode.KeyName;
textBox2.Text = barCode.VirtKey.ToString();
textBox3.Text = barCode.ScanCode.ToString();
textBox4.Text = barCode.Ascll.ToString();
textBox5.Text = barCode.Chr.ToString();
textBox6.Text = barCode.IsValid? barCode.BarCode : "";//是否为扫描枪输入,如果为true则是 否则为键盘输入
textBox7.Text += barCode.KeyName;
//MessageBox.Show(barCode.IsValid.ToString());
}
}
//C#中判断扫描枪输入与键盘输入
//Private DateTime _dt = DateTime.Now; //定义一个成员函数用于保存每次的时间点
//private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
//{
// DateTime tempDt = DateTime.Now; //保存按键按下时刻的时间点
// TimeSpan ts = tempDt .Subtract(_dt); //获取时间间隔
// if (ts.Milliseconds > 50) //判断时间间隔,如果时间间隔大于50毫秒,则将TextBox清空
// textBox1.Text = "";
// dt = tempDt ;
//}
void BarCode_BarCodeEvent(BardCodeHooK.BarCodes barCode)
{
ShowInfo(barCode);
}
private void FrmMain_Load(object sender, EventArgs e)
{
BarCode.Start();
}
private void FrmMain_FormClosed(object sender, FormClosedEventArgs e)
{
BarCode.Stop();
}
private void textBox6_TextChanged(object sender, EventArgs e)
{
if (textBox6.Text.Length > 0)
{
MessageBox.Show("条码长度:" + textBox6.Text.Length + "\n条码内容:" + textBox6.Text, "系统提示");
}
}
}
}
文章浏览阅读340次。20世纪80年代,计算机界提出oop(object oriented programming:面向对象)思想,支持面向对象的程序设计应运而生。1982年,本贾尼在c语言的基础上引入并扩充了面向对象的概念,命名为C++。C++既可以进行C语言的过程化程序设计,又可以进行以抽象数据类型为特点的基于对象的程序设计,还可进行面向对象的程序设计C++祖师爷--本贾尼 的照片。
文章浏览阅读714次。@echo off&setlocal enabledelayedexpansiontitle make by zcHacker QQ: 36868508color 1fmode con: cols=78 lines=30for /f "tokens=* delims= " %%f in ('ver') do echo 本机系统为:%%fecho 系统内核为:%os%echo.echo.☆请..._batq切换网卡
文章浏览阅读3.4k次。在应用中搜索‘Attachments List and Document Indexation’,安装后在form视图中就出现了附件按钮_odoo form发送附件
文章浏览阅读235次,点赞5次,收藏3次。代码运行环境为MATLAB r2021b,非下采样小波包变换执行信号的等带宽精细滤波,而不是离散小波变换中的较粗糙的倍频程滤,而且很好的缓解了小波包变换中的时间分辨率损失,这使得非下采样小波包在许多应用中比小波包或离散小波变换表现的更好。从结果中可以看出,NSWT在保留信号细节的同时,具有更好的频率分辨率,能够更清晰地显示信号的频谱特性。以150 和 200 Hz 的两个间歇正弦波为例,看一下非下采样小波包的时频谱图和离散小波变换的时频谱图,效果显而易见。哥廷根数学学派2023。
文章浏览阅读5.1k次,点赞8次,收藏2次。在网上看了很多种解决办法,如下:在pom文件中添加lombok的依赖。在IDEA中安装lombok的插件:在IDEA中点击 File -> Settings -> Plugins,输入Lombok,选择Lombok的Installed进行安装。在IDEA中勾选Enable Annotation Processors:File -> Settings -> Build, Execution, Deployment -> Compiler -> Annotation._java: 找不到符号 符号: 变量 log
文章浏览阅读189次。感觉在哔哩哔哩(bilibili)上看比赛直播比较好,一直可以看到比赛的直播画面,还能听到英文解说和中文主持人的解说。YouTube上是不错,但是一方面爬梯子比较卡,另一方面只能听到英文解说。韩国著名围棋九段棋手李世石与谷歌人工智能“阿尔法围棋”(AlphaGo)的5盘对决,将于3月9日、10日、12日、13日和15日在首尔举行。比赛将采用贴7.5目的中国规则(比赛结束时,先走棋的棋手贴..._围棋游戏九段
文章浏览阅读1.1k次。利用Cubemx生成H743的代码,点击烧录后,无法再识别到Jlink了,只能烧录时按着复位再松开_stm32h743 jlink
文章浏览阅读1.7k次。原标题:高端人士候机专属特权 三星W2017一张行走的VIP卡17年春运时间为1月13日至2月21日,如今春节假期已过,亿万人开始踏上了离乡之路追寻梦想。每年春运都给交通带来巨大压力,今年为期40天的春运预计全国发送旅客或超29亿人次。铁路、公路、水运、民航等不同交通方式面临新的挑战。实际上,受限于预算和距离的原因,各个群体选择的交通方式也有差异,其中距离较长且快捷的出行民航必为首选。 不管是选择..._三星w24vip服务
文章浏览阅读97次。由于之前安装目录带有空格,所以现在想卸载ROR重新安装:gem uninstall Railsgem uninstall Rakegem uninstall Actionwebservicegem uninstall Actionmailergem uninstall Actionpackgem uninstall Activesu转载于:https://www.cnblog..._mac 卸载rails ruby
文章浏览阅读2.1k次,点赞22次,收藏69次。Python数据分析主要包括数据清洗、数据探索和数据可视化三个部分。数据清洗是数据分析的重要环节,主要是对数据进行预处理,包括缺失值处理、异常值处理、数据类型转换等。数据探索则是通过对数据进行初步分析,了解数据的分布和特征,为后续的数据分析提供基础。数据可视化则是将数据以图形化的方式呈现,帮助人们更好地理解和分析数据。_python数据分析与可视化
文章浏览阅读2.8k次,点赞3次,收藏43次。“生产者消费者”问题是一个著名的同时性编程问题的集合。通过编写经典的“生产者消费者”问题的实验,读者可以进一步熟悉 Linux 中多线程编程,并且掌握用信号量处理线程间的同步互斥问题。_编写经典的“生产者-消费者”问题的实验,进一步熟悉linux中的多线程编程,并且掌握
文章浏览阅读2k次,点赞3次,收藏15次。一谈到互斥,我们很自然就会想到了锁。首先我们把一段需要互斥执行的代码称为临界区。线程在进入临界区之前,首先尝试加锁 `lock()`,如果成功,则进入临界区,此时我们称这个线程持有锁;否则呢就等待,直到持有锁的线程解锁;持有锁的线程执行完临界区的代码后,执行解锁 `unlock()`。_java互斥锁也有必要学吗