Windows Mobile keyboard hook shielding buttons

Source: Internet
Author: User

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 );}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.