Underlying keyboard hook

Source: Internet
Author: User

Author: quicksand in the wind Source: http://blog.vckbase.com/flowsand/

 

It's not a good thing if the hard disk is too big.

/* ===================================================== ========================================
File: kbevent. h
Description: global message hook
Time: 2005-03-20
Write: oshj | oshj@21cn.com
Environment: vc6.0/Win2000 Pro/SP4/1024*768
Note: nt5.0 and above are supported, but not Win9x!
========================================================== ======================================= */
//-------------------------------------------------------------------------
# Ifndef _ inc_keevent
# DEFINE _ inc_keevent

# Include <windows. h>
# Include <stdio. h>
//-------------------------------------------------------------------------
# Define dllexport extern "C" _ declspec (dllexport)
//-------------------------------------------------------------------------
# Define MAX_TEXTLEN 1024
# Define UM_GETTEXT WM_USER + 0x392
//-------------------------------------------------------------------------
// Low-level keyboard hook callback function
Lresult callback LowLevelKBProc (int nCode, WPARAM wParam, LPARAM lParam );
// The System Manager is invalid when pressing ctrl + alt + del
Void RegKeyEnable (BOOL bEnable = TRUE );

//////////////////////////////////////// ///////////////////////////////////
// Keyboard void time callback function
Lresult callback MyKbdHook (int code, WPARAM wParam, LPARAM lParam );
// Mouse void time callback function
Lresult callback MyMouseHook (int code, WPARAM wParam, LPARAM lParam );
//-------------------------------------------------------------------------

# Endif // _ INC_KEEVENT

//. Cpp

//-------------------------------------------------------------------------
# Define _ WIN32_WINNT 0x0500 // available only for systems above NT5.0

# Include "kbevent. h"
//-------------------------------------------------------------------------
// Magic registry key/value for "Remove Task Manager" policy.
# Define HKCU HKEY_CURRENT_USER
LPCTSTR KEY_DisableTaskMgr =
"Software // Microsoft // Windows // CurrentVersion // Policies // System ";
LPCTSTR VAL_DisableTaskMgr = "DisableTaskMgr ";
//-------------------------------------------------------------------------
# Pragma data_seg (". sdata ")
HHOOK g_hHookKeybd = NULL;
HHOOK g_hHookMouse = NULL;
DWORD g_dwLastInputTick = 0;
# Pragma data_seg ()
# Pragma comment (linker, "-Section:. sdata, RWS ")
//-------------------------------------------------------------------------
// Global variable
Hinstance g_hinstance = NULL; // module instance handle
Hhook g_hhook = NULL; // hook handle
//-------------------------------------------------------------------------
// DLL main
Bool winapi dllmain (hinstance hinstdll, DWORD fdwreason, lpvoid lpvreserved)
{
// Save the module instance handle
G_hinstance = (hinstance) hinstdll;

Switch (fdwreason)
{
Case dll_process_attach:
Break;

Case dll_thread_attach:
Break;

Case dll_thread_detach:
Break;

Case dll_process_detach:
If (null! = G_hhook)
Unhookwindowshookex (g_hhook );
If (null! = G_hhookkeybd)
Unhookwindowshookex (g_hhookkeybd );
If (null! = G_hhookmouse)
Unhookwindowshookex (g_hhookmouse );
Break;
}
Return true;
}

//-------------------------------------------------------------------------
// Underlying keyboard Hook Function
Lresult callback LowLevelKBProc (int nCode, WPARAM wParam, LPARAM lParam)
{
If (nCode = HC_ACTION)
{
PKBDLLHOOKSTRUCT p;

Switch (wParam)
{
Case WM_KEYDOWN:
Case WM_SYSKEYDOWN:
Case WM_KEYUP:
Case WM_SYSKEYUP:
P = (PKBDLLHOOKSTRUCT) lParam;

If (// (p-> vkCode = VK_TAB) |
(P-> vkCode = VK_ESCAPE) |
(P-> vkCode = VK_LWIN) |
(P-> vkCode = VK_RWIN) |
(P-> vkCode = VK_CONTROL) |
(P-> vkCode = VK_SHIFT) |
(P-> vkCode = VK_DELETE) |
(P-> vkCode = VK_F4) & (p-> flags & LLKHF_ALTDOWN )! = 0) | // ALT + F4
(P-> vkCode = VK_TAB) & (p-> flags & LLKHF_ALTDOWN )! = 0) | // ALT + TAB
(P-> vkCode = VK_ESCAPE) & (p-> flags & LLKHF_ALTDOWN )! = 0) | // ALT + ESC
(P-> vkCode = VK_ESCAPE) & (GetKeyState (VK_CONTROL) & 0x8000 )! = 0) | // CTRL + ESC
(P-> vkCode = VK_DELETE) & (p-> flags & LLKHF_ALTDOWN )! = 0) & (GetKeyState (VK_CONTROL) & 0x8000 )! = 0) // CTRL + ESC

)
{
Return TRUE;
}

