Press enter to export Keyboard Hook message - c++

I've written a program to get keyboard hook. What I want is, when a user presses Enter, the typed text, for example: "hello world", which I stored in exportMsg, should be returned from the function.
I want to make a dll and export exportMsg.
Here is my code. Thanks in advance.
#include <Windows.h>
#include <stdio.h>
#include <iostream>
LRESULT CALLBACK KeyboardProc(int nCode, WPARAM wParam, LPARAM lParam);
HHOOK keyboardHook;
HWND prevWindow;
std::string exportMsg="";
int main()
{
keyboardHook = SetWindowsHookEx(WH_KEYBOARD_LL, KeyboardProc, 0, 0);
MSG msg{ 0 };
while (GetMessage(&msg, NULL, 0, 0) != 0);
UnhookWindowsHookEx(keyboardHook);
return 0;
}
LRESULT CALLBACK KeyboardProc(int nCode, WPARAM wParam, LPARAM lParam)
{
wchar_t title[256];
HWND fwindow = GetForegroundWindow();
PKBDLLHOOKSTRUCT key = (PKBDLLHOOKSTRUCT)lParam;
//a key was pressed
if (wParam == WM_KEYDOWN && nCode == HC_ACTION )
{
//return if enter pressed
if (key->vkCode == '\r')
{
std::cout << exportMsg << std::endl;
}
else
{
exportMsg.push_back(key->vkCode);
}
}
return CallNextHookEx(keyboardHook, nCode, wParam, lParam);
}

As std::cin doesn't work in Unity, I needed to use Keyboard Hook to scan qrCode through a QR Code Scanner Machine and send the scanned string to the Unity.
This algorithm capture the key strokes and returns the string when Enter is pressed.
My DLL code looks like this:
Header file:
LRESULT CALLBACK KeyboardProc(int nCode, WPARAM wParam, LPARAM lParam);
HHOOK keyboardHook;
HWND prevWindow;
std::string exportMsg = "";
QRCODEREADER_EXPORTS_API void StartHook(char* str, int strlen);
Source file:
QRCODEREADER_EXPORTS_API void StartHook(char* str, int strlen)
{
string result;
cout << "please scan the QR code!!!\n";
//std::cin >> result;
exportMsg.clear();
HHOOK keyboardHook = SetWindowsHookEx(WH_KEYBOARD_LL, KeyboardProc, GetModuleHandle(NULL), 0);
if (!keyboardHook) {
std::cout << "Failed to hook keyboard\n";
}
else {
MSG msg{ 0 };
while (GetMessage(&msg, NULL, 0, 0) != 0);
result = exportMsg;
cout << "your data: " << result << "\n";
result = result.substr(0, strlen);
//str = new char[result.length()];
std::copy(result.begin(), result.end(), str);
str[min(strlen - 1, (int)result.size())] = 0;
}
std::cout << "Quitting...\n";
UnhookWindowsHookEx(keyboardHook);
}
LRESULT CALLBACK KeyboardProc(int nCode, WPARAM wParam, LPARAM lParam)
{
HWND fwindow = GetForegroundWindow();
PKBDLLHOOKSTRUCT key = (PKBDLLHOOKSTRUCT)lParam;
//a key was pressed
if (wParam == WM_KEYDOWN && nCode == HC_ACTION)
{
//return if enter pressed
if (key->vkCode == '\r')
{
std::cout << exportMsg << std::endl;
PostQuitMessage(0);
UnhookWindowsHookEx(keyboardHook);
}
else
{
exportMsg.push_back(key->vkCode);
}
}
return CallNextHookEx(keyboardHook, nCode, wParam, lParam);
}
dllmain:
int main() {
char* a;
for (int i = 0; i < 3; i++)
{
a = new char[50];
StartHook(a, 50);
cout<< i << "th iter: " << a << endl;
}
return 0;
}

Related

Error SetWindowsHookEx and GetRawInputData

