當前位置:編程學習大全網 - 源碼下載 - C# 如何編寫程序監控鍵盤,即使程序的窗體不是當前活動窗體

C# 如何編寫程序監控鍵盤,即使程序的窗體不是當前活動窗體

必須使用Windows api,用鍵盤鉤子函數截取鍵盤按鍵記錄,然後把這個EXE程序註冊為系統服務就能自動運行了

C#中鍵盤鉤子的使用 [轉帖]

public class Win32Hook

{

[DllImport("kernel32")]

public static extern int GetCurrentThreadId();

[DllImport( "user32",

CharSet=CharSet.Auto,CallingConvention=CallingConvention.StdCall)]

public static extern int SetWindowsHookEx(

HookType idHook,

HOOKPROC lpfn,

int hmod,

int dwThreadId);

public enum HookType

{

WH_KEYBOARD = 2

}

public delegate int HOOKPROC(int nCode, int wParam, int lParam);

public void SetHook()

{

// set the keyboard hook

SetWindowsHookEx(HookType.WH_KEYBOARD,

new HOOKPROC(this.MyKeyboardProc),

0,

GetCurrentThreadId());

}

public int MyKeyboardProc(int nCode, int wParam, int lParam)

{

//在這裏放置妳的處理代碼 return 0;

}

}

使用方法

可以在Form的構造函數裏放入

Win32Hook hook = new Win32Hook();

hook.SetHook();

  • 上一篇:Synchronize的實現原理
  • 下一篇:真空商城源代碼
  • copyright 2024編程學習大全網