WASD Key movement c++

Hey everyone, first board post here. Taking my first c++ class and i need help with my dungeon crawler i am building. So far i have my character movement set up to take in a string press enter and move depending on string entered. I want to use just the WASD keys, which i have it set up for but the user needs to hit enter afterwards. So this noob needs to know how to get around this problem. So i have


string input
if (input=="w")
Preform my code through the array to play the game.
I saw the input=getch(); somewhere online i don't know exactly what it does though.

Any help or references are greatly appreciated. Needs to be all console based.
GetAsyncKeyState
Here, take a look at it

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
#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
    char ch=0;
    cout << "Press Q to quit\n";
    do
    {
        ch = getch();

        switch(ch)
        {
            case 'W':
            case 'w':
                cout << "W was pressed \n";
                break;
            case 'A':
            case 'a':
                cout << "A was pressed \n";
                break;
            case 's':
            case 'S':
                cout << "S was pressed \n";
                break;
            case 'D':
            case 'd':
                cout << "D was pressed \n";
                break;

        }

    }while (ch != 'Q' && ch!='q');
}
Just learned about this one today. getch(); captures one key stroke and you dont need to press enter. we made a rock paper scissors program, if u want the code i can give it to you
closed account (zwA4jE8b)
1
2
3
4
5
6
7
8
if (_kbhit())
			{
				_input = _getch();
				if (_input == _up || _input == _down || _input == _right || _input == _left)
					_snake1._direction = _input;
				else if (_input == 'p')
					pause();
			}


this is from my first game.

if(_kbhit()) checks to see if the keyboard is hit.

_up, _down, _left, _right are just const char _right = 'd', _left = 'a', _up = 'w', _down = 's';
Last edited on
Just keep in mind that if you use "getch()" like that you'll want to flush\sync the input buffer at the end of your loop with: std::cin.sync();.
Pegleg, it seems that you might not have the experience (no offense, of course) to understand those replies, so let me simplify it for you:

1) Make sure you have #include<conio.h>
2) set any char variable equal to "_getch();" to take-in a single char without needing to press enter.
1
2
char x;
x = _getch();

3) Optionally, you can add
std::cin.sync();
after each _getch(); to increase stability. although you don't have to.

I don't mean to condescend, I just remember what it was like looking at these forums when I was starting out.
Thanks for the help everyone. I ended up using the switch statement since when i was declaring char x then setting x=getch(); it was saying it had no stored variable type.
Topic archived. No new replies allowed.