This question already has answers here:
How to detect if keystroke was emulated by keybd_event or SendInput?
(2 answers)
Closed 9 months ago.
I'm developing an application which needs to count user keystrokes. It works fine however user can trick the app with SendInput() WINAPI function. Is it any way to differentiate between keystrokes made by real user and those sent via SendInput?
Set a hook with SetWindowsHookEx with the type WH_KEYBOARD_LL. Your callback can inspect the KBDLLHOOKSTRUCT::flags field. If it has the LLKHF_INJECTED flag set, then it's from SentInput or keybd_event. Otherwise, it's from the local keyboard driver.
Related
This question already has answers here:
Window Title Changed Event
(2 answers)
Closed 4 years ago.
Hello im creating program that check for opened window name but at the moment it check it like 100 times per second and i want to check it every time the window change.I know its propably becasue of infinity loop.But im new in C++ and i have no idea how to do it, can someone help me ?
char wnd_title[256];
while(1) {
HWND hwnd=GetForegroundWindow();
GetWindowText(hwnd,wnd_title,sizeof(wnd_title));
cout << wnd_title;
}
Best regards.
There are several things you can do to achieve that:
Best (thanks to Remy in the comments): "Another option is to use SetWinEventHook() to listen for EVENT_OBJECT_NAMECHANGE events. That is "more promising" than intercepting WM_SETTEXT messages, and less intrusive than "infiltrating"."
simple and boring (and it seems not really what you wanted): set a timer for the request so it is called in longer periods
propably the most promising one: Set up a Window hook with WH_GETMESSAGE to listen for the WM_SETTEXT which is messaged from the SetWindowTextA function
or go full crazy and CreateRemoteThread and infiltrate your enemy with a trusty spy.
This question already has an answer here:
How to get middlemouse click event in C++
(1 answer)
Closed 9 years ago.
I need to detect a middle mouse click event in C++. Does anyone know how I can do so? If there is a way, can someone tell me how I can do it? I'm not so familiar with mouse events in C++. Seems a bit more complicated to do it in C++ as oppose to Java.
Since you are working on windows the following may work for you:
If your application contains a window, then simply look for WM_MBUTTONDOWN in your message loop.
If you dont have a window, then you can use a mouse hook.
Where you check the wParam for WM_MBUTTONDOWN(or WM_BUTTONUP).
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Redirecting cout to a console in windows
I've created a child richedit window with CreateWindow and I wonder is it possible to redirect all cout calls so text would appear in RichEdit controll instead of console ?
As far as I know, you can't do that. You need to send window messages to set the text in the control.
That's not to say you can't use stream syntax. You could define your own ostream and use that instead of cout, then pass bytes into your window. It wouldn't help with any output that you don't generate, nor would it help if you mix printf calls (which you shouldn't really do).
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
C++ console keyboard events
I want a Windows console program to do something if a certain key is pressed down,
something like
while(1)
{
....
if(the key 'o' is pressed down)
....
}
but I don't know what to put in the if statement. How do I check if the key 'o' is pressed down?
I'm using Windows 7 64-bit and Visual Studio Professional 2008.
You can use the std::cin.get() or you can use the windows.h GetAsyncKeyState, depending on what exactly you want to do.
If you want lower level stuff, look into hooks and events from the WinAPI.
Rather than busy-polling for a keypress, you should register for key events in your application (assuming this is a Windows GUI app), and check for the keys you're interested in.
If you are actually making a console app, see here: C++ console keyboard events
This question already has answers here:
Redirecting cout to a console in windows
(11 answers)
Closed 9 years ago.
When you create a C++ console application under Windows you automatically get the console window created for you and std::cout outputs to the console window.
I have a GUI application for which I also want to create a console window. I can create the console window using the AllocConsole() function, but how do I redirect / attach std::cout to the console so that the output appears in the console window?
You want to use the GetStdHandle and SetStdHandle. Given that it is a long, long time since I have done anything similar, you would be better looking at some Some examples
There is also this duplicate question
As far as I know you can't redirect the standard handles to the new console. You'll have to call GetStdHandle(DWORD) to get a handle for each device you want to write to. Using this handle you'll need to call the WriteFile, ReadFile, WriteConsole and ReadConsoleInput functions with the appropriate handle to pass data back and forth.