I have two questions.
First, I wrote a little program to catch all mouse events. I start it in a separate thread, and I get a error which I can not debug:
#include <windows.h>
#include <iostream>
#include <thread>
HHOOK hookHandle;
LRESULT CALLBACK callBackHook(int nCode, WPARAM wParam, LPARAM lParam) {
if(nCode == HC_ACTION) {
std::cout << "Something!" << std::endl;
}
return CallNextHookEx(hookHandle, nCode,
wParam, lParam);
}
int mouseHook() {
hookHandle = SetWindowsHookEx(WH_MOUSE_LL , callBackHook, NULL, 0);
if(hookHandle == NULL) {
std::cout << "ERROR CREATING HOOK: ";
std::cout << GetLastError() << std::endl;
return 0;
}
MSG msg;
while(GetMessage(&msg, NULL, 0, 0) != 0) {
std::cerr << "message!" << std::endl;
}
UnhookWindowsHookEx(hookHandle);
return 0;
}
int main(int argc, char *argv[])
{
std::thread mouse(mouseHook);
return 0;
}
Error Message (the german buttons say "Cancel", "Retry", "Ignore"):
Second, is it possible to get the raw data input from lParam of the callBackHook function? I don't know how to register an input device without an HWND.
First, you needs to wait the thread exit, use mouse.join(). If the main process returns directly, the thread it owns will also be terminated, which will cause this issue.
int main(int argc, char* argv[])
{
std::thread mouse(mouseHook);
mouse.join();
return 0;
}
Second,
I don't know how to register an input device without an HWND
Don't worry, you could create a Message-Only Window for that.
Sample (remove some error checking):
#include <windows.h>
#include <iostream>
using namespace std;
LRESULT CALLBACK WindProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam)
{
if (Msg == WM_INPUT)
{
cout << "Something!" << endl;
HRAWINPUT hRawInput = (HRAWINPUT)lParam;
}
return DefWindowProc(hWnd, Msg, wParam, lParam);
}
int main()
{
WNDCLASSEX wcx = { 0 };
wcx.cbSize = sizeof(WNDCLASSEX);
wcx.lpfnWndProc = WindProc;
wcx.hInstance = GetModuleHandle(NULL);
wcx.lpszClassName = TEXT("RawInputClass");
RegisterClassEx(&wcx);
HWND hWnd = CreateWindowEx(0, TEXT("RawInputClass"), NULL, 0, 0, 0, 0, 0, HWND_MESSAGE, NULL, GetModuleHandle(NULL), NULL);
RAWINPUTDEVICE rid = { 0 };
rid.usUsagePage = 0x01;
rid.usUsage = 0x02; //mouse
rid.dwFlags = RIDEV_INPUTSINK;
rid.hwndTarget = hWnd;
RegisterRawInputDevices(&rid, 1, sizeof(RAWINPUTDEVICE));
MSG msg;
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}

WM_MOUSEMOVE GET_X_LPARAM AND GET_Y_LPARAM Catch wrong Coordinates

LRESULT CALLBACK MouseProc(int nCode, WPARAM wParam, LPARAM lParam)
{
if (nCode == HC_ACTION)
{
if (wParam == WM_MOUSEMOVE)
{
cout << "X : " << GET_X_LPARAM(lParam) << " Y: " << GET_Y_LPARAM(lParam) << "\n";
}
}
return CallNextHookEx(hMSHook, nCode, wParam, lParam);
}
int _tmain() {
HMODULE hInstance = GetModuleHandle(NULL);
hMSHook = SetWindowsHookEx(WH_MOUSE_LL, MouseProc, hInstance, NULL);
MSG Msg;
while (GetMessage(&Msg, NULL, 0, 0)) { DispatchMessage(&Msg); }
::ReleaseDC(0, dc);
return 0;
}
result = Always return wrong coordinates,
example = X = -1844, Y = 79,
X = -1556 Y = 271,
X = -1028 Y = 91
...
Value is changing when every launch
somebody can help me?
The lParam parameter of MouseProc is not identical to the lParam parameter for WM_MOUSEMOVE. It is a MOUSEHOOKSTRUCT *.
So, change MouseProc to :
LRESULT CALLBACK MouseProc(int nCode, WPARAM wParam, LPARAM lParam)
{
if (nCode == HC_ACTION)
{
auto &ms = * (const MOUSEHOOKSTRUCT *) lParam;
if (wParam == WM_MOUSEMOVE)
{
cout << "X : " << ms.pt.x << " Y: " << ms.pt.y << "\n";
}
}
return CallNextHookEx(hMSHook, nCode, wParam, lParam);
}

