Hey guys. Is there a way to get a single keystroke in my program? I'm running Unix, so I can't use getch (and if I were running DOS, I would decide against it for lack of portability). If it's easier in C++, I'm willing to adapt.
Hey guys. Is there a way to get a single keystroke in my program? I'm running Unix, so I can't use getch (and if I were running DOS, I would decide against it for lack of portability). If it's easier in C++, I'm willing to adapt.
Is this what you are thinking of?
Last edited by kermit; 06-29-2010 at 03:52 PM.
The Qs are the same, but I'm running Unix. And I'd like this code to be portable.
Well, as per the answer,
"Alas, there is no standard or portable way to do these things in C. Concepts such as screens and keyboards are not even mentioned in the Standard, which deals only with simple I/O ``streams'' of characters."
You will have to roll your own non-portable way.
curses is as portable as you'll get. It's the defacto lib, on Unix compatible OSes, for your needs. DOS/Win has something else that is just as unportable, but I can't think of what it's called.
Curses has also been ported to windows.
I didn't know that. Well there you go. Use curses. It's the only apparent portable solution.
Yes, it's called PDCurses. It has a few extensions that aren't portable to other curses platforms though, so be sure to read the documentation.
I copied it from the last program in which I passed a parameter, which would have been pre-1989 I guess. - esbo
Alright, I have the curses library pre-installed. How would I go about doing this with curses?
Dude. . . Google is your friend.
Alright. When I try to compile the following...
...the compiler gives me grief.Code:#include <stdio.h> #include <curses.h> int main() { int a; while ((a = getch()) != 'q'); { if (a == KEY_LEFT) printf("You pressed left.\n"); if (a == KEY_RIGHT) printf("You pressed right.\n"); if (a == KEY_UP) printf("You pressed up.\n"); if (a == KEY_DOWN) printf("You pressed down.\n"); } return 0; }
Help?Code:cc prog.c -o prog Undefined symbols: "_stdscr", referenced from: _stdscr$non_lazy_ptr in ccBn7zUp.o "_wgetch", referenced from: _main in ccBn7zUp.o ld: symbol(s) not found collect2: ld returned 1 exit status make: *** [prog] Error 1
Undefined symbols are often times libraries that are not Linked in at compile time (as you see from "ld: symbol(s) not found"). So, you'll have to include the compile time library. Looks like you are using some version of gcc, so you'll need to gcc -lncurses <rest of your stuff>. If you are using an IDE to compile, look for the libraries tab.