Making my c++ program run while other programs do

I want to make a program that runs at the same time other programs do. I want the program to run in the background, and at the same time. So it works as normal, while others work as normal.
Heres the code-

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <conio.h>
#include <iostream>

using namespace std;

int main()
{
    loops:
    while(1)
    {
        if(getch())
        {
            cout << "42";
            break;
        }
    }
    goto loops;
}


It works like i want when its the selected program... Not so when its not selected.
How can I make it run at the same time?
It works like i want when its the selected program...
How exactly do you want it to behave?
(why do you have a goto-style loop surrounding a normal loop? Ditch that goto nonsense)

I want to make a program that runs at the same time other programs do. I want the program to run in the background, and at the same time


Your program is already doing this. It's called multitasking -- and every modern OS does it automatically. There's nothing special you need to do.


I *think* what you mean is you want input to be received when your window doesn't have focus. This is trickier, and will probably need to be done by interfacing with the OS's API rather than with standard libs or with fugly conio.

I'm assuming you're using Windows, right? You can get the realtime status of any single keypress with GetASyncKeyState, but it only lets you check one key at a time. I'm not sure how you'd go about seeing if any key was pressed.

This sounds kind of fishy though. Why would you want this? The only thing I can think of would be for something like a keylogger, and if that's the case I'm not going to waste my time researching to help.
We do hope you're not trying to write a keylogger for purposes other than your own personal use.

Also, we don't like using goto here because it makes someone's code hard to read. Sometimes it's hard to follow those tags. For something like this, I guess it's fine, but otherwise... actually, just don't use it.

In the event that you are writing a malicious keylogger, the link I gave above was removed by me. Sorry, but something IS fishy. Why exactly do you want to do this?

-Albatross
Last edited on
I've used GetAsyncKeyState() in the past to implement global hotkeys.
OP is obviously trying to make a keylogger.
I've used goto in what I'd say was a valid way before. Unconditional jumps are very fast so it can be better to goto somewhere rather than duplicate code. Also, if you're in a nested loop for whatever reason and you need to break out of both loops, you can only get out with return or goto; and if you need to execute code after both loops are done, the only way you can really do it is to use goto.
I don't think goto is the only way to break nested loops. Thought I don't want to argue with the ones more experience than me.
Well, it's not, it's just the least convoluted way I can think of. I guess you could do this:

1
2
3
4
5
6
7
8
9
10
11
bool outerloop = true;
while (outerloop) {
        bool innerloop = true;
        while (innerloop) {
                ...
                if (escape())
                        outerloop = false;
                innerloop = false;
                ...
        }
}


But I think goto is better there:
1
2
3
4
5
6
7
8
9
10
while (...) {
        do {
                ...
                if (escape())
                        goto endwhile;
        } while (...);
}

endwhile:
        std::cout << "Woohoo!\n";


The first method also doesn't work when instead of a nested loop you have a switch statement. You could replace that with an if/else-if ladder but let's not talk about those...
Last edited on
...
I feel so...
soo...
...
I guess the word is DISGUSTED
a keylogger?
really?

And I'll be using GOTO. I know how it works... basically.

And... wait, getch() can be used to sense WICH buttons are pushed?!?!?!
...
ok, anyway.
its not a keylogger- there are tutorials for that.
And I want to do this, because EVENTUALLY I want to make a simpol game that you push (nearly) any button to try and keep you carractor alive.
>.>
Its gunna take me a while to learn what I need.
ESPECIALLY if people decide, out of the THOUSANDS of things I could be asking for, that I'm making a keylogger.


Jeez...
OK, I'll be looking for a differenf forrum. Obviously, You guys don't like noobs very much.
Sorry about that, maybe we misinterpret you. Also I don't hate beginners, since I've been a beginner.
To be honest, if that's your reaction to well-justified suspicion then I can't imagine you'll be missed.
And I want to do this, because EVENTUALLY I want to make a simpol game that you push (nearly) any button to try and keep you carractor alive.


Well when you do this, you hopefully won't be using the console (since the console is a terrible medium for games). So any advice we give you on how to accomplish this in the console will be impossible to use for your game.

How you do it depends on what library you're using. So I can't really give you a definitive answer without selling you a specific library.

I'm also still not sure why you'd need to be able to accept keyboard input when your window doesn't have focus for a game. Usually that behavior is undesirable in a game (I often switch between games and chat programs, and if my typed words were messing up my game in the background I'd be very upset).

And I'll be using GOTO. I know how it works... basically.


Okay. It's your program. It really is fugly though, and a very bad habit to get into. I really would recommend against it.

ESPECIALLY if people decide, out of the THOUSANDS of things I could be asking for, that I'm making a keylogger.


I see a lot of people looking for that. Your post seemed like you were asking about it, and you didn't say specifically what you were working on, so it seemed like a reasonable conclusion for me to reach.

Note that I didn't assume you were working on a keylogger, I speculated that you might be. If my response was something like:

We don't help people who are making keyloggers. Piss off.


Then I can see you getting upset. But my original reply was not at all offensive or impolite, and I refuse to apologize for it.

OK, I'll be looking for a differenf forrum. Obviously, You guys don't like noobs very much.


Now who's making assumptions?

I make a concious effort to reach out to people looking for help on these forums.

Grow a longer fuse. Don't read so much into things. If you're really leaving the forums, that's your call. But if you want to stick around we'd all be still willing to help you.
OK, I like Disch.
yep...
I have a short fuse. My only justification is because I've been keylogged(?) before, so I don't like it.

Ok, so if that game doesn't work with Console...
...
I don't know how to use anything but console
@.@
Where might I find a non-console tutorial?
They all seem to be console...


I don't think I'll be leaving though, the other forums look strange.

Eventually I'll quit using GoTo, but not now. I'm just weird that way.


so anyway, good non-console tut?
OK, I like Disch.


Thanks =) You seem pretty cool too, now that we better understand each other.

I'm glad you decided to stick around.

Eventually I'll quit using GoTo, but not now. I'm just weird that way.


Fair enough.

so anyway, good non-console tut?


Well when you leave the console you have to pick a library.

For games, I would recommend SFML. It's pretty simple and easy to use (but still very powerful and fast): http://www.sfml-dev.org/

Note that you'll have to actually install the library on your machine and set it up, which is a somewhat difficult process for a first timer. Here are tutorials on how to set it up in VS and MinGW: http://www.sfml-dev.org/tutorials/1.6/ (see the "Getting Started" section)

Setting it up will be a big hassle, but you'll only have to do it once. It'll be worth it in the long run.

Once you have it set up, you can use this introduction tutorial as an example of a basic program: http://www.sfml-dev.org/documentation/1.6/

As for getting keystates when your program doesn't have focus... I *think* SFML can do it, but I'm not entirely sure. It looks like the Input class allows you to get realtime states of any key (http://www.sfml-dev.org/documentation/1.6/classsf_1_1Input.htm#5312ed524e1f2837aa4597978553876a <-- see sf::Input::IsKeyDown). Whether or not that works when your program doesn't have focus, I couldn't say. I would assume it does... but I don't really know.
Last edited on
Topic archived. No new replies allowed.