c#**
[dllimport("user32.dll", charset = charset.auto,
callingconvention = callingconvention.stdcall, setlasterror = true)]
private static extern int setwindowshookex(
int idhook,
hookproc lpfn,
intptr hmod,
int dwthreadid);
[dllimport("user32.dll", charset = charset.auto,
callingconvention = callingconvention.stdcall, setlasterror = true)]
private static extern int unhookwindowshookex(int idhook);
[dllimport("user32.dll", charset = charset.auto,
callingconvention = callingconvention.stdcall)]
private static extern int callnexthookex(
int idhook,
int ncode,
int wparam,
intptr lparam);
下面是有關這兩個low-level hook在winuser.h中的定義:
///
/// windows nt/2000/xp: installs a hook procedure that monitors low-level mouse input events.
///
private const int wh_mouse_ll = 14;
///
/// windows nt/2000/xp: installs a hook procedure that monitors low-level keyboard input events.
///
private const int wh_keyboard_ll = 13;
在安裝全域性鉤子的時候,我們就要做替換了,將wh_mouse和wh_keyborad分別換成wh_mouse_ll和wh_keyborad_ll:
//install hook
hmousehook = setwindowshookex(
wh_mouse_ll, //原來是wh_mouse
mousehookprocedure,
marshal.gethinstance(
assembly.getexecutingassembly().getmodules()[0]),
0);
//install hook
hkeyboardhook = setwindowshookex(
wh_keyboard_ll, //原來是wh_keyborad
keyboardhookprocedure,
marshal.gethinstance(
assembly.getexecutingassembly().getmodules()[0]),
0);
這樣替換了之後,我們就可以實現全域性鉤子了,而且,不需要寫dll。看一下程式運**況:
下面是關於滑鼠和鍵盤的兩個callback函式:
private int mousehookproc(int ncode, int wparam, intptr lparam)
//double clicks
int clickcount = 0;
if (button != mousebuttons.none)
if (wparam == wm_lbuttondblclk || wparam == wm_rbuttondblclk) clickcount = 2;
else clickcount = 1;
//generate event
mouseeventargs e = new mouseeventargs(
button,
clickcount,
mousehookstruct.pt.x,
mousehookstruct.pt.y,
mousedelta);
//raise it
onmouseactivity(this, e);
} //call next hook
return callnexthookex(hmousehook, ncode, wparam, lparam);
} private int keyboardhookproc(int ncode, int32 wparam, intptr lparam)
// raise keypress
if (keypress != null && wparam == wm_keydown)
} // raise keyup
if (keyup != null && (wparam == wm_keyup || wparam == wm_syskeyup))
} if (handled)
return 1;
else
return callnexthookex(hkeyboardhook, ncode, wparam, lparam);
}
C 全域性滑鼠鍵盤Hook
原文出自 using system using system.collections.generic using system.reflection using system.runtime.interopservices using system.text using system.windows...
C 全域性滑鼠鍵盤Hook 備查
using system using system.collections.generic using system.reflection using system.runtime.interopservices using system.text using system.windows.form...
如何在WPF中使用虛擬鍵盤
在使用wpf做觸屏功能開發時,發現有時候對觸屏支援不甚友好,特別是虛擬鍵盤。於是上網找資料,發現一般有兩種方法,一種是開啟c program files common files microsoft shared ink 目錄下的taptip.exe程式,可後來發現該方法實現概率太低。所以沒有實行。...