ncurses keyboard constants don't work?

Feb 23, 2013 at 6:31pm
Hi,

I've been learning to use ncurses and have run into an issue. I made the following program to copy the program less in ncurses:
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 <string>
#include <fstream>
#include <vector>
#include <ncurses.h>
using namespace std;

int main(int argc, char* argv[])
{
	if(argc != 2)
	{
		cout << "Please give a single valid filename.\n";
		return -1;
	}

	char* filename = argv[1];
	ifstream reader(filename);

	if(! reader)
	{
		cout << "Error opening file!\n";
		return -1;
	}

	vector <string> file;
	while(reader.good())
	{
		string buffer;
		getline(reader, buffer);
		file.push_back(buffer);
	}

	char keyboard = 0;
	int x, y, currentLine = 0;

	initscr();
	keypad(stdscr, true);
	do
	{
		clear();
		getmaxyx(stdscr, y, x);

		for(int i = 0; i < y - 1; i++)
		{
			move(i, 0);
			printw(file[i + currentLine].c_str());
		}
		move(y - 1, 0);
		printw(":");
		refresh();
		
		keyboard = getch();
		if((keyboard == 2) && (currentLine != (file.size() - y)))
			currentLine++;
		else if((keyboard == 3) && (currentLine != 0))
			currentLine--;

	} while((keyboard != 27) && (keyboard != 113));
	endwin();
	return 0;
} 


When I was making it, I used the KEY_UP and KEY_DOWN constants, and nothing happened. So I had the program output the value of keyboard when I pressed the up and down key, and got 2 for the down key and 3 for the up. If I add those numbers, it works fine. Why is it that KEY_UP and KEY_DOWN do not work?

If it's worth anything I'm compiling it using g++ -lncurses less-clone.cpp -o less2.
Last edited on Feb 23, 2013 at 6:33pm
Feb 23, 2013 at 7:16pm
Compile with warnings
g++ -W{all,extra} less-clone.cpp -lncurses -o less2
53: warning: comparison is always false due to limited range of data type [-Wtype-limits]

RTFM int getch(void);
Last edited on Feb 23, 2013 at 7:17pm
Feb 23, 2013 at 7:42pm
That's not hugely useful, could you please go into more detail? I made this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <ncurses.h>

int main()
{
        initscr();
        keypad(stdscr, true);
        printw("UP: %i ", has_key(KEY_UP));
        mvprintw(1, 0, "DOWN: %i ", has_key(KEY_DOWN));
        mvprintw(2, 0, "LEFT: %i", has_key(KEY_LEFT));
        mvprintw(3, 0, "RIGHT: %i", has_key(KEY_RIGHT));
        getch();
        endwin();
        return 0;
}


And it reported all the keys as true (1).
Feb 23, 2013 at 7:56pm
closed account (Dy7SLyTq)
i know what ur problem is. i had the same myself. there is a a datatype called wchar_t which is a bigger version of char. key_anything is too big for char so use that instead
Feb 23, 2013 at 8:03pm
Yeah, I didn't realise it was a char and got confused because I read it had to be at least a short int. Changed it to int and now it is fine. Thanks!

EDIT: Still having issues I'm afraid. Now KEY_ENTER doesn't seem to be working. Again, has_key reports it as true but I press enter and it's not recognised.
Last edited on Feb 23, 2013 at 8:34pm
Feb 23, 2013 at 9:20pm
closed account (Dy7SLyTq)
no, make it a char_t. it doesn't need to be int. we rnt in the c age anymore
Feb 23, 2013 at 9:50pm
@DTSCode
char_t? You mean wchar_t? getch() returns an int so why not store the result in an int?

Feb 25, 2013 at 2:43am
closed account (Dy7SLyTq)
One) yes sorry I mean wchar_t. Two) it always
bugged me using ints as ASCII codes.
Feb 25, 2013 at 10:24am
But getch() doesn't return ASCII codes. ASCII is for characters, not for key buttons.
Feb 25, 2013 at 4:04pm
closed account (Dy7SLyTq)
no, getch returns the ascii int equivalent which when printed turns out the ascii char. the key_anything are escape chars. they are characters. if you make a non ncurses program and ask for input, then hit key_up, you will see its ascii code
Feb 25, 2013 at 4:11pm
You mean the up key has an ASCII code?
Feb 25, 2013 at 4:19pm
closed account (Dy7SLyTq)
yes any key on the keyboard is (with some exceptions like alternate fn buttons) a character and thus has an ascii key code. the only reasn ncurses uses key_fn(int), key_enter, key_up, key_down etc etc, is because the ascii characters for each of those are diffrent between operating systems. ncurses is the attempt to make code involving stuff like that cross-compatible
Feb 26, 2013 at 9:04am
yes any key on the keyboard is (with some exceptions like alternate fn buttons) a character and thus has an ascii key code.


It seems fairly obvious you don't know what ascii is.
Feb 26, 2013 at 3:20pm
closed account (Dy7SLyTq)
yes i do. why would you say that
Feb 26, 2013 at 4:28pm
yes i do. why would you say that


Why would you say "yes i do" without bothering to google to verify that you did?

http://www.asciitable.com/
http://stackoverflow.com/questions/2876275/what-are-the-ascii-values-of-up-down-left-right
Feb 27, 2013 at 2:36am
closed account (Dy7SLyTq)
how do you know i didn't. and as i said, its os dependent
Feb 27, 2013 at 4:31am
how do you know i didn't. and as i said, its os dependent


Whether or not ascii is used is O/S dependent. Ascii values are not. Which set of extended ascii is present may depend on the locale, but again, there are no representations there for arrow keys.

How do I know you didn't google it? Because you said "yes i do. why would you say that." I know you can read, so it seems pretty obvious you didn't and apparently didn't bother visiting the links supplied. I'm out.
Feb 27, 2013 at 11:10pm
closed account (Dy7SLyTq)
sorry i made a mistake. the tutorials i looked at for ncurses said they were ascii, but i searched under ascii keyboard constants and read an explination of how they were ascii sounding. so once again sorry, i was going off improper tutorials
Topic archived. No new replies allowed.