How to: Bind a keyboard event, Hide console, Work with clipboard?

Firstly is there a cross-platform way to work with the clipboard using C++? There are windows functions but these can only be used on windows OS.

Secondly, how do I hide the console window but have the C++ program active? You'll get to know why I want this in the next paragraph.

And lastly how do I bind keyboard events? Like a certain combination ctrl+a, or say even just the key 'a'. So every time I press this key, a certain part of the C++ program must execute? Remember that C++ would be in the background so it must work even thought it's not the active window.

If there is no easy way to do that then what's an alternative to communicate between C++ and the clipboard when a keyboard event is observed?


What's the purpose of this? I'm using an OCR program that has the ability of copying the output to clipboard. After this output is gotten, I want to work with the output using C++.

The platforms are quite different. C++ itself has no concept of "clipboard".

One could write an abstract interface and implement it in multiple platforms.
Some have done so. One example:
http://doc.qt.io/qt-5/qclipboard.html#details
Secondly, how do I hide the console window but have the C++ program active? You'll get to know why I want this in the next paragraph.

The simple answer is - don't run it from a console window. Most OS's have a way to automatically start an application when the OS starts, or when the user logs on, for example. Or else, you can have it started by clicking a desktop icon, but have no visible GUI.

And lastly how do I bind keyboard events? Like a certain combination ctrl+a, or say even just the key 'a'. So every time I press this key, a certain part of the C++ program must execute? Remember that C++ would be in the background so it must work even thought it's not the active window.

I would be extremely wary of hijacking keyboard events to perform actions in a program, when that program isn't active, because that's basically changing the way the user interacts with other applications, and with the OS itself.

In your example, what if I'm editing a document, and I type "Ctrl-A" because I know that's the convention (on Windows) for selecting all the text. Suddenly, that no longer works, because you've hijacked that key combo for your own app that's running in the background. You've taken away part of the functionality of something else I'm used to using.

Now, there's probably a way to forward that event onto the active app, so your program does whatever Ctrl-A makes it do, and then my editor also gets the Ctrl-A to select all the text. But your app has no idea what I wanted that Ctrl-A to do. Did I do it because I wanted to select all text? Did I do it because I wanted your app to do whatever it does on Ctrl-A? Did I do it because I wanted to do both?

Your proposal would change the way a user expects to be able to interact with their other software, and with their OS, in ways that are likely to be confusing and frustrating. I would avoid doing that.
@MikeyBoy when I compile an executable for C++, I get a console window, how do I avoid this console (of course I would only do this if I had keyboard keys being binded)?


Okay here's a question unrelated to the previous ones yet about the same thing: How do I (in general) get C++ to recognize key pushes?

Suppose I have a menu, I would want the user to be able to navigate it with his arrow keys.

Do I have to rely on loops? Consider that I might have other stuff happening on the console so I can't halt everything for input, so in that case would I need to use multiple threads?

A loop like this with no delay can be very resource demanding, so we need to add a small delay.

But is there a way to check if the button had been pressed by using windows functions somehow? In that case we can eliminate relying on the fastness of the loop. So we can even have a 1 second delay in the loop, the loop is only there waiting for the user's input.

I hope I'm making sense ;-;
Last edited on
@Nwb

Have a look at the Qt framework, or any other GUI system, all of your questions seem to be related to GUI's.

I am not sure if it is 100% cross compilable.

https://en.cppreference.com/w/cpp/links/libs
@MikeyBoy when I compile an executable for C++, I get a console window

No. The console window does not come from the C++, nor from the compiler.

What's probably happening is that when you use your IDE to run a console app, the IDE starts a console, and runs the app from that console.

If you run your app some other way, you won't magically get a new console appearing.
Nwb wrote:
when I compile an executable for C++, I get a console window


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;

int main()
{
   int N;
   cout << "How many command windows would you like (be reasonable, you'll have to close them afterwards!): ";
   cin >> N;
   for ( int i = 1; i <= N; i++ ) 
   {
      string cmd =  "start ";
      cmd += '"' + string( "THIS IS CONSOLE NUMBER " ) + to_string( i ) + '"';
      system( cmd.c_str() );
   }
}

@lastchance that's fine and all my dude but how do I make it so that 0 console windows open when the executable is launched yet the program still runs in the background?

I don't think I will bother about trying to use C++ though honestly, better I just use some automation scripting language or tool itself. Because it would be too complex to do it with C++ and I could use something simple.
how do I make it so that 0 console windows open when the executable is launched yet the program still runs in the background?

