C 鍵盤鉤子

2021-07-03 09:15:51 字數 2261 閱讀 7997

鍵盤鉤子是一種可以監控鍵盤操作的指令,我們去釣魚只要魚兒上鉤

不管它怎麼逃,只要掌控好鉤子上的繩子總是可以找到這條魚,鍵盤

鉤子是利用電腦一行行執行**特性,在目的視窗處理鍵**前攔截

把某個指令替換為另外一種指令,然後再把訊息傳送給目的視窗這樣

乙個週期下來,視窗程式會認為使用者輸入的就是現在的數值或者沒有 輸

入,不過鍵盤鉤子在某些不法分子手裡則成為了 盜號、監控密碼 等

違法操作。

原型:hhook setwindowshookex(int idhook, hookproc lpfn, hinstance hmod, dword dwthreadid);

// win32 keyboard hook.

public const int wh_keyboard_ll = 13;

public const int null = 0;

public delegate int hookproc(int code, int wparam, kbdllhookstruct lparam);

[dllimport("user32.dll", setlasterror = true)]

public static extern int setwindowshookex(int hooktype, hookproc lpfn, int hmod, int dwthreadid);

[dllimport("user32.dll", setlasterror = true)]

public extern static int callnexthookex(int handle, int code, int wparam, kbdllhookstruct lparam);

[structlayout(layoutkind.sequential)]

public class kbdllhookstruct

[flags]

public enum kbdllhookstruct : uint

public volatile int hhook;

protected override void onload(eventargs e)

protected int keyboardproc(int code, int wparam, kbdllhookstruct lparam)

監控系統內所有程序鍵盤訊息:

setwindowshookex(wh_keyboard_ll, keyboardproc, null, null)

// win32 keyboard hook.

public const int wh_keyboard = 2;

public const int null = 0;

public delegate int hookproc(int code, int wparam, int lparam);

[dllimport("kernel32.dll", setlasterror = true)]

public static extern int getcurrentthreadid();

[dllimport("user32.dll", setlasterror = true)]

public static extern int setwindowshookex(int hooktype, hookproc lpfn, int hmod, int dwthreadid);

[dllimport("user32.dll", setlasterror = true)]

public extern static int callnexthookex(int handle, int code, int wparam, int lparam);

public volatile int hhook;

protected override void onload(eventargs e)

protected int keyboardqueue(int code, int wparam, int lparam)

監控本程序的所有鍵盤訊息:

setwindowshookex(wh_keyboard, keyboardproc, null, getcurrentthreadid());

protected int keyboardproc(int code, int wparam, int lparam)

c 全域性鍵盤鉤子

using system using system.collections.generic using system.text using system.windows.forms using system.runtime.interopservices using system.diagnosti...

c 鍵盤鉤子全解

c 鍵盤鉤子全解 using system using system.collections.generic using system.text using system.runtime.interopservices 呼叫作業系統動態鏈結庫 using system.reflection usin...

C winform鍵盤鉤子

新建乙個工程csharpkeyboardhook,在winform窗體上拉乙個lable 用於顯示,按下的是那個鍵 和兩個button 用於開關勾子 下面是c winform鍵盤勾子的 照搬上去就可以的啦 一 新建乙個類檔案keyboardhooklib,如下所示 using system usin...