How to get focus? (or Inderect Input?)

July 8, 2011 at 12:50:35
Specs: Windows XP
Okay, so here is my code:

#include <iostream>
#include <fstream>
#include <conio.h>
using namespace std;

int main()
	ofstream file;"file.txt");
	int i;
    for(i=0; i < 99; i++)
		int a = getch();
		file << a << "\n";

It works just fine, the only problem is I can't get it be in focus if I minimize it. So, what I want is person comes to webpage and types a message. Sort of like a C++ chat... My file gets the keys, sends them to the file, but only if you are inputting directly into the command window.

See More: How to get focus? (or Inderect Input?)

Report •

July 9, 2011 at 11:46:05
Not sure what you're doing to be honest, but what you might try is getting a handle to your window and if it's not minimized, process your file needs.

#include "windows.h"

TCHAR cTitle[80];
if(GetConsoleTitle(cTitle, sizeof(cTitle)) > 0) {
   HWND hcon = FindWindow(NULL, cTitle);
   if(hcon != NULL && !IsIconic(hcon)) {
      // file bits

You'll *probably* need some sort of outer control loop so that the program doesn't terminate on you if/when you minimize the window.


Report •

July 9, 2011 at 16:49:02

What about using GetConsoleWindow() instead. That way you don't need a buffer.

The only thing that needs to be done is "#define _WIN32_WINNT 0x0500" (or greater) before including windows.h. GetConsoleTitle() support starts at windows 2000, so the executable will still work on the same OS'.

Report •

July 9, 2011 at 18:54:30
Nice; thanks for the tip. :) I didn't know about that one...

Report •
Related Solutions

Ask Question