Hook to shield keyboard and mouse events

Source: Internet
Author: User

(Transfer)

First, create a dynamic link library.

File Name dllhook. cpp

The Code is as follows:

# Include "windows. H"

Hinstance h_st;
Hhook g_keyboardhook;
Hhook g_mousehook;

Hwnd g_wnd;
/* Bool winapi dllmain (
Hinstance hinstdll, // handle to the DLL module
DWORD fdwreason, // reason for calling function
Lpvoid lpvreserved // Reserved
)
{
H_st = hinstdll;
// Return 1;
}*/

Lresult callback mouseproc (
Int ncode, // hook code
Wparam, // message identifier
Lparam // mouse coordinates
)
{
Return 1;
}


Lresult callback keyboardproc (
Int code, // hook code
Wparam, // virtual-key code
Lparam // keystroke-message information
)
{
If (vk_f2 = wparam)
{
: Sendmessage (g_wnd, wm_close, 0, 0 );
Unhookwindowshookex (g_mousehook );
Unhookwindowshookex (g_keyboardhook );
}
Else
{
Return 1;
}
}


Void sethook (hwnd)
{
G_wnd = hwnd;
G_mousehook = setwindowshookex (wh_mouse, mouseproc, getmodulehandle ("hookdll"), 0 );
G_keyboardhook = setwindowshookex (wh_keyboard, keyboardproc, getmodulehandle ("hookdll"), 0 );
}

Then, create a hookdll. Def file under the directory file of the project and use it as a module for export.

Use VC to import this file

Project-> Add project-> files import this file to the Project

The code for this file is as follows:

Library hookdll
Exports
Sethook @ 3

The next step is to create an MFC application. Create a dialog box application named dllhook

Find the onindialog () function under classview,

Make changes

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.