1 using System;
2 using System.Collections.Generic;
3 using System.Text;
4 using System.Runtime.InteropServices;
5 using System.Reflection;
6
7 namespace Common
8 {
9 public class BardCodeHooK
10 {
11 public delegate void BardCodeDeletegate(BarCodes barCode);
12 public event BardCodeDeletegate BarCodeEvent;
13
14 public struct BarCodes
15 {
16 public int VirtKey;//虛擬嗎
17 public int ScanCode;//掃描碼
18 public string KeyName;//鍵名
19 public uint Ascll;//Ascll
20 public char Chr;//字元
21
22 public string BarCode;//條碼資訊
23 public bool IsValid;//條碼是否有效
24 public DateTime Time;//掃描時間
25 }
26
27 private struct EventMsg
28 {
29 public int message;
30 public int paramL;
31 public int paramH;
32 public int Time;
33 public int hwnd;
34 }
35
36 [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
37 private static extern int SetWindowsHookEx(int idHook, HookProc lpfn, IntPtr hInstance, int threadId);
38
39 [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
40 private static extern bool UnhookWindowsHookEx(int idHook);
41
42 [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
43 private static extern int CallNextHookEx(int idHook, int nCode, Int32 wParam, IntPtr lParam);
44
45 [DllImport("user32", EntryPoint = "GetKeyNameText")]
46 private static extern int GetKeyNameText(int IParam, StringBuilder lpBuffer, int nSize);
47
48 [DllImport("user32", EntryPoint = "GetKeyboardState")]
49 private static extern int GetKeyboardState(byte[] pbKeyState);
50
51 [DllImport("user32", EntryPoint = "ToAscii")]
52 private static extern bool ToAscii(int VirtualKey, int ScanCode, byte[] lpKeySate, ref uint lpChar, int uFlags);
53
54 delegate int HookProc(int nCode, Int32 wParam, IntPtr lParam);
55 BarCodes barCode = new BarCodes();
56 int hKeyboardHook = 0;
57 string strBarCode = "";
58
59 private int KeyboardHookProc(int nCode, Int32 wParam, IntPtr lParam)
60 {
61 if (nCode == 0)
62 {
63 EventMsg msg = (EventMsg)Marshal.PtrToStructure(lParam, typeof(EventMsg));
64 if (wParam == 0x100)//WM_KEYDOWN=0x100
65 {
66 barCode.VirtKey = msg.message & 0xff;//虛擬嗎
67 barCode.ScanCode = msg.paramL & 0xff;//掃描碼
68 StringBuilder strKeyName = new StringBuilder(225);
69 if (GetKeyNameText(barCode.ScanCode * 65536, strKeyName, 255) > 0)
70 {
71 barCode.KeyName = strKeyName.ToString().Trim(new char[] { ' ', '\0' });
72 }
73 else
74 {
75 barCode.KeyName = "";
76 }
77 byte[] kbArray = new byte[256];
78 uint uKey = 0;
79 GetKeyboardState(kbArray);
80
81
82 if (ToAscii(barCode.VirtKey, barCode.ScanCode, kbArray, ref uKey, 0))
83 {
84 barCode.Ascll = uKey;
85 barCode.Chr = Convert.ToChar(uKey);
86 }
87
88 TimeSpan ts = DateTime.Now.Subtract(barCode.Time);
89
90 if (ts.TotalMilliseconds > 50)
91 {
92 strBarCode = barCode.Chr.ToString();
93 }
94 else
95 {
96 if ((msg.message & 0xff) == 13 && strBarCode.Length > 3)
97 {
98 barCode.BarCode = strBarCode;
99 barCode.IsValid = true;
100 }
101 strBarCode += barCode.Chr.ToString();
102 }
103 barCode.Time = DateTime.Now;
104 if (BarCodeEvent != null) BarCodeEvent(barCode);//觸發事件
105 barCode.IsValid = false;
106 }
107 }
108 return CallNextHookEx(hKeyboardHook, nCode, wParam, lParam);
109 }
110
111 //安裝鉤子
112 public bool Start()
113 {
114 if (hKeyboardHook == 0)
115 {
116 //WH_KEYBOARD_LL=13
117 hKeyboardHook = SetWindowsHookEx(13, new HookProc(KeyboardHookProc), Marshal.GetHINSTANCE(Assembly.GetExecutingAssembly().GetModules()[0]), 0);
118 }
119 return (hKeyboardHook != 0);
120 }
121
122 //卸載鉤子
123 public bool Stop()
124 {
125 if (hKeyboardHook != 0)
126 {
127 return UnhookWindowsHookEx(hKeyboardHook);
128 }
129 return true;
130 }
131 }
132 }
頁面中用法:
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, "系統提示");
}
}
}
}
留言列表