Home > Net >  C # class hook, most want to be in a lock screen window screening can jump out of the interface of t
C # class hook, most want to be in a lock screen window screening can jump out of the interface of t

Time:10-22

C # class hook, the following code can run, can call, no error, is doesn't work, solving (on all my points)
The class myHook
{
Private IntPtr pKeyboardHook=IntPtr. Zero;//handle to the keyboard hook
//hook entrusting statement
Public delegate int HookProc (int nCode, Int32 wParam, IntPtr lParam);
Private HookProc KeyboardHookProcedure;//keyboard hook, a delegate instance cannot be omitted variable
Public const int idHook=13;//underlying keyboard hook
//install hooks
[DllImport (" user32. DLL ", CallingConvention=CallingConvention StdCall)]
Public static extern IntPtr SetWindowsHookEx (int idHook, HookProc LPFN, IntPtr pInstance, int threadID);
//uninstall hooks
[DllImport (" user32. DLL ", CallingConvention=CallingConvention StdCall)]
Public static extern bool UnhookWindowsHookEx (IntPtr pHookHandle);

//keyboard hook handler
Private int KeyboardHookProc (int nCode, Int32 wParam, IntPtr lParam)
{
KeyMSG m=(KeyMSG) Marshal PtrToStructure (lParam, typeof (KeyMSG));//keyboard message processing
If (pKeyboardHook!=IntPtr. Zero)//whether hook handle null
{
Switch (((Keys) m. kCode))//judgment Keys
{
Case Keys. LWin://keyboard on the left side of the Win key
Case Keys. RWin://Windows on the right side of the keyboard Keys
Case Keys. Delete://Delete key
Case Keys. Alt://Alt
Case Keys. The Escape://Esc key
Case Keys. The F4://F4 key
Case Keys. Control://Ctrl key
Case Keys. Tab://Tab
return 1;//does not perform any operations
}
}
return 0;
}
//install the hook method
Public bool InsertHook ()
{
IntPtr pIn=(IntPtr) 4194304;//convert 4194304 to handle
If (this. PKeyboardHook==IntPtr. Zero) when there are no hooks in//
{
//create a hook
Enclosing KeyboardHookProcedure=new HookProc (KeyboardHookProc);
//use the SetWindowsHookEx function to install hooks
Enclosing pKeyboardHook=SetWindowsHookEx (idHook KeyboardHookProcedure, pIn, 0).
If (this. PKeyboardHook==IntPtr. Zero)//if the installation hook failure
{
Enclosing UnInsertHook ();//uninstall hooks
return false;
}
}
return true;
}
//uninstall the hook method
Public bool UnInsertHook ()
{
Bool result=true;
If (this. PKeyboardHook!=IntPtr. Zero)//if there is a hook
{
//use UnhookWindowsHookEx functions uninstall hooks
Result=(UnhookWindowsHookEx (enclosing pKeyboardHook) & amp; & The result);
Enclosing pKeyboardHook=IntPtr. Zero;//to empty pointer
}
return result;
}
//keyboard message processing structure
[StructLayout (LayoutKind. Sequential)]
Public struct KeyMSG
{
Public int vkCode;//keys
}
}
}
  •  Tags:  
  • C#
  • Related