C winform鍵盤鉤子

2021-07-04 07:16:19 字數 3312 閱讀 1420

新建乙個工程csharpkeyboardhook,在winform窗體上拉乙個lable(用於顯示,按下的是那個鍵)和兩個button(用於開關勾子)

下面是c#winform鍵盤勾子的**,照搬上去就可以的啦:

一、新建乙個類檔案keyboardhooklib,**如下所示:

using system;

using system.collections.generic;

using system.text;

using system.runtime.interopservices;

using system.reflection;

using system.diagnostics;

using microsoft.win32;

using system.windows.forms;

namespace csharpkeyboardhook

//設定鉤子

[dllimport("user32.dll")]

private static extern int setwindowshookex(int idhook, hookhandle lpfn, intptr hinstance, int threadid);

//取消鉤子

[dllimport("user32.dll", charset = charset.auto, callingconvention = callingconvention.stdcall)]

private static extern bool unhookwindowshookex(int idhook);

//呼叫下乙個鉤子

[dllimport("user32.dll")]

private static extern int callnexthookex(int idhook, int ncode, int wparam, intptr lparam);

//獲取當前執行緒id

[dllimport("kernel32.dll")]

private static extern int getcurrentthreadid();

//gets the main module for the associated process.

[dllimport("kernel32.dll")]

private static extern intptr getmodulehandle(string name);

private intptr _hookwindowptr = intptr.zero;

//構造器

public keyboardhooklib()

//外部呼叫的鍵盤處理事件

private static processkeyhandle _clientmethod = null;

/// /// 安裝勾子

///

/// 外部呼叫的鍵盤處理事件

public void installhook(processkeyhandle clientmethod)

}//取消鉤子事件

public void uninstallhook()

}//鉤子事件內部呼叫,呼叫_clientmethod方法**到客戶端應用。

private static int gethookproc(int ncode, int wparam, intptr lparam)

}return callnexthookex(_hhookvalue, ncode, wparam, lparam);}}

}

/////

介面事件**

using system;

using system.collections.generic;

using system.componentmodel;

using system.data;

using system.drawing;

using system.text;

using system.windows.forms;

using system.runtime.interopservices;

using system.reflection;

using system.diagnostics;

using microsoft.win32;

namespace csharpkeyboardhook

private void button1_click(object sender, eventargs e)

private void button2_click(object sender, eventargs e)

/// /// 客戶端鍵盤捕捉事件.

///

/// 由hook程式傳送的按鍵資訊

/// 是否攔截

public void keypress(keyboardhooklib.hookstruct hookstruct, out bool handle)

if (hookstruct.vkcode == 92)// 截獲右win

//截獲ctrl+esc

if (hookstruct.vkcode == (int)keys.escape && (int)control.modifierkeys == (int)keys.control)

//截獲alt+f4

if (hookstruct.vkcode == (int)keys.f4 && (int)control.modifierkeys == (int)keys.alt)

//截獲alt+tab

if (hookstruct.vkcode == (int)keys.tab && (int)control.modifierkeys == (int)keys.alt)

//截獲f1

if (hookstruct.vkcode == (int)keys.f1)

//截獲ctrl+alt+delete

if ((int)control.modifierkeys == (int)keys.control + (int)keys.alt + (int)keys.delete)

//如果鍵a~z

if (hookstruct.vkcode >= (int)keys.a && hookstruct.vkcode <= (int)keys.z)

keys key = (keys)hookstruct.vkcode;

label1.text = "你按下:" + (key == keys.none ? "" : key.tostring());}}

}

C 鍵盤鉤子

鍵盤鉤子是一種可以監控鍵盤操作的指令,我們去釣魚只要魚兒上鉤 不管它怎麼逃,只要掌控好鉤子上的繩子總是可以找到這條魚,鍵盤 鉤子是利用電腦一行行執行 特性,在目的視窗處理鍵 前攔截 把某個指令替換為另外一種指令,然後再把訊息傳送給目的視窗這樣 乙個週期下來,視窗程式會認為使用者輸入的就是現在的數值或...

VC全域性鍵盤鉤子

hodll.h main header file for the hodll dll if defined afx hodll h b2a458dc 71e2 47d5 9ea0 58385d558643 included define afx hodll h b2a458dc 71e2 47d5 ...

c 全域性鍵盤鉤子

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