Thread: Get keystroke?

  1. #1
    Third Eye Babkockdood's Avatar
    Join Date
    Apr 2010
    Posts
    352

    Get keystroke?

    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.

  2. #2
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    Is this what you are thinking of?
    Last edited by kermit; 06-29-2010 at 03:52 PM.

  3. #3
    Third Eye Babkockdood's Avatar
    Join Date
    Apr 2010
    Posts
    352
    The Qs are the same, but I'm running Unix. And I'd like this code to be portable.

  4. #4
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    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.

  5. #5
    Registered User
    Join Date
    Jun 2010
    Posts
    182
    Quote Originally Posted by kermit View Post
    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.
    Maybe a solution could be using a port of a C compiler that gives you a virtual portability
    across many OS platforms.

  6. #6
    Password:
    Join Date
    Dec 2009
    Location
    NC
    Posts
    587
    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.

  7. #7
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    Curses has also been ported to windows.

  8. #8
    Password:
    Join Date
    Dec 2009
    Location
    NC
    Posts
    587
    I didn't know that. Well there you go. Use curses. It's the only apparent portable solution.

  9. #9
    Registered User NeonBlack's Avatar
    Join Date
    Nov 2007
    Posts
    431
    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

  10. #10
    Third Eye Babkockdood's Avatar
    Join Date
    Apr 2010
    Posts
    352
    Alright, I have the curses library pre-installed. How would I go about doing this with curses?

  11. #11
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    Dude. . . Google is your friend.

  12. #12
    Third Eye Babkockdood's Avatar
    Join Date
    Apr 2010
    Posts
    352
    Quote Originally Posted by Kennedy View Post
    Dude. . . Google is your friend.
    :P__

  13. #13
    Third Eye Babkockdood's Avatar
    Join Date
    Apr 2010
    Posts
    352
    Alright. When I try to compile the following...

    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;
    }
    ...the compiler gives me grief.

    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
    Help?

  14. #14
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Proplem with keyStroke (ALT-x)
    By meokhung1980 in forum C++ Programming
    Replies: 0
    Last Post: 05-11-2008, 04:57 PM
  2. need help passing keystroke to program
    By snap-tech in forum C++ Programming
    Replies: 2
    Last Post: 02-01-2008, 04:41 AM
  3. Hook and Keystroke Question
    By ldb88 in forum Windows Programming
    Replies: 8
    Last Post: 12-04-2007, 12:26 AM
  4. Problem with keystroke stacking
    By Flakster in forum C++ Programming
    Replies: 13
    Last Post: 10-20-2005, 02:18 PM
  5. keystroke processing
    By lambs4 in forum Windows Programming
    Replies: 3
    Last Post: 09-07-2003, 04:22 PM