时间:2021-05-19
复制代码 代码如下:
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); //触发事件
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;
}
}
}
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
01、函数参数和返回值的作用函数根据有没有参数以及有没有返回值,可以相互结合,共有四种:无参数无返回值无参数有返回值有参数无返回值有参数有返回值定义函数时候,是
复制代码代码如下://返回值给父窗体functionreturnParent(value){//获取子窗体返回值varparent=window.dialogA
sort()是可变对象(字典、列表)的方法,无参数,无返回值,sort()会改变可变对象,因此无需返回值。sort()方法是可变对象独有的方法或者属性,而作为不
函数返回值和返回引用是不同的函数返回值时会产生一个临时变量作为函数返回值的副本,而返回引用时不会产生值的副本,既然是引用,那引用谁呢?这个问题必须清楚,否则将无
python中return不返回值是因为你没有将返回的值取出来。解决方法:调用函数,将函数的返回值赋给一个变量,输出这个变量就可以看到函数的返回值了示例如下:d