SetWindowsHookEx 7e error

I'm having some troubles hooking the process to read keypresses. GetLastError gives 0x7e which means module not found, though it seems I implemented the module finding thing correctly. What I currently have (PId is process id, ThreadId is main process thread id):
HWND hWindow = NULL;
BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM lParam)
{
DWORD lpdwProcessId;
GetWindowThreadProcessId(hwnd, &lpdwProcessId);
if (lpdwProcessId == lParam)
{
hWindow = hwnd;
return FALSE;
}
return TRUE;
}
LRESULT CALLBACK kbCallback(int nCode, WPARAM wParam, LPARAM lParam)
{
KBDLLHOOKSTRUCT kbdStruct = *((KBDLLHOOKSTRUCT *)lParam);
if (wParam == WM_KEYDOWN) {
if (kbdStruct.vkCode = VK_F1) {
std::cout << "f1 was pressed" << std::endl;
}
}
return CallNextHookEx(NULL, nCode, wParam, lParam);
}
int main()
{
...
EnumWindows(EnumWindowsProc, initt.PId);
HHOOK hKbdHook = SetWindowsHookEx(WH_CALLWNDPROC, kbCallback, (HINSTANCE)GetWindowLongPtr(hWindow, GWLP_HINSTANCE), initt.ThreadId);
}

why i can't using this code in CALLBACK Function?

