<output id="qn6qe"></output>

    1. <output id="qn6qe"><tt id="qn6qe"></tt></output>
    2. <strike id="qn6qe"></strike>

      亚洲 日本 欧洲 欧美 视频,日韩中文字幕有码av,一本一道av中文字幕无码,国产线播放免费人成视频播放,人妻少妇偷人无码视频,日夜啪啪一区二区三区,国产尤物精品自在拍视频首页,久热这里只有精品12

      孤行Blog

      C# Winform中無焦點狀態下獲取鍵盤輸入或者USB掃描槍數據

      類文件:

       

      C#類文件 
      using System;
      using System.Collections.Generic;
      using System.Text;
      using System.Runtime.InteropServices;
      using System.Reflection;
      
      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;
              }
          }
      }
      View Code

       

       

      頁面中用法:

       

      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, "系統提示");
                  }
              }
          }
      }

      posted on 2013-09-03 10:47  孤行  閱讀(6318)  評論(3)    收藏  舉報

      主站蜘蛛池模板: 人禽无码视频在线观看| 亚洲av无码专区在线亚| 亚洲人妻av伦理| 成人免费视频一区二区三区| 国产在线一区二区在线视频 | 久久精品国产热久久精品国产亚洲 | 亚洲春色在线视频| 久久精产国品一二三产品| 国产一区二区午夜福利久久| AV无码免费不卡在线观看| 午夜免费啪视频| 国色精品卡一卡2卡3卡4卡在线| 铅山县| 真实国产乱子伦视频| 色爱综合另类图片av| 亚洲精品一区二区三区在线观看| 国产办公室秘书无码精品99 | 亚洲一品道一区二区三区| 人妻有码av中文字幕久久琪| 国产永久免费高清在线观看| 国产熟女高潮一区二区三区| 丝袜国产一区av在线观看| 国内少妇偷人精品免费| 欧美成年黄网站色视频| 亚洲成av人片在www色猫咪| 成人国产精品一区二区网站公司| 九九热视频精品在线播放| 高清自拍亚洲精品二区| 玩弄少妇人妻| 色综合久久综合中文综合网| 亚洲午夜成人精品电影在线观看| 曰本丰满熟妇xxxx性| 亚洲欧美日韩在线码| 亚洲ΑV久久久噜噜噜噜噜| 东方市| 久久精品蜜芽亚洲国产av| 精品国产丝袜自在线拍国语| 景德镇市| 中文熟妇人妻av在线| 无码人妻丰满熟妇区毛片18| 日本高清视频网站www|