Break;
}

}

Return: CallNextHookEx (g_hHook, nCode, wParam, lParam );

}

//-------------------------------------------------------------------------
// Start the hook
Dllexport bool winapi StartKBEvent ()
{
// If the keyboard Hook has been installed, FALSE is returned.
If (NULL! = G_hHook) return FALSE;

RegKeyEnable (FALSE );
// Install the underlying keyboard hook
// NT5.0 or above can be supported
// WIN98 not supported!
G_hHook = SetWindowsHookEx (WH_KEYBOARD_LL, LowLevelKBProc, g_hInstance, NULL );
 
If (g_hHook = NULL) return FALSE;

Return TRUE;

}
 
//-------------------------------------------------------------------------
// End hook
Dllexport bool winapi StopKBEvent ()
{
RegKeyEnable (TRUE );
// Uninstall the hook
If (UnhookWindowsHookEx (g_hHook) = 0)
Return FALSE;
G_hHook = NULL;

Return TRUE;

}
//-------------------------------------------------------------------------
// The System Manager is invalid when pressing ctrl + alt + del
Void RegKeyEnable (BOOL bEnable)
{
HKEY hk;
If (RegOpenKey (HKCU, KEY_DisableTaskMgr, & hk )! = ERROR_SUCCESS)
{
Regcreatekey (hkcu, key_disabletaskmgr, & HK );
}
//
If (true = benable)
{
Regdeletevalue (HK, val_disabletaskmgr );

}
Else
{
DWORD val = 1;
Regsetvalueex (HK, val_disabletaskmgr, null, REG_DWORD, (byte *) & Val, sizeof (VAL ));

}

}

//////////////////////////////////////// ///////////////////////////////////
// The following is the hook for obtaining the idle time of the keyboard and mouse.
//-------------------------------------------------------------------------
// Keyboard idle time callback function
Lresult callback mykbdhook (INT code, wparam, lparam)
{
If (code = hc_action)
{
G_dwlastinputtick = gettickcount ();
Pkbdllhookstruct P;
P = (pkbdllhookstruct) lparam;
// MessageBox (0, "111", "AAA", mb_ OK );
// Press the z key more than twice in a row
If (P-> vkcode = vk_escape) // & (P-> flags & kf_repeat)> = 2 ))
{
// MessageBox (0, "yes", "AAA", mb_ OK );
Return true;
}
}
Return: callnexthookex (g_hhookkeybd, code, wparam, lparam );
}
//-------------------------------------------------------------------------
// Mouse idle time callback function
Lresult callback mymousehook (INT code, wparam, lparam)
{
If (code = hc_action)
{
G_dwlastinputtick = gettickcount ();
}
Return: callnexthookex (g_hhookmouse, code, wparam, lparam );
}
//-------------------------------------------------------------------------
// Start the hook
Dllexport bool winapi idleinit ()
{
If (null! = G_hhookkeybd & null! = G_hhookmouse) return false;

G_hhookkeybd = setwindowshookex (wh_keyboard, mykbdhook, g_hinstance, 0 );
G_hhookmouse = setwindowshookex (wh_mouse, mymousehook, g_hinstance, 0 );
G_dwlastinputtick = gettickcount ();

Return true;

}
//-------------------------------------------------------------------------
// End hook
Dllexport bool winapi idleexit ()
{
If (NULL = g_hHookKeybd & NULL = g_hHookMouse) return FALSE;

UnhookWindowsHookEx (g_hHookKeybd );
UnhookWindowsHookEx (g_hHookMouse );

Return TRUE;

}

//-------------------------------------------------------------------------
// Obtain the idle time
Dllexport dword IdleGetLastTime ()
{
Return g_dwLastInputTick;
}
 

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.