I use this code for the main loop (my function):
while (running)
{
if(is_close)
{
Log().push_log("close", "message close!", logInfo);
running = active = false;
break;
}
while (PeekMessage(&msg, g_hWnd, 0, 0, PM_REMOVE))
{
std::cout << "Wnd: " << msg.message << std::endl;
if (msg.message == WM_QUIT || msg.message == WM_DESTROY || msg.message == WM_CLOSE)
{
MessageBox(0, "Hello, World", "Hello", MB_OK);
running = false;
}
// TranslateMessage(&msg);
DispatchMessage(&msg);
}
if (running && active)
render.DrawObject(g_hDC);
}
Well, then I use WndProc:
LRESULT CALLBACK GLWindowProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
std::cout << "Wnd Proc: " << msg << std::endl;
return DefWindowProc(hWnd, msg, wParam, lParam);
}
When I'm trying to get the message WM_QUIT, WM_DESTROY, or WM_CLOSE in my function, it does not work. My function does not see messages.
How can I get this message?
PeekMessage or GetMessage will only return messages that were posted to the message queue with PostMessage(). That will never be WM_CLOSE or WM_DESTROY, those messages are sent with SendMessage(), directly delivered to the window procedure and don't go in the message queue. You won't get WM_QUIT unless you've got a PostQuitMessage() call in your code, you don't.
You really do have to write a window procedure for your main window. Simply handling WM_DESTROY and calling PostQuitMessage(0) should be enough.
LRESULT CALLBACK GLWindowProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
if (msg == WM_DESTROY) PostQuitMessage(0);
return DefWindowProc(hWnd, msg, wParam, lParam);
}
You'll now get WM_QUIT in your game loop.
Related
I'm working on an anti-clicker in C++. My code works with all Programs that simulate Mouse Clicks, except for Programs which use code like this:
NativeMethods.SendMessage((IntPtr)processHandle, WM_LBUTTONDOWN, (IntPtr)1, (IntPtr)LParams(y, x));
My code:
while (TRUE) {
struct tagMSG Msg;
HHOOK Mouse = SetWindowsHookEx(WH_MOUSE_LL, MouseProcces, NULL, NULL);
while (GetMessage(&Msg, GameMainWindow, 0, 0)) {
Sleep(5000);
TranslateMessage(&Msg);
DispatchMessageA(&Msg);
}
UnhookWindowsHookEx(Mouse);
}
LRESULT CALLBACK MouseProcces(INT NCode, WPARAM WParam, LPARAM LParam) {
if (WParam != WM_MOUSEMOVE && WParam != WM_MOUSEWHEEL)
{
std::cout << "click" << std::endl;
}
return CallNextHookEx(NULL, NCode, WParam, LParam);
}
I'm looking to rewrite ctrl+c and ctrl+v on my Windows 10 machine to add some additional functionality.
I'm able to copy and paste correctly and have successfully created a keyboard hook to execute my code after these keys are pressed but I'm having an issue after I press ctrl while my program is running, ctrl continuously acts as if it's held down. Even after I terminate the program entirely, ctrl continues to act as if it's being held down until I logout of the computer entirely. What can I do to correct this?
Thanks!
Edit: after doing a bit of messing around, I can conclude any key gets stuck. Shift and caps lock get stuck as well.
#include <Windows.h>
#include <stdio.h>
#include <queue>
using namespace std;
LRESULT CALLBACK KeyboardProc(int nCode, WPARAM wParam, LPARAM lParam) {
PKBDLLHOOKSTRUCT p = (PKBDLLHOOKSTRUCT)lParam;
if (wParam == WM_KEYDOWN) {
if (p->vkCode == 0x43 && GetKeyState(VK_CONTROL) & 0x8000) { // ctrl-c is pressed
WM_COPY;
}
else if (p->vkCode == 0x56 && GetKeyState(VK_CONTROL) & 0x8000) { // ctrl-v is pressed
OpenClipboard(NULL);
char* buffer;
buffer = (char*)GetClipboardData(CF_TEXT);
CloseClipboard();
cout << buffer;
}
return CallNextHookEx(NULL, nCode, wParam, lParam);
}
}
int main()
{
HHOOK keyBoard = SetWindowsHookEx(WH_KEYBOARD_LL, KeyboardProc, NULL, NULL);
MSG msg;
while (!GetMessage(&msg, NULL, NULL, NULL)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
UnhookWindowsHookEx(keyBoard);
}
Do not put return CallNextHookEx(NULL, nCode, wParam, lParam) in if (wParam == WM_KEYDOWN).
Modify:
LRESULT CALLBACK KeyboardProc(int nCode, WPARAM wParam, LPARAM lParam) {
PKBDLLHOOKSTRUCT p = (PKBDLLHOOKSTRUCT)lParam;
if (wParam == WM_KEYDOWN) {
if (p->vkCode == 0x43 && GetKeyState(VK_CONTROL) & 0x8000) { // ctrl-c is pressed
WM_COPY;
}
else if (p->vkCode == 0x56 && GetKeyState(VK_CONTROL) & 0x8000) { // ctrl-v is pressed
OpenClipboard(NULL);
char* buffer;
buffer = (char*)GetClipboardData(CF_TEXT);
CloseClipboard();
cout << buffer;
}
}
return CallNextHookEx(NULL, nCode, wParam, lParam);
}
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);
}
I am trying to intercept mouse wheel by child windows under cursor. But something is wrong. It seems like message sends many times. What did I do wrong?
LRESULT CALLBACK MouseProc(__in int nCode,
__in WPARAM wParam,
__in LPARAM lParam)
{
LRESULT ret = 0;
static BOOL b = TRUE;
if (wParam == WM_MOUSEWHEEL)
{
if (b)
{
MOUSEHOOKSTRUCTEX *pMhs = (MOUSEHOOKSTRUCTEX *)lParam;
short zDelta = HIWORD(pMhs->mouseData);
POINT pt;
GetCursorPos(&pt);
LPARAM lParam = MAKELPARAM(pt.x, pt.y);
HWND hWnd = WindowFromPoint(pt);
b = FALSE;
SendMessage(hWnd, WM_MOUSEWHEEL, zDelta, lParam);
}
else
{
b = TRUE;
}
ret = 1;
}
else
{
CallNextHookEx(0, nCode, wParam, lParam);
}
return ret;
}
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;
}