Click to See Complete Forum and Search --> : Curses Letter / Number Codes


nko
08-25-2004, 03:45 PM
I'm trying to make a simple program, using curses, that detects if you've hit a button (in this particular case, number keys 1, 2, and 3). I gather that, for example, the integer 51 represents the code for when a user hits the "3" key on the keyboard, but this isn't what I'm looking for. I've found scores of lists telling me what the codes are for all the Fn keys, arrow keys, num pad keys, escape, tab, etc., but not for letters and numbers! How do I check to see if getch() is returning a letter or a number, outside of using raw integers?

madcompnerd
08-25-2004, 04:29 PM
The letters are integers, as getch() returns. They should follow whatever your system uses, probably utf-8.
You can always swap a char to an int easily, like so:
int x = int('3');

If you are curious I think I remember them right:

'0' == 48
'A' == 65
'z' == 128

So if you wanna find out if they just typed a letter:
int x = getch();
if ((x >= int('A') && x <= int('Z')) || (x >= int('a') && x <= int('z')))
//do your thing
;


It's that easy.

nko
08-25-2004, 04:34 PM
First, sorry, I forgot to mention that I can barely read C, and that I'm using Python :-)

Second, I know I can get their integer values that way, but I was hoping for predefined variables (like curses.KEY_F1 for an F1 keystroke). They exist and are listed for every key on the keyboard (and some I've never seen) with the exception of letters and numbers. It's certainly not KEY_A, KEY_B, KEY_C or anything like that.

mwinterberg
08-26-2004, 01:33 AM
There are probably no symbolic constants (i.e. KEY_A) because (at least in C), you can easily compare the return value of getch to a character constant, i.e. '3' if you wanted to see if the user pressed the 3 key (at least, the one in the non-keypad section of keys).

Have you tried something like (in pseudocode-english 'cause my introduction to Python was just now, and I'd rather not give you crappy Python code :) ):
Get the return value from getch
Create a string from that return value by calling chr with the return value as a parameter
Comparing that string with "3" (or whatever other key you care about), however that's done in Python.
?

Since it's a string now, you can just call the "is*" functions on it (isdigit, isalpha, isalnum) to determine what the specific "type" of key it is.

Of course, all of that seems rather extensive for something so easy. Did comparing the return value directly with a string literal not work?

nko
08-26-2004, 11:22 AM
You're absolutely right! I'd been converting the returned value to a str, but using chr() instead of str() returns the proper value.

Absolutely perfect. Thank you!!!

Moral of the story, for all us Python-using (and possibly C-using) Curses developers (or would-be developers, such as myself):

There are no contstants representing letters and numbers from keyboard input. None. Just convert the returned value from getch() to a character like so:

choice = sdtscr.getch()
stdscr.addstr(y,x, chr(choice))

Much thanks to madcompnerd and mwinterberg, who have similar sounding names!