Scan keyboard for input

Hi. I've been challenging myself to create a mini console game, where you move a character to avoid falling beads. I've been plotting it out on notes on my iPad, and it should give the context for my question:

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
*3 columns to move between
  *     *        
              *
  *     O
------------------

class Player 
==
VARIABLES
nameOfPlayer
Int position
FUNCTIONS
String getName()
Int getposition()
void moveleft()
Void moveright()

class Bead
==
VARIABLES
Int[] position
FUNCTION
Constructor (determines entry position at random)
Void drop()
Get Xpos
Get ypos

class Console()
==
VARIABLES
FUNCTIONS
draw()
takeUserInput();
Getuserunput();
+=+=+=+=+=+=

main() 
While (no bead is in the same location as the player) 
    Wait .75 seconds;
    Take user input;
    Drop beads;
    Move player;
    Draw board;
End while;

Display: game over!!!;


My problem is that I would like to input whether to go left or right using the arrow keys. I obviously cannot use cin or a similar console input because the program needs to continue regardless of whether or not I input a key. How do I surmount this issue? Thanks!
Last edited on
@heyyouyesyouiloveyou
Here is a small program that shows how to use _getch(), and gotoXY(). You should be able to adapt this to your program.

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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include <iostream> 
#include <Windows.h> 
#include <conio.h> 

void gotoXY(int, int); 
void removeCursor(void);

//Global scope variables 
//int posX(5), posY(10);
int x = 5, y=10; // Set where to start printing
int key; // Changed to an integer, instead of a char
#define ENTER 13
#define UP 72
#define LEFT 75
#define RIGHT 77
#define DOWN 80

int main()
{
 removeCursor();

 std::cout<<"\tUse arrow keys: \x1a \x1b \x19 \x18";
   gotoXY(x, y);
	 std::cout<<((char)('\x01'));
 do	
 {
	key = _getch();
	switch(key)
	{
	case RIGHT:
	 gotoXY(x, y);
	 std::cout<<' ';
	 x++;
	 gotoXY(x, y);
	 std::cout<<((char)('\x01'));
	 break;
	case LEFT:
	 gotoXY(x, y);
	 std::cout<<' ';
	 x--;
	 gotoXY(x, y);
	 std::cout<<((char)('\x01'));
	 break;
	case UP:
	 gotoXY(x, y);
	 std::cout<<' ';
	 y--;
	 gotoXY(x, y);
	 std::cout<<((char)('\x01'));
	 break;
	case DOWN:
	 gotoXY(x, y);
	 std::cout<<' ';
	 y++;
	 gotoXY(x, y);
	 std::cout<<((char)('\x01'));

	}
 }while(key != ENTER);
 return 0;
}

void gotoXY(int x, int y){
 COORD c;
 c.X = x, c.Y = y;
 SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), c);
}

void removeCursor(void){
 HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
 CONSOLE_CURSOR_INFO cci;
 cci.dwSize = 1; 
 cci.bVisible = false; //false = invisible
 SetConsoleCursorInfo(handle, &cci);
}
I'm very sorry, I fought to mention that I'm using a Mac. In place of Windows.h I can use ncurses, correct? I'm not sure about conio.h, however.
@heyyouyesyouiloveyou

Yes, you could probably use ncurses, but I'm not positive, as I know nothing about the Mac. I'm sure there are users here, that do use it, and can steer you in the right direction. Good luck..
@whitenite1
Thank you for your help!


Is there anyone that can help steer me in the right direction? Thanks.
Just a quick follow up:

I've done some looking into ncurses, and I have found that is offers both the functionality to get input in the way I want and the functionality to move the cursor around the console.

Thanks!
Topic archived. No new replies allowed.