Need directions to current information on arrow key input

Hi,

Normally, I would ask for specific help, but I've been googling the topic of accepting arrow keys for input in a compiler based program for 2 hours, and either a library needed isn't recognized, or C only, or OS specific, or perhaps just beyond my current scope of understanding. So, instead, can anyone just point me to a good, usable source or tutorial on accepting arrow key, or worst case a source on not requiring enter to be pressed.

Thank you

the only way I know of to read without getting an enter key is to use nonstandard tools. getch and getche are what I use most often for that. There are more complex ways to do this but I don't recommend you go there. You can peek and clear the input buffer with some effort, if you want a true c++ no library solution.

nothing is "c only". You can make all c code work with a c++ program as far as I know. It may be ugly to do this.

as for arrow keys, you need to write a mini program to see what you are getting when one is pressed, probably as a numeric value(s). Once you see what it is sending, you can trap it.
Last edited on
@jjordon33

Check here for a small program I wrote that uses the four arrow directional keys.

http://www.cplusplus.com/forum/general/248025/

EDIT: Removed tags
Last edited on
@whitenite, He wants something that is not OS specific. Something like ncurses.
(And why would you put the link in tags? It can't be clicked!)
@dutch

I put the link in tags just to highlight it. Corrected it.

I have been working with ncurses since I posted this. It seemed like the best I was going to find. I forgot all about posting this. I'm about to check out whitenite's now.

Thank you for the tips so far.
Hm.

Whitenite, your conio include works. I swear to you gents that I tried using conio earlier today, and my compiler thought I was speaking pig-latin.

You care if I strip your code down to just the arrow key input for study and implementation into a personal non profit project of mine?
@jjordan33

Feel free to use any of the code for any purpose you need. I'm glad it can be of use to you.
Thanks man.

I think I stripped it down too much. I get absolutely nothing happening upon my arrow key presses. Do you see what I need to put back in?

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
#include <iostream>
#include <conio.h>
#include <ctime>
#include <string>
#include <Windows.h>

using namespace std;

#define ENTER 13
#define UP 72
#define LEFT 75
#define RIGHT 77
#define DOWN 80

int main()
{
    int ch;

        ch = _getch();
       
        switch (ch) {

            case LEFT: {
                
                cout << "LEFT" << endl;

                break;
            }

            case RIGHT: {
               
                cout << "right" << endl;

                break;
            }

            case UP: {
               
                cout << "up" << endl;
               
                break;
            }

            case DOWN: {

                cout << "down" << endl;

            }

                break;

            case ENTER: {

                break;

            }

        }
        
    return 0;
}


****edit****

The do while loop. I fixed that.
The only issue I'm having now is it works correctly in the little dos window that my antivirus pops up, but it doesn't work in my compiler, which is CLion. Is there any reason that should be doing that?
Last edited on
@jjordan33

I don't know anything about the Mac, (If that's what you're using) to be of any help. Sorry. Unless the CLion doesn't like the #include <Windows> , which, of course, you don't really need in that program you have. I needed it for the pause function and the gotoXY function, which, neither you're using.
Last edited on
Topic archived. No new replies allowed.