CODE:#include "pch.h"
HHOOK hMouseHook;
HHOOK KeyBoard;
int thread_mouse_create = 0;
int thread_keyboard_create = 0;
int thread_keyboard_block_create = 0;
int thread_mouse_block_create = 0;
int scroll = 0;
int keyboard = 0;
int mouseData = 0;
int keyboardData = 0;
int block_key = 0;
int block_mouse_flag = 0;
// блокировка клавы и мыши
LRESULT CALLBACK LLKeyProc_Block(int nCode, WPARAM wParam, LPARAM lParam)
{
if (block_key == 1)return -1;
return CallNextHookEx(NULL, nCode, wParam, lParam);
}
LRESULT CALLBACK MouseProc_Block(int nCode, WPARAM wParam, LPARAM lParam)
{
if (block_mouse_flag == 1)return -1;
return CallNextHookEx(NULL, nCode, wParam, lParam);
}
// слежение за нажатыми клавишами клавиатуры и мыши
LRESULT CALLBACK LLKeyProc(int nCode, WPARAM wParam, LPARAM lParam)
{
switch (wParam)
{
case WM_KEYDOWN:
keyboard = 1;
keyboardData = ((LPKBDLLHOOKSTRUCT)lParam)->vkCode;
break;
case WM_SYSKEYDOWN:
keyboard = 1;
keyboardData = ((LPKBDLLHOOKSTRUCT)lParam)->vkCode;
break;
}
return CallNextHookEx(NULL, nCode, wParam, lParam);
}
LRESULT CALLBACK LowLevelMouseProc(int nCode, WPARAM wParam, LPARAM lParam)
{
MOUSEHOOKSTRUCT* pMouseStruct = (MOUSEHOOKSTRUCT*)lParam;
MSLLHOOKSTRUCT* hook = (MSLLHOOKSTRUCT*)lParam;
if (pMouseStruct != NULL)
{
if (wParam == WM_MOUSEWHEEL)
{
scroll = 1;
mouseData = hook->mouseData;
}
}
return CallNextHookEx(hMouseHook, nCode, wParam, lParam);
}
DWORD WINAPI MyKeyBoard_Block(LPVOID lpParm)
{
KeyBoard = SetWindowsHookEx(WH_KEYBOARD_LL, LLKeyProc_Block, GetModuleHandle(NULL), 0);
MSG message;
while (GetMessage(&message, NULL, 0, 0) > 0)
{
TranslateMessage(&message);
DispatchMessage(&message);
}
UnhookWindowsHookEx(KeyBoard);
return 0;
}
DWORD WINAPI MyMouse_Block(LPVOID lpParm)
{
hMouseHook = SetWindowsHookEx(WH_MOUSE_LL, MouseProc_Block, NULL, 0);
MSG message;
while (GetMessage(&message, NULL, 0, 0) > 0)
{
TranslateMessage(&message);
DispatchMessage(&message);
}
UnhookWindowsHookEx(hMouseHook);
return 0;
}
DWORD WINAPI MyKeyBoardLogger(LPVOID lpParm)
{
KeyBoard = SetWindowsHookEx(WH_KEYBOARD_LL, LLKeyProc, GetModuleHandle(NULL), 0);
MSG message;
while (GetMessage(&message, NULL, 0, 0) > 0)
{
TranslateMessage(&message);
DispatchMessage(&message);
}
UnhookWindowsHookEx(KeyBoard);
return 0;
}
DWORD WINAPI MyMouseLogger(LPVOID lpParm)
{
hMouseHook = SetWindowsHookEx(WH_MOUSE_LL, LowLevelMouseProc, NULL, 0);
MSG message;
while (GetMessage(&message, NULL, 0, 0) > 0)
{
TranslateMessage(&message);
DispatchMessage(&message);
}
UnhookWindowsHookEx(hMouseHook);
return 0;
}
extern "C" __declspec(dllexport)int block_keyboard(int block)
{
if (thread_keyboard_block_create == 0)
{
HANDLE hThread = CreateThread(NULL, 0, MyKeyBoard_Block, NULL, 0, NULL);
if (hThread != NULL)
{
thread_keyboard_block_create = 1;
}
}
if (block == 1)block_key = 1;
else block_key = 0;
return 0;
}
extern "C" __declspec(dllexport)int block_mouse(int block)
{
if (thread_mouse_block_create == 0)
{
HANDLE hThread = CreateThread(NULL, 0, MyMouse_Block, NULL, 0, NULL);
if (hThread != NULL)
{
thread_mouse_block_create = 1;
}
}
if (block == 1)block_mouse_flag = 1;
else block_mouse_flag = 0;
return 0;
}
extern "C" __declspec(dllexport)int hook_WH_MOUSE_LL()
{
if (thread_mouse_create == 0)
{
HANDLE hThread = CreateThread(NULL, 0, MyMouseLogger, NULL, 0, NULL);
if (hThread != NULL)
{
thread_mouse_create = 1;
return 1;
}
}
scroll = 0;
return 0;
}
extern "C" __declspec(dllexport)int hook_WH_KEYBOARD_LL()
{
if (thread_keyboard_create == 0)
{
HANDLE hThread = CreateThread(NULL, 0, MyKeyBoardLogger, NULL, 0, NULL);
if (hThread != NULL)
{
thread_keyboard_create = 1;
return 1;
}
}
keyboard = 0;
return 0;
}
extern "C" __declspec(dllexport)int scroll_exist(int* wheel_delta)
{
*wheel_delta = mouseData;
return scroll;
}
extern "C" __declspec(dllexport)int key_exist(int* vk_code)
{
*vk_code = keyboardData;
return keyboard;
}