Main Menu Key C++ - c++

I am creating a simple C++ console program for fun and I am looking for a way to allow a user to return to a “Main Menu” output at any time if they were to hit a function key like F1 or F2.
Is there anyway I can have my program be at the ready to accept this key at any time or do I need to have a switch statement that can call the “Main Menu” print function everytime the program accepts user input? Is this something that is perhaps out of reach for a console-based program?
EDIT: For now, I am only working on a Windows-based program.

If you are using Windows:
You can use GetKeyState or GetAsyncKeyState, but that won't give you keydown/keyup events. It will only tell you what keys are currently down.
So if you need to get the keydown/keyup events, you could install a hook. A Console window has a window handle that is owned by code in Windows and a message pump, also owned by code in Windows.
You can get the window handle of of the console window by using GetConsoleWindowThen install a WH_CALLWNDPROC hook using SetWindowsHookEx to listen in on messages send to the console window.
You might try a WH_MSGFILTER hook instead. I don't know if this works for console windows, but it would generate less messages to be ignored if it does work.

Related

How to make C++ to execute a function when I close the console

I want a C++ program to execute a function when I close manually the console.
I made a C++ program that test a password and if it isn't correct make the windows to log off.
But if I close the console from the "X" button nothing happens and I want to make windows to log off too if the console is closed from "X" button?
I tried _onexit_t oe() function but it doesn't help me.
So there is a method to do that or to hide the bar which contains the "Minimize", "Maximize" and "Close" buttons?
Assuming you mean the normal textual console window, you can register your own event handler via SetConsoleCtrlHandler and watch for the events CTRL_C_EVENT, CTRL_BREAK_EVENT, CTRL_CLOSE_EVENT, CTRL_LOGOFF_EVENT, CTRL_SHUTDOWN_EVENT, etc.

Qt - c++ simulate keypress

I'm trying to create a simple bot in Qt and need therefor a way of simulating keyboard presses OUTSIDE the Qt application itself.
I've successfully made this possible by using the "old" keybd_event
keybd_event(Qt::Key_A,0,0, 0); // Pressing the 'A-button"
and that works out fine. But i can't make it when i'm trying to execute a 'select all' command which needs two buttons to be pressed at once.
As i researched the problem on Google i was refereed to the 'SendInput' function with the message 'This function (keybd_event) has been superseded. Use SendInput instead.'
The problem now is that I've little knowledge of windows API and especially in the contex of "Qt" and would like guidance on how to get started.
keybd_event is actually not Qt function, but part of Windows Api.
Both keybd_event and SendInput allow you to send press event and release event. If you want to send combination ctrl+A you should send events as follows:
press Ctrl -> press A -> release A -> release Ctrl
If you want to use keybd_event, you need to call it 4 times subsequently, if you want to use SendInput, you can make an array of 4 events.
You should use keyboard codes from Windows API to simulate keyboard events, while Qt's codes may coincide with Microsoft's.
Also you should understand that this solution has nothing to do with Qt, it Windows specified.
You just found all links to docs you would need, I think you should start studing it and ask more concrete questions, if you would have any problems.

How do I manipulate input from the keyboard in Win32?

Or to clarify this question, how can I make Windows think I hit a key, when I really didn't? I know I could possibly use SendMessage and specify the input there, but then wouldn't only my application receive it? I'd like control to the extent of all applications receiving the "fake" input. Any advice?
What you describe, faking input, is implemented by the SendInput function. Input goes to the thread which has input focus.
You can SendMessage to whatever window you want, even on other processes. You can even use HWND_BROADCAST to send it to every to-level window on the system. But is that what you really want? If you're only interested in a specific program, you can get its window's handle using FindWindow, and then send the message only to that window.
Note that if all you want to do is a simple keystrokes injection into another process, then SendInput is indeed the way to go. If you'd like to send some global keyboard shortcut it doesn't matter who has the focus. If you'd like to send the same input to more than one window using SendInput, you'll have to loop over the list of windows, and for each window first set the focus and then send the input.

c++ get other windows messages

im learning to make things to other windows like resize the ie or any type of window. the only problem i don't know how i can get or give messages to other windows.
so like i pressed a key in ie i would like to get that message to my program too!
any idea
To get the messages that are sent to windows programs you have to install a hook in order to listen to the messages you want. You do this via the SetWindowsHookEx function.
However, I believe that you should read a book about this kind of behaviour, since there are certain rules you have to apply. For instance, before returning from your callback function, you have to call CallNextHookEx in order to let the other hooks handle the message. This is the first hit in books.google.com when searching for setwindowshookex.
Normally your application won't receive Windows messages for other applications (this can be a security problem, for example with keylogger spyware). However, the CBT Hook method can be used to install a hook that receives other window message inputs.
However, note that a Windows feature called UIPI can cause problems with CBT hooks.

C++ console keyboard events

Is there any way to get key events in a Windows console? I need a way to get keydown and keyup events quickly without a GUI. I've tried using getch(), but it doesn't get keyups and waits until a key has been pressed to return.
Use ReadConsoleInput() API. Watch for events of kind KEY_EVENT. This won't work for all keydown events (Ctrl-key, shift-key, Pause-key cannot be read), but most can be read.
Use GetNumberOfConsoleInputEvents to avoid blocking.
You can use GetKeyState or GetAsyncKeyState, but that won't give you keydown/keyup events. It will only tell you what keys are currently down.
So if you really need to get the keydown/keyup events, you could install a hook.
A Console window has a window handle that is owned by code in Windows and a message pump, also owned by code in Windows.
You can get the window handle of of the console window by using GetConsoleWindowThen install a WH_CALLWNDPROC hook using SetWindowsHookEx to listen in on messages send to the console window.
You might try a WH_MSGFILTER hook instead. I don't know if this works for console windows, but it would generate less messages to be ignored if it does work.
I was just curious, how comes such a logical question doesn't have any explanation on Google,
So one has to ask it here. So I googled for: "keyboard events console application" and
guess what ... first 2 links are interesting (but unfortunately, not exactly answers to your question):
Processing mouse / keyboard input on MSDN.
Console event handlers (like Ctrl-C and window close button).
There are a number of ways. GetKeyboardState would be one of the most obvious.
You can also try SetConsoleCtrlHandler