As you've already been told, just don't use your IDE to launch it. It's your IDE that's creating the console, not your program.

How do you want to launch the program? From a desktop icon? Automatically, when the user logs in? Some other way?

Last edited on
Nwb wrote:
how do I make it so that 0 console windows open when the executable is launched


start /B progname.exe
What if I don't want to have to start the program from the command line? Anyways thanks it won't be of much use now anyway and I don't think hiding the console would have any real use unless we used C++ for binding keys to do stuff while working on another window but that doesn't seem to be something that can be done easily with C++.

Bump following post:
Nwb wrote:

Okay here's a question unrelated to the previous ones yet about the same thing: How do I (in general) get C++ to recognize key pushes?

Suppose I have a menu, I would want the user to be able to navigate it with his arrow keys.

Do I have to rely on loops? Consider that I might have other stuff happening on the console so I can't halt everything for input, so in that case would I need to use multiple threads?

A loop like this with no delay can be very resource demanding, so we need to add a small delay.

But is there a way to check if the button had been pressed by using windows functions somehow? In that case we can eliminate relying on the fastness of the loop. So we can even have a 1 second delay in the loop, the loop is only there waiting for the user's input.

I hope I'm making sense ;-;
Last edited on
What if I don't want to have to start the program from the command line?

I can only repeat my previous question: how do you want to start it?

Bump following post:

Already answered by TheIdeasMan - use an existing GUI framework to do event handling. There's no point whatsoever trying to re-implement an event handling framework yourself, and you'll almost certainly make a worse job of it if you try.
there are 2 ways to do it using extended OS specific c++. You can make a gui program and not show the gui, and it will be an invisible program seen only in the task manager. there is also a weird way to make a command line program do this, but I can't recall (try google windows c++ visual studio hidden console) or words to that effect to find the settings to do it. None of this is pure c++. Not sure how to do it on linux, the gui trick works there and console programs can be started without a console eaiser on unix by hand.

a batch file can start a program but the console is going to appear if you didnt use something to hide it, in windows, and the batch file itself may open one. I don't know what all you can do to a shortcut, why not google it or play with it to see if you can do it?
Nwb wrote:
What's the purpose of this? I'm using an OCR program that has the ability of copying the output to clipboard. After this output is gotten, I want to work with the output using C++.


OCR program copies output to clipboard.

Open notepad and paste the clipboard. Save the file.

Read the file in C++. Or whatever. Even notepad!



Probably the OCR program can write directly to file instead of the clipboard anyway. Which cuts out the middleman.
Last edited on
@lastchance, for the purpose I'm using this for, it will be time consuming.

To give a bit more perspective, what I am performing OCR on is a log of sorts. So with what I had in mind while writing this post, I could just use the hotkey for the OCR and another hotkey to automatically process the clipboard and write it to a text file.

But with what you're suggesting however, I need to OCR, open text file, paste, and repeat.

And I might need to do this regularly so I need a faster alternative. So what I decided is that I'll instead use an automation scripting language which was in fact designed for tasks exactly like this one (I know of two right now: autoit and autohotkey, I have used autohotkey before and its pretty good, don't know about autoit).

@MikeyBoy Ideally I would have wanted to start the executable like you would start an executable.. by clicking on it ;p, but there's no point now I won't have any use of hiding the console anymore I've scraped that idea. But I get how I would have done it.

But I will definitely look into trying to handle events with Qt, it's just I wanted to know if there was something more simple, Qt is a GUI framework, I'm just working with console, so I would have to bring Qt to the field just because I wanted one functionality to observe keyboard events.

Thanks for everybody's contribution.
Qt is a GUI framework, I'm just working with console,


I think this is the disconnect you are having with everybody who has responded.

You say you are just working with console, but you don't want a console (or want to hide it). That is no different than saying you are working with a GUI framework but don't want a GUI.

GUI frameworks give you a lot of event handling capability that console programming does not. The console by default handles key events by placing characters in the console. GUI frameworks give you the option to do much more with keystrokes.

What is it about GUI frameworks that you don't want, or what is it about console that you do want? Is it just the learning curve for the GUI, or is there something else?
it sounds like he wants a driver / system tray / background type process to me. Is that what you want??

btw in windows, you can do an awful lot with system 'clip.exe' if you are feeling lazy. I don't remember if yuo can read it this way, but you can write to it that way. Its not really a serious answer, but its useful for shorty programs.
Last edited on
Thanks guys but as I said I think it's easier to work with something designed for automations like this instead of messing with c++.
Topic archived. No new replies allowed.