關於滑鼠和鍵盤的全域性獲取的乙個類

2021-03-31 08:56:57 字數 3610 閱讀 1935

用這個類的方法start可以開始捕獲鍵盤和滑鼠的在全域性事件和相應的引數資訊,也就所謂的鉤子程式:

得到滑鼠的訊息的類:

using system;

using system.runtime.interopservices;

using system.reflection;

using system.threading;

using system.windows.forms;

namespace hookglobal

//宣告滑鼠鉤子的封送結構型別

[structlayout(layoutkind.sequential)]

public class mousehookstruct

//裝置鉤子的函式

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

public static extern int setwindowshookex(int idhook, hookproc lpfn, intptr hinstance, int threadid);

//卸下鉤子的函式

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

public static extern bool unhookwindowshookex(int idhook);

//下乙個鉤掛的函式

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

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

public delegate int hookproc(int ncode, int32 wparam, intptr lparam);

///

/// 墨認的建構函式構造當前類的例項.

///

public mousehook()

//析構函式.

~mousehook()

public void start()}}

public void stop()

//如果卸下鉤子失敗

if (!(retmouse)) throw new exception("unhookwindowshookex failed.");

}private int mousehookproc(int ncode, int32 wparam, intptr lparam)

//從**函式中得到滑鼠的資訊

mousehookstruct mymousehookstruct = (mousehookstruct) marshal.ptrtostructure(lparam, typeof(mousehookstruct));

mouseeventargs e=new mouseeventargs(button, clickcount, mymousehookstruct.pt.x, mymousehookstruct.pt.y, 0 );

onmouseactivity(this, e);

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

}得到鍵盤訊息的類:

using system;

using system.runtime.interopservices;

using system.reflection;

using system.threading;

using system.windows.forms;

namespace hookglobal

//裝置鉤子的函式

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

public static extern int setwindowshookex(int idhook, hookproc lpfn, intptr hinstance, int threadid);

//卸下鉤子的函式

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

public static extern bool unhookwindowshookex(int idhook);

//下乙個鉤掛的函式

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

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

[dllimport("user32")]

public static extern int toascii(int uvirtkey, int uscancode, byte lpbkeystate,byte lpwtranskey, int fustate);

[dllimport("user32")]

public static extern int getkeyboardstate(byte pbkeystate);

public delegate int hookproc(int ncode, int32 wparam, intptr lparam);

///

/// 墨認的建構函式構造當前類的例項並自動的執行起來.

///

public keybordhook()

//析構函式.

~keybordhook()

public void start()}}

public void stop()

//如果卸下鉤子失敗

if (!(retkeyboard)) throw new exception("unhookwindowshookex failed.");

}private int keyboardhookproc(int ncode, int32 wparam, intptr lparam)

//引發onkeypressevent

if ( onkeypressevent!=null &&  wparam ==wm_keydown )

}//引發onkeyupevent

if ( onkeyupevent!=null && ( wparam ==wm_keyup || wparam==wm_syskeyup ))

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

}作者blog:http://blog.csdn.***/hbxtlhx/

C 獲取鍵盤和滑鼠操作的時間的類

最近在開發專案時需要實現屏保功能,即使用者在設定的時間內沒有對系統進行操作時,系統將會自動進入屏保狀態。為此封裝了乙個獲取滑鼠鍵盤動作的類,並有乙個方法可以返回使用者多長時間沒有作業系統的時間。如下 public class mousekeyboardoperate dllimport user32...

selenium 滑鼠和鍵盤事件的操作

perform 執行所有actionchains儲存的行為 context click 右擊 double click 雙擊 drag and drop 拖動 move to element 懸停 from selenium import webdriver from selenium.webdri...

pygame 鍵盤和滑鼠事件的處理

所謂事件,就是程式上發生的事。例如使用者按下鍵盤上的某個鍵或者單擊 移動滑鼠,對於這些事件,遊戲程式需要作出反應。如 pygame 影象 圖形繪製 中例子,程式會一直執行下去,直到使用者關閉視窗而產生乙個 quit 事件,pygame 會接收使用者的各種操作 例如按鍵盤上的鍵 移動滑鼠等 產生事件。...