C # locking the keyboard and mouse

Source: Internet
Author: User
My advertisement unit. Please click here. Thank you!

Using system; using system. collections. generic; using system. LINQ; using system. text; using system. runtime. interopservices; using Microsoft. win32; using system. diagnostics; namespace workhelper {class syshook {[dllimport ("USER32")] public static extern bool blockinput (bool isblock); [dllimport (@ "native. DLL ", entrypoint =" fucksyskey ")] private extern static bool fucksyskey (bool enable); Public void Block Keyandmouse (bool B) {blockinput (B); fucksyskey (B); // lock CTRL + ALT + DEL} Public Delegate int hookproc (INT ncode, int wparam, intptr lparam ); static int hhook = 0; Public const int wh_keyboard_ll = 13; // The lowlevel keyboard is intercepted. If it is wh_keyboard = 2, the system keyboard cannot be intercepted, acrobat Reader gets the keyboard before you intercept it. Hookproc keyboardhookprocedure; // keyboard hook Structure Function [structlayout (layoutkind. sequential)] public class keyboardhookstruct {public int vkcode; Public int scancode; Public int flags; Public int time; Public int dwextrainfo ;} # region dllimport // set hook [dllimport ("user32.dll")] public static extern int setwindowshookex (INT idhook, hookproc lpfn, intptr hinstance, int threadid ); [dllimport ("user32.dll", char Set = charset. auto, callingconvention = callingconvention. stdcall)] // remove the hook public static extern bool unhookwindowshookex (INT idhook); [dllimport ("user32.dll")] // call the next hook public static extern int callnexthookex (INT idhook, int ncode, int wparam, intptr lparam); [dllimport ("kernel32.dll")] public static extern int getcurrentthreadid (); [dllimport ("kernel32.dll")] public static extern intptr getmodul Ehandle (string name); Public void hook_start () {// install the keyboard hook if (hhook = 0) {keyboardhookprocedure = new hookproc (keyboardhookproc ); // hhook = setwindowshookex (2, // keyboardhookprocedure, // getmodulehandle (process. getcurrentprocess (). mainmodule. modulename), getcurrentthreadid (); hhook = setwindowshookex (wh_keyboard_ll, keyboardhookprocedure, getmodulehandle (process. getcurrentprocess (). mainmodul E. modulename), 0); // If Hook setting fails. If (hhook = 0) {hook_clear (); // throw new exception ("hook setting failed! ") ;}}// Cancel the hook event public void hook_clear () {bool retkeyboard = true; If (hhook! = 0) {retkeyboard = unhookwindowshookex (hhook); hhook = 0;} // If the hook fails to be removed, if (! Retkeyboard) throw new exception ("unhookwindowshookex failed. ") ;}// here you can add the desired information to process public static int keyboardhookproc (INT ncode, int wparam, intptr lparam) {If (ncode> = 0) {keyboardhookstruct KBH = (keyboardhookstruct) Marshal. ptrtostructure (lparam, typeof (keyboardhookstruct); // MessageBox. show (KBH. vkcode. tostring (); If (KBH. vkcode! = 32) {return 1 ;}} return callnexthookex (hhook, ncode, wparam, lparam) ;}# endregion }}call syshook H = new syshook (); H. hook_start (); // lock the keyboard H. hook_clear (); // unlock H. blockkeyandmouse (true); // lock the keyboard and mouse, including the task manager hotkey

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.