encapsulated classes are directly added to the project for use.
Public class keyboardhookclass {Public Delegate int hookproc (INT ncode, int wparam, ref keymsg lparam); // declares a proxy for callback functions: Private Static int hkeyboardhook = 0; private hookproc keyboardhookedure; // use user32.dll in Win32 and coredll in Windows Mobile. DLL // set the hook [dllimport ("coredll. DLL ", entrypoint =" setwindowshookex ")] public static extern int setwindowshookex (hooktype idhook, hookproc lpfn, intptr hinstance, int threadid); // hook [dllimport (" coredll. DLL ", entrypoint =" unhookwindowshookex ")] public static extern bool unhookwindowshookex (INT idhook); [dllimport (" coredll. DLL ", entrypoint =" getcurrentthreadid ")] public static extern int getcurrentthreadid (); [dllimport (" coredll. DLL ", entrypoint =" getmodulehandle ")] public static extern intptr getmodulehandle (string name); [dllimport (" coredll. DLL ", entrypoint =" callnexthookex ")] public static extern int callnexthookex (INT idhook, int ncode, int wparam, ref keymsg lparam ); // hook type public Enum hooktype {wh_keyboard = 2, wh_keyboard_ll = 20} // key value enumeration public Enum keyvaluemap: int {vk_tend = 0x73, // Mount key vk_ttalk = 0x72, // dial key wm_keydown = 0x0100, wm_keyup = 0x0101, wm_syskeydown = 0x0104, wm_syskeyup = 0x0105} // The keyboard structure public struct keymsg {public int vkcode; // the key value public int scancode; Public int flags; Public int time; Public int dwextrainfo ;} public void starthook () {// install the keyboard hook try {keyboardhookprocedure = new hookproc (keyboardhookproc); hkeyboardhook = setwindowshookex (hooktype. wh_keyboard_ll, keyboardhookprocedure, intptr. zero, 0);} catch (exception ex) {stophook (); throw new exception (ex. tostring () + "An error occurred while installing the hook. ") ;}} public void stophook () {try {unhookwindowshookex (hkeyboardhook);} catch (exception ex) {Throw new exception (ex. tostring () + "failed to unload hook. ") ;}} // hook callback function private int keyboardhookproc (INT ncode, int wparam, ref keymsg lparam) {If (ncode> = 0) {// keys keydata = (KEYS) wparam; // wparam is used to determine whether to press the key or pop up keymsg M = (keymsg) lparam; If (wparam = (INT) keyvaluemap. wm_keydown | wparam = (INT) keyvaluemap. wm_keyup | wparam = (INT) keyvaluemap. wm_syskeydown | wparam = (INT) keyvaluemap. wm_syskeyup) // key down {// ProcessingCode Switch (M. vkcode) {Case (INT) keyvaluemap. vk_tend: // disable the case (INT) keyvaluemap key. vk_ttalk: {return 1 ;}}} return callnexthookex (hkeyboardhook, ncode, wparam, ref lparam );}}