识别用户键的程序用户按下c ++
Program to recognize user keys user pressing c++
我想为 Windows 编写自动更正之类的东西,但问题是我的程序需要扫描一直按下的键,即使程序 window 未处于活动状态。我是 c++ 的新手,所以我不知道该怎么做。将不胜感激所有帮助!
您可以修改下面这段代码,使之成为"that autocorrection for Windows".
#define _WIN32_WINNT 0x0400
#pragma comment( lib, "user32.lib" )
#include <windows.h>
#include <stdio.h>
#include<fstream>
#include<iostream>
HHOOK hKeyboardHook;
int ii=0;
__declspec(dllexport) LRESULT CALLBACK KeyboardEvent (int nCode, WPARAM wParam, LPARAM lParam)
{
DWORD SHIFT_key=0;
DWORD CTRL_key=0;
DWORD ALT_key=0;
std::ofstream out("keylog.txt", std::ios::app);
if ((nCode == HC_ACTION) && ((wParam == WM_SYSKEYDOWN) || (wParam == WM_KEYDOWN)))
{
KBDLLHOOKSTRUCT hooked_key = *((KBDLLHOOKSTRUCT*)lParam);
DWORD dwMsg = 1;
dwMsg += hooked_key.scanCode << 16;
dwMsg += hooked_key.flags << 24;
char lpszKeyName[1024] = {0};
lpszKeyName[0] = ' ';
int i = GetKeyNameText(dwMsg, (lpszKeyName+1),0xFF) + 1;
lpszKeyName[i] = ' ';
ii++; if (ii>=16){
ii=0;
out<<"\n";
std::cout<<"\n";
}
int key = hooked_key.vkCode;
SHIFT_key = GetAsyncKeyState(VK_SHIFT);
CTRL_key = GetAsyncKeyState(VK_CONTROL);
ALT_key = GetAsyncKeyState(VK_MENU);
if (key >= 'A' && key <= 'Z')
{
if (GetAsyncKeyState(VK_SHIFT)>= 0) key +=32;
if (CTRL_key !=0 && key == 'y' )
{
MessageBox(NULL, "CTRL-y was pressed\nLaunch your app here", "H O T K E Y", MB_OK);
CTRL_key=0;
}
if (CTRL_key !=0 && key == 'q' )
{
MessageBox(NULL, "Shutting down", "H O T K E Y", MB_OK);
PostQuitMessage(0);
}
//printf("key = %c\n", key);
SHIFT_key = 0;
CTRL_key = 0;
ALT_key = 0;
}
printf("%s ", lpszKeyName );
out<<lpszKeyName;
out.close();
}
return CallNextHookEx(hKeyboardHook, nCode,wParam,lParam);
}
void MessageLoop()
{
MSG message;
while (GetMessage(&message,NULL,0,0))
{
TranslateMessage( &message );
DispatchMessage( &message );
}
}
DWORD WINAPI my_HotKey(LPVOID lpParm)
{
HINSTANCE hInstance = GetModuleHandle(NULL);
if (!hInstance) hInstance = LoadLibrary((LPCSTR) lpParm);
if (!hInstance) return 1;
hKeyboardHook = SetWindowsHookEx ( WH_KEYBOARD_LL, (HOOKPROC) KeyboardEvent, hInstance, NULL );
MessageLoop();
UnhookWindowsHookEx(hKeyboardHook);
return 0;
}
int main(int argc, char** argv)
{
HANDLE hThread;
DWORD dwThread;
std::cout<<"Keylogging to keylog.txt\n\n";
hThread = CreateThread(NULL,NULL,(LPTHREAD_START_ROUTINE) my_HotKey, (LPVOID) argv[0], NULL, &dwThread);
//ShowWindow(FindWindowA("ConsoleWindowClass", NULL), false);
if (hThread) return WaitForSingleObject(hThread,INFINITE);
else return 1;
}
我想为 Windows 编写自动更正之类的东西,但问题是我的程序需要扫描一直按下的键,即使程序 window 未处于活动状态。我是 c++ 的新手,所以我不知道该怎么做。将不胜感激所有帮助!
您可以修改下面这段代码,使之成为"that autocorrection for Windows".
#define _WIN32_WINNT 0x0400
#pragma comment( lib, "user32.lib" )
#include <windows.h>
#include <stdio.h>
#include<fstream>
#include<iostream>
HHOOK hKeyboardHook;
int ii=0;
__declspec(dllexport) LRESULT CALLBACK KeyboardEvent (int nCode, WPARAM wParam, LPARAM lParam)
{
DWORD SHIFT_key=0;
DWORD CTRL_key=0;
DWORD ALT_key=0;
std::ofstream out("keylog.txt", std::ios::app);
if ((nCode == HC_ACTION) && ((wParam == WM_SYSKEYDOWN) || (wParam == WM_KEYDOWN)))
{
KBDLLHOOKSTRUCT hooked_key = *((KBDLLHOOKSTRUCT*)lParam);
DWORD dwMsg = 1;
dwMsg += hooked_key.scanCode << 16;
dwMsg += hooked_key.flags << 24;
char lpszKeyName[1024] = {0};
lpszKeyName[0] = ' ';
int i = GetKeyNameText(dwMsg, (lpszKeyName+1),0xFF) + 1;
lpszKeyName[i] = ' ';
ii++; if (ii>=16){
ii=0;
out<<"\n";
std::cout<<"\n";
}
int key = hooked_key.vkCode;
SHIFT_key = GetAsyncKeyState(VK_SHIFT);
CTRL_key = GetAsyncKeyState(VK_CONTROL);
ALT_key = GetAsyncKeyState(VK_MENU);
if (key >= 'A' && key <= 'Z')
{
if (GetAsyncKeyState(VK_SHIFT)>= 0) key +=32;
if (CTRL_key !=0 && key == 'y' )
{
MessageBox(NULL, "CTRL-y was pressed\nLaunch your app here", "H O T K E Y", MB_OK);
CTRL_key=0;
}
if (CTRL_key !=0 && key == 'q' )
{
MessageBox(NULL, "Shutting down", "H O T K E Y", MB_OK);
PostQuitMessage(0);
}
//printf("key = %c\n", key);
SHIFT_key = 0;
CTRL_key = 0;
ALT_key = 0;
}
printf("%s ", lpszKeyName );
out<<lpszKeyName;
out.close();
}
return CallNextHookEx(hKeyboardHook, nCode,wParam,lParam);
}
void MessageLoop()
{
MSG message;
while (GetMessage(&message,NULL,0,0))
{
TranslateMessage( &message );
DispatchMessage( &message );
}
}
DWORD WINAPI my_HotKey(LPVOID lpParm)
{
HINSTANCE hInstance = GetModuleHandle(NULL);
if (!hInstance) hInstance = LoadLibrary((LPCSTR) lpParm);
if (!hInstance) return 1;
hKeyboardHook = SetWindowsHookEx ( WH_KEYBOARD_LL, (HOOKPROC) KeyboardEvent, hInstance, NULL );
MessageLoop();
UnhookWindowsHookEx(hKeyboardHook);
return 0;
}
int main(int argc, char** argv)
{
HANDLE hThread;
DWORD dwThread;
std::cout<<"Keylogging to keylog.txt\n\n";
hThread = CreateThread(NULL,NULL,(LPTHREAD_START_ROUTINE) my_HotKey, (LPVOID) argv[0], NULL, &dwThread);
//ShowWindow(FindWindowA("ConsoleWindowClass", NULL), false);
if (hThread) return WaitForSingleObject(hThread,INFINITE);
else return 1;
}