i hope to change static text but i can't,
LRESULT CALLBACK KeybdProc(int nCode, WPARAM wParam, LPARAM lParam)
{
if ((nCode == HC_ACTION) && (wParam == WM_KEYDOWN))
{
KBDLLHOOKSTRUCT *pKey = (KBDLLHOOKSTRUCT*)lParam;
int key = pKey->vkCode;
cout << key << " ";
if (key == VK_INSERT)
{
SetDlgItemText(IDC_STATUSTEXT, "On")
}
if (key == VK_DELETE)
{
SetDlgItemText(IDC_STATUSTEXT, "Off")
}
}
CallNextHookEx(KbHook, nCode, wParam, lParam);
return 0;
}
HHOOK KbHook;
BOOL CUSBDlg::OnInitDialog()
{
CDialog::OnInitDialog();
SetDlgItemText(IDC_STATUSTEXT, "Successfully Loaded!!!");
cout << "Successfully Loaded !!!" << endl;
SetIcon(m_hIcon, TRUE);
SetIcon(m_hIcon, FALSE);
HMODULE hInstance = GetModuleHandle(NULL);
KbHook = SetWindowsHookEx(WH_KEYBOARD_LL, KeybdProc, hInstance, 0);
return TRUE;
}
the error "function does not take 2 parameters"
can i fix it?
and can i using this function
void CUSBDlg::Pray(byte t, int i)
in KeybdProc?
if i try to using this function like this
LRESULT CALLBACK KeybdProc(int nCode, WPARAM wParam, LPARAM lParam)
{
if ((nCode == HC_ACTION) && (wParam == WM_KEYDOWN))
{
KBDLLHOOKSTRUCT *pKey = (KBDLLHOOKSTRUCT*)lParam;
int key = pKey->vkCode;
cout << key << " ";
if (key == VK_INSERT)
{
SetDlgItemText(IDC_STATUSTEXT, "On")
Pray(0x04, 6)
}
if (key == VK_DELETE)
{
SetDlgItemText(IDC_STATUSTEXT, "Off")
}
}
CallNextHookEx(KbHook, nCode, wParam, lParam);
return 0;
}
the error "undeclared identifier"
somebody help me for fixing this problem X(
Your callback is not a non-static method of your dialog class, so it does not have a this pointer with which to call the CWnd::SetDlgItemText() and CUSBDlg::Prey() methods. You are getting an "undeclared identifier" error on Prey() because there is no function in scope with that name. And you are getting the "function does not take 2 parameters" error because the callback is trying to call the Win32 API SetDlgItemText() function, which is in scope but takes 3 parameters as input.
To solve both problems, you need to store a global pointer to your dialog so the callback can reach it when needed, eg:
CUSBDlg *Dlg;
HHOOK KbHook;
LRESULT CALLBACK KeybdProc(int nCode, WPARAM wParam, LPARAM lParam)
{
if ((nCode == HC_ACTION) && (wParam == WM_KEYDOWN))
{
KBDLLHOOKSTRUCT *pKey = (KBDLLHOOKSTRUCT*)lParam;
int key = pKey->vkCode;
cout << key << " ";
if (key == VK_INSERT)
{
Dlg->SetDlgItemText(IDC_STATUSTEXT, "On")
Dlg->Pray(0x04, 6)
}
if (key == VK_DELETE)
{
Dlg->SetDlgItemText(IDC_STATUSTEXT, "Off")
}
}
CallNextHookEx(KbHook, nCode, wParam, lParam);
return 0;
}
BOOL CUSBDlg::OnInitDialog()
{
CDialog::OnInitDialog();
...
Dlg = this;
GetModuleHandle(NULL);
KbHook = SetWindowsHookEx(WH_KEYBOARD_LL, KeybdProc, hInstance, 0);
...
}

How to remap keyboard key in c++ with LowLevelKeyboardProc?

I need to remap some of keys like Left Alt but i just disable it so code for disable Left Alt look like this:
LRESULT CALLBACK LowLevelKeyboardProc(int nCode, WPARAM wParam, LPARAM lParam)
{
if (nCode == HC_ACTION)
{
KBDLLHOOKSTRUCT* p = (KBDLLHOOKSTRUCT*) lParam;
if (p->vkCode == VK_LMENU) return 1;
}
return CallNextHookEx(hHook, nCode, wParam, lParam);
}
So I try to remap Left Alt to Left Ctrl and use function like keybd_event and SendMessageA but didn't get nothing.
LRESULT CALLBACK LowLevelKeyboardProc(int nCode, WPARAM wParam, LPARAM lParam)
{
if (nCode == HC_ACTION)
{
KBDLLHOOKSTRUCT* p = (KBDLLHOOKSTRUCT*) lParam;
if (p->vkCode == VK_LMENU)
{
keybd_event(VK_CONTROL, 0, 0, 0);
// or use this is sameSendMessageA(0, WM_KEYUP, VK_CONTROL, 0);
}
}
return CallNextHookEx(hHook, nCode, wParam, lParam);
}
How to remap Left Alt to Left Ctrl?
This is a way to remap Alt to Ctrl works great. Don't forget the default parameter
switches back to alt if you do not define wParam may just be my computer well give it a try
include iostream
include windows.h
using namespace std;
HHOOK hHook = 0;
LRESULT CALLBACK LowLevelKeyboardProc(int nCode, WPARAM wParam, LPARAM lParam)
{
if (nCode == HC_ACTION)
{
KBDLLHOOKSTRUCT* p = (KBDLLHOOKSTRUCT*) lParam;
if (p->vkCode == VK_LMENU) // VK_LMENU = ALT key
{
switch (wParam){
case WM_SYSKEYDOWN :{ // use SYSKEYDOWN
cout << "Key down" << endl;
keybd_event(VK_LCONTROL, 0x1D, KEYEVENTF_EXTENDEDKEY | 0, 0 );
break;
}
case WM_KEYUP: // use regular keyup
{
cout << "Key up" << endl;
keybd_event( VK_LCONTROL, 0x1D, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
return 1;
break;
}
default:
wParam = WM_SYSKEYDOWN; // if you do not specify it changes back to alt
break;
}
return 1;
}
}
return CallNextHookEx(hHook, nCode, wParam, lParam);
}
int WINAPI WinMain (HINSTANCE hThisInstance, HINSTANCE hPrevInstance, LPSTR lpszArgument, int nCmdShow)
{
hHook = SetWindowsHookEx(WH_KEYBOARD_LL, &LowLevelKeyboardProc, hThisInstance, NULL);
if (hHook == NULL)
{
cout << "Error" << endl;
return 1;
}
MSG messages;
while (GetMessage (&messages, NULL, 0, 0))
{
TranslateMessage(&messages);
DispatchMessage(&messages);
}
return messages.wParam;
}