••Key press at any time. HELP!••

I want to make a simple game using the console and X,Y coordinates. I haven't started on this project because if I can't figure out how to make the computer recognize a key (Like the up arrow.) at any time to make the character move on the screen then the game won't work.

I'm asking: how do I make a key be recognized any time in a loop and have it do something?

Thank you for your help in advance.
Yeah you can use stuffs like this:

1
2
3
4
5
6
char temp;

temp=getch();   //temp = any key you press!

if(GetAsyncKeyState(VK_DOWN)   //if the key pressed was the down arrow key, execute...
{...........}


or you can download this game's .cpp file and look at how stuffs work!
http://dl.dropbox.com/u/43721327/C%2B%2B/Skeptor/Skeptor-Code.zip

Hope it HELPS!!!
Last edited on
It doesn't do what I want is there a way to make it so it doesn't pause and have it where WHENEVER you press the up key it does something.
OK I read that, but I don't care I just want to learn how to go that so I can later use it with the free game engine I got.
A free game engine will have a different way of getting input than anything you'll do in the console... so if that's your goal... then learning how to do this in the console is a complete waste of time.
OK. Any ways I want to know how to do it. Can someone just answer me and stop telling me to not do it this way.
I know there is a way to do it because I have seen other programs made on the console that are games.
Well then GetAsyncKeyState is what you want (if you're on Windows).

GetAsyncKeyState alone will let you check the realtime state of any key, and it is non-blocking, meanting it will return immediately (not causing your program to stop).
OK. Tell me more. Show me an example please.
Never mind I found out how to do it with http://www.cplusplus.com/forum/windows/6632/.
Thank you for your help. This is the code I have so far. I think I'm going to make a maze with something chasing you.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <iostream>
#include <string>
#include <stdio.h>
#include <windows.h>
#include <ctime>
#include <vector>
#include <cstdlib>
#include <conio.h>
using namespace std;

void gotoxy (int x, int y)
	{
	    COORD coord; // coordinates
	    coord.X = x; coord.Y = y; // X and Y coordinates
	    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord); // moves to the coordinates
	}


int main(){
	unsigned short int X=5;
	unsigned short int Y=5;
	while ( 1 ){
	char temp;
	if(GetAsyncKeyState(VK_DOWN))   //if the key pressed was the down arrow key, execute...
	{
		if (Y != 24){
		Y = Y + 1;
		}
		else continue;
	}
	if(GetAsyncKeyState(VK_UP))
	{
		if (Y != 0){
		Y = Y - 1;
		}
		else continue;
	}
	if(GetAsyncKeyState(VK_LEFT))
	{
		if (X != 1){
		X = X - 2;
		}
		else continue;
	}
	if(GetAsyncKeyState(VK_RIGHT))
	{
		if (X != 79){
		X = X + 2;
		}
		else continue;
	}
		system("CLS");
		gotoxy (X,Y);
		cout << X << ", " << Y;
	}
}
Glad it's working.

Now if you'll excuse me I'm going to go in the corner and cry because of this console nonsense.
What?
It just makes me sad to see effort wasted on console games. So I'm crying in a corner.
OK. It turns out I'm just going to make a program that you use arrow keys to move to words to start folders and shutdown the computer.
Topic archived. No new replies allowed.