程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> C#應用鉤子取得按鍵信息的辦法

C#應用鉤子取得按鍵信息的辦法

編輯:C#入門知識

C#應用鉤子取得按鍵信息的辦法。本站提示廣大學習愛好者:(C#應用鉤子取得按鍵信息的辦法)文章只能為提供參考,不一定能成為您想要的結果。以下是C#應用鉤子取得按鍵信息的辦法正文


本文實例講述了C#應用鉤子取得按鍵信息的辦法。分享給年夜家供年夜家參考。詳細以下:

窗體相干代碼:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Threading;
using ReadBadCode;
namespace gouzi
{
 public partial class Form2 : Form
 {
  BarCodeHook BarCode = new BarCodeHook();
  public Form2()
  {
   InitializeComponent();
   BarCode.BarCodeEvent += new BarCodeHook.BarCodeDelegate(BarCode_BarCodeEvent);
  }
  private delegate void ShowInfoDelegate(BarCodeHook.BarCodes barCode);
  private void ShowInfo(BarCodeHook.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.AscII.ToString();//AscII
    textBox5.Text = barCode.Chr.ToString();//字符
    textBox6.Text = barCode.IsValid ? barCode.BarCode : "";
    //在這裡停止鍵入值
   }
  }
  void BarCode_BarCodeEvent(BarCodeHook.BarCodes barCode)
  {
   ShowInfo(barCode);
  }
  private void Form2_Load(object sender, EventArgs e)
  {
   BarCode.Start();
  }
  private void Form2_StyleChanged(object sender, EventArgs e)
  {
   BarCode.Stop();
  }
 }
}

後台類代碼:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Reflection;
namespace ReadBadCode
{
 class BarCodeHook
 {
  public delegate void BarCodeDelegate(BarCodes barCode);
  public event BarCodeDelegate BarCodeEvent;
  public struct BarCodes
  {
   public int VirtKey;  //虛擬碼 
   public int ScanCode;  //掃描碼 
   public string KeyName; //鍵名 
   public uint AscII;  //AscII 
   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 lParam, StringBuilder lpBuffer, int nSize);
  //將256個虛擬鍵復制到指定的緩沖區中
  [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[] lpKeyState, 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(255);
     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.AscII = uKey;
      barCode.Chr = Convert.ToChar(uKey);
     }
     if (DateTime.Now.Subtract(barCode.Time).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;
  }
 }
}

【留意】要想測試現實的後果,必需履行編譯後的Exe文件,在開辟情況直接運轉會沒有用果的。

願望本文所述對年夜家的C#法式設計有所贊助。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved