Check whether key is pressed down in Windows console [duplicate] - c++

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

Related

How to detect when Ctrl+R or tab is pressed on linux?

I am trying to code a custom shell implementation in C++ for linux. In that, I have to implement a functionality which will run when Ctrl+R is pressed, and another when the Tab key is pressed. How do I detect when these two keys are pressed in C++? For Ctrl+C and Ctrl+Z, I used signal() function, but I am at a loss about what to do for these two.

Console application freezes until key pressed / mouse clicked [duplicate]

This question already has answers here:
How and why does QuickEdit mode in Command Prompt freeze applications?
(2 answers)
Closed 4 years ago.
Sometimes it works great, but usually after i started application (after log)- nothing happen. Just after i press key- consumers start to work (that's not displaying problem, because there are messages in queues and they are not going to be free before i press keyboard key).
At this demonstration i started current service. It took some messages and went to sleep. After that i sent more messages (using Web API application), but nothing happen in service. Just after i pressed key in service console window it woke up.
I've just unchecked the Property > Options > Edit Options > QuickEdit Mode checkbox.
Possible duplicate answer: How and why does QuickEdit mode in Command Prompt freeze applications?

Detect middle mouse click event [duplicate]

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).

Using keypress to switch a bool

I am writing a Windows C++ app which I would like to have detect a keypress (for this example, using the letter 'S'). When the key is pressed, the program should switch a bool value either on or off (depending on its current state).
I know that in console apps you can use cin.get, but I'm unfamiliar with the Win32 API. I also would like to be sure that when the key is pressed, the event is only registered once, i.e. if the user presses 'S' but holds the key down for a while, the program should detect only 'S'; not 'SSSSSSS'.
So you have a windows message loop going, right? Capture WM_KEYDOWN and check whether it's on autorepeat.
But I'd guess you are actually using a framework, be it MFC, QT or something else. The framework will wrap your windows message loop and allow you to capture key events, but if you want us to tell you how, you'll have to say what framework you're using.

How to track keyboard input and bypass SendInput [duplicate]

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.