Keep a running C++ program console on top? - c++

Let's say I was using cin in my program to allow the user to input into the console. That is simple enough but what if they were typing into, let's say, a web browser and I wanted them to input that into the console at the same time? When I click away the C++ program console window and have something else on top, the input obviously does not go into the console. How can I make it so the console is always running on top so that even when I were to input something into a web browser, it would also go into the console?

in order capture keyboard input when your application is not focused, you need to use windows hook, see:
http://msdn.microsoft.com/en-us/library/ms644959%28v=vs.85%29.aspx#wh_keyboardhook
Example code can be found here:
http://www.codeguru.com/cpp/w-p/system/keyboard/article.php/c5699/Hooking-the-Keyboard.htm

I think you may want to take a look at hooking the keyboard to get the input?

Related

how to handle different input/output in one console?

I am working on a windows sever program using c++, when the program started it stays for several days and output important logs to the default windows console.
now I want to add some control function to the console, like I cound enter something like query or stop, and the program ouput the variable number or stop accept requests. So there is the problem, I got two output stream(log and query response) and one input stream both mixed in one single console.
How do I seperate the three different stream in one single console? Maybe I cound write my own console to replace the default windows console?
I believe this is a very normal need and a lot of server application has implemented this but I could not find any source code...
I know I could use ncurses, but I think ncurses seems too low level for this. Any suggestion will be apprecitated.

how to start console with custom size without flicker?

I know this method of setting console size :
system("mode 128,128");
But when program starts, system at first is trying to create console with standard size and when execution reaches mode command - only then the console window gets my desired size. The question is how to make console according to my needs right from the start.
I'm speaking here of running the program with double click from explorer, so the console belongs to the program.
You might have more luck linking your program as a GUI application rather than command-line, calling AllocConsole() to create a console, and then SetConsoleWindowInfo() to resize it. The console would still get created before the resizing, but because you are using the API calls directly, the delay may be small enough to not be noticeable.
If you do this, you may need to do some setup to connect the C and C++ standard input/output/error to the console you created. For that, take a look at _fdopen() and ios_base::sync_with_stdio().

Windows Console2 coolType

As you can see in u413.com: Text comes on the screen, letter by letter, which looks kind of cool,
I want to do the same thing in Console
I got the source code of version 1.2; because I dont need all the complexity of version 2;
I just need a simple Command Prompt, with text that comes on to the screen letter by letter.
I dont need most of the builtin functions offered by Console, like Transparency, Taskbar Icon, etc.
The source code base is pretty small with only about 5 files.
The main class file seems to be Console.cpp;
Since console is like a GUI application, things dont get written to the STDOUT.
but heres what happens;
A Handle is called;
And that handle apparently writes to the console;
m_hStdOut = ::GetStdHandle(STD_OUTPUT_HANDLE);
Now what I want to be able to do; Is somehow read the handle; see what text it has; And throw in a loop with a ::Sleep(20) method in it; to make sure that text appears letter by letter.
#Alf P. Steinbach I wrote the psuedo code for making the sleep command(in java) I also made use of it in every other java program I wrote, but the disadvantage was that it will work only for my programs and not every program run in the command prompt, but what I dont know is code to make a console, a windows console subsystem program, I wish it was possible with java, so that I could use it on linux too, but now, let me ask you exactly what I had in mind...
A program which simply, took input from the screen, send it to cmd.exe for processing, and sent back the reply, and all I have to do is throw in a sleep command between every character...
All I need is help, in getting this done, I wish you could start me up with this, and possible provide links and refrences to get this done...
This Console.cpp seems like a console emulator, meaning it runs OTHER programs. You don't need the source code, you can run existing console applications.
STD_INPUT_HANDLE, STD_OUTPUT_HANDLE and STD_ERROR_HANDLE are the windows handles for STDIN, STDOUT and STDERR. They basicly does the same with only a few differences in how to use it.
If you want to integrate the console into your code, then you have to understand it, and find its own output function and call that. But it won't be any standard handle or things like that.

The problem of attachconsole

I need to make a windows GUI application can run in console, so I attach the console to the process when the application is invoked with a command line. However, after the application exit, the console prompt with the path doesn't show unless the ENTER key is pressed. Is there any way that the prompt with the path can directly show up without pressing the enter key? Thanks.
Short answer: This is not possible.
Long answer: Well, it is sort of possible if you are willing to relax your requirements a little bit. You basically have three options:
What you have done already. You can attach GUI application to a console but cmd.exe will not wait for your application to exit.
Create a GUI application and open console in the GUI application. The console will only last as long as you application.
Or you can restructure your application/source a bit and provide two executables, GUI one that starts GUI directly, another that is console executable.
In C#, I use SendKeys.SendWait("{ENTER}"); to do that. I think in C++, the keybd_event function does something similar.
Like Autodesk Maya with MayaBatch, you can build a small console application which basically run your GUI application with CreateProcess and wait with WaitForSingleObject.
You will have to use this "batch" version of your application in the console.

Async Console Output

I have a problem with my application win32 console.
The console is used to give commands to my application. However, at the same time it is used to output log messages which mostly comes from asynchronous threads. This becomes a problem when the user tries to write some input and simultaneously an async log message is printed, thus thrashing the display of the users input.
I would like to have some advice in regards to how to handle such a situtation?
Is it possible for example to dedicate the last line in the console to input, similarly to how it looks in the in-game consoles for some games?
You can use SetConsoleMode to disable input echo and line editing mode. You can then echo back input whenever your program is ready to do so. Note that this means you will need to implement things like backspace manually. And don't forget to reset the mode back when you're done with the console!
This is possible using the Console API, but it involves quite a bit of work and all the threads that use the console will have to cooperate by calling your output method rather than directly calling the Console API functions or the runtime library output functions.
The basic idea is to have your common output function write to the console screen buffer, and scroll the buffer in code rather than letting the text flow onto the last line and scroll automatically. As I recall, you'll have to parse the output for newlines and other control characters, and handle them correctly.
You might be able to get away with using "cooked" console input on the last line, although in doing so you risk problems if the user enters more text than will fit on a single line. Also, the user hitting Enter at the end of the line might cause it to scroll up. Probably best in this situation to use raw console input.
You'll want to become very familiar with Windows consoles.
Any time you have asyncronous threads trying to update the same device at once, you are going to have issues like this unless something synchronizes them.
If you have access to everyone's source code, the thing to do would probably be to create some kind of sync object that every task must use to access the console (semaphore, etc).