Use System hooks to lock the mouse and keyboard

Source: Internet
Author: User
To intercept messages of all applications in the system, you must write the hook function in the dynamic connection library. Otherwise, the hook can only intercept messages of the application. In fact, this principle is very simple. Let's look at the following API for installing the HOOK: Hhook setwindowshookex (
Int
Idhook ,// Type of hook to install
Hookproc Lpfn ,// Address of hook procedure
Hinstance Hmod ,// Handle to application instance
DWORD Dwthreadid// Identity of thread to install hook
);
Note that the third parameter ( Hinstance hmod) And the fourth parameter ( DWORD dwthreadidThe two parameters indicate a DLL instance handle and the thread ID to intercept messages. If hmod is null, dwthreadid must be specified. If dwthreadid is 0, the hook is valid for all threads, but you must specify hmod. This is because a DLL is an independent thread, it is independent of other applications and can intercept messages from other threads. The following function implements hook installation: //////////////////////////////////////// /////////////////////////////////////
// The keyboard hook install function implement
Dllexport void winapi installhook ()
{
G_keyboardhook = setwindowshookex (wh_keyboard, (hookproc) keyboardhookproc,
Theapp. m_hinstance, 0 );
G_mousehook = setwindowshookex (wh_mouse, (hookproc) mousehookproc,
Theapp. m_hinstance, 0 );
}
Theapp is a global object of the DLL class.The following function uninstalls the HOOK: //////////////////////////////////////// /////////////////////////////////////
// The keyboard hook uninstall function implement
Dllexport void winapi uninstallhook ()
{
Unhookwindowshookex (g_keyboardhook );
Unhookwindowshookex (g_mousehook );
}
The following two functions are hook functions for the keyboard and mouse: //////////////////////////////////////// /////////////////////////////////////
// The keyboard & mouse hook function implement
Dllexport lresult callback keyboardhookproc (INT ncode, wparam, lparam)
{
If (ncode = hc_action)
{
Switch (wparam)
{
Case vk_space:
If (getkeystate (vk_lcontrol) & 0x8000 )! = 0) & (getkeystate (vk_rshift) & 0x8000 )! = 0 ))
{
Beatkeystroke = false;
Beatmousestroke = false;
}
Break;
}
}
Return (beatkeystroke? 1: callnexthookex (g_keyboardhook, ncode, wparam, lparam ));
}
Dllexport lresult callback mousehookproc (INT ncode, wparam, lparam)
{
Return (beatmousestroke? 1: callnexthookex (g_mousehook, ncode, wparam, lparam ));
}
It is very simple. Here we only judge whether the left Ctrl + Right Shift + space is pressed. If it is, it will be unlocked. Finally, introduce the DLL Lib in the application, and call the above function to install the hook in the appropriate position of the application, and then uninstall it. The following is an application that can lock the keyboard and mouse for a period of time: // Timerlock. h
// Head File
// Declare application class and window class
// Lionmountainman
// 2005-8-25
Class cmyapp: Public cwinapp
{
Protected:
Ctime m_time;
Public:
Virtual bool initinstance ();
Virtual int run ();
};
//////////////////////////////////////// //////////////////////////////////////// /////
// Timerlock. cpp
// Implement File
// Implement the timer LOCK keyboard and mouse function.
// Lionmountainman
// 2005-8-25
# Include <afxwin. h>
# Include "timerlock. H"
# Include "hookdll. H"
Cmyapp MyApp; //////////////////////////////////////// //////////////////////////////////////// /////
// Cmyapp member functions
Bool cmyapp: initinstance ()
{
M_time = ctime: getcurrenttime ();
Return true;
}
Int cmyapp: Run ()
{
Bool block = false;
Ctime tmalarm1 (m_time.getyear (), m_time.getmonth (),/
M_time.getday (), 10, 30, 0 );
Ctime tmcancelalarm1 (m_time.getyear (), m_time.getmonth (),/
M_time.getday (), 10, 40, 0 );
Ctime tmalarm2 (m_time.getyear (), m_time.getmonth (),/
M_time.getday (), 15, 30, 0 );
Ctime tmcancelalarm2 (m_time.getyear (), m_time.getmonth (),/
M_time.getday (), 15, 40, 0 );
Ctime tmalarm3 (m_time.getyear (), m_time.getmonth (),/
M_time.getday (), 12, 30, 0 );
Ctime tmcancelalarm3 (m_time.getyear (), m_time.getmonth (),
M_time.getday (), 13, 30, 0 );
Ctime tmalarm4 (m_time.getyear (), m_time.getmonth (),/
M_time.getday (), 17,30, 0 );
Ctime tmcancelalarm4 (m_time.getyear (), m_time.getmonth (),/
M_time.getday (), 18, 0, 0 );

While (1)
{
M_time = ctime: getcurrenttime ();
If (tmalarm1 <= m_time & tmcancelalarm1> = m_time) |/
(Tmalarm2 <= m_time & tmcancelalarm2> = m_time) |/
(Tmalarm3 <= m_time & tmcancelalarm3> = m_time) |/
(Tmalarm4 <= m_time & tmcancelalarm4> = m_time ))
{
If (! Block)
{
Installhook ();
MessageBox (null, "Time is up, stop all operations! "," Tip ", mb_iconstop | mb_topmost );
Block = true;
}
}
Else if (Block)
{
Uninstallhook ();
Block = false;
}
Sleep (1000 );
}
Return 0;
}

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.