getch for Unix-C

What is equivalent to getch in Unix C. I need a function to get keystroke and don't want to wait for the <ENTER> key. I tried getchar(), it waits for the <ENTER> key.
OS           : AIX 4.2.1
compiler     : cc to compile the program.
Include Files: stdio.h and stdlib.h.

If I use getch() the program won't compile.
thetaAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
jos010697Connect With a Mentor Commented:
Here's a quick, dirty and lazy hack. If you don't like it, you have to dig up your man
pages for ioctl() and its compadres. Here's the hack:

   #include <stdio.h>

   int main() {

   int c;

   system("stty raw");

   while ((c= getchar()) != 'S')
           printf("char= %d\r\n", c);

   system("stty sane");

   return 0;

   }

It simply sets your terminal to raw mode (where 'stty' does all the dirty work for you).
Note the \r character in the printf call -- because your terminal is in raw mode, it
doesn't to any newline translation (it doesn't do any buffering either, but that's
just what you wanted). The last system() call sets your terminal back to a sane
mode again ...

kind regards,

Jos aka jos@and.nl
0
 
tatoCommented:
getch() is a define not a function,
#define getch() getc(stdin)
try with a function getc(FILE *fp).

xcuse my english
0
 
marcjbCommented:
tato's answer will unfortunately not work.  getc requires you to press ENTER when rading from stdin
0
Turn Raw Data into a Real Career

There’s a growing demand for qualified analysts who can make sense of Big Data. With an MS in Data Analytics, you can become the data mining, management, mapping, and munging expert that today’s leading corporations desperately need.

 
thetaAuthor Commented:
tato's suggestion doesn't answer my question.

Here is what I want to do:

I want to read the keystroke made by user, and then check if that key is an Escape key, UpArrow key, DownArrow key, RightArrow key, LeftArrow key, PageUp key or PageDown key,
based on the keystroke I have to change the screen. I can't use getc() or getchar because they require and ENTER key to be followed after a keystroke.

0
 
viktornetCommented:
for (;;) {
  while (!kbhit());
  ch = toupper(getch());
  if (ch == 'Q' || ch == 27) //Q or Esc
    break;
  switch(ch) {
    //check for arrow keys and change the screen...
  }
}

This will not wait until the enter key is pressed.... I know that i've used getch(), but please don't reject my answer until you have not tried it... First try it and them tell me how it works....

Hope this helps...

-Viktor
--Ivanov
0
 
marcjbCommented:
kbhit is not a function that works with UNIX.  It is a DOS function.

theta, try looking for a package called CURSES.  It is a UNIX library of functions that mimic the conio.h library.
0
 
viktornetCommented:
are you allowed to use Intel assembler in Unix? If so, i'll write a kbhit() function for you to use....
0
 
ozoCommented:
Only if that Unix is running on an Intel machine.
0
 
tatoCommented:
try to include conio.h
0
 
viktornetCommented:
well is it? (i mean is it running on an Inte machine theta?)

tato, I just checked out my help file, and kbhit() can only be used in DOS...
0
 
thetaAuthor Commented:
As I mentioned earlier getch() is not being compiled under unix, anyway I tried viktornet's proposed answer and got following error.

ld: 0711-317 ERROR: Undefined symbol: .getch

The Unix box I am working on is an upgrade of RS6000 called SPF. I don't belive it has an intel.

I will try marcjb's suggestion of looking into curses.h and see if I can find anything. (Can't include conio.h because compiler couldn't find it, I think it is not part of unix c library).

Thanks to all of you for your effort.

0
 
viktornetCommented:
I could send conio.h to you if you want me to... it will work then I think...
0
 
thetaAuthor Commented:
Ok, send it, and I will let you know if it works.

Thanks.

0
 
viktornetCommented:
well?? e-mail address?
0
 
thetaAuthor Commented:
amirthe@hotmail.com

0
 
viktornetCommented:
I did....tell me if I can answer the question (and if it works at all)
0
 
bertvermeerbergenCommented:
I remember some option you could set with stty to put a terminal in 'raw' mode (instead of 'cooked').  I believe it had something to do with buffering the input until you press <return>.  This was a long time ago, and I am not even sure this will get the result you want.  But, everyone seems to give his opinion on this question, so I am joining the party :-)
0
 
thetaAuthor Commented:
viktornet,

It didn't work. I got the following errors:

line 13.10: 1506-296 (S) #include file <_defs.h> not found.
line 61.14: 1506-166 (S) Definition of function _Cdecl requires parentheses.
line 61.21: 1506-276 (S) Syntax error: possible missing '{'?

If you look at the third line in conio.h, you will see:
   
  Direct  MSDOS console input/ouput.

So, conio.h is not for Unix anyway.

I will try Curses.h and see if it works.

Thanks again.



0
 
viktornetCommented:
ok, no problem...
0
 
marcjbCommented:
A couple of notes about curses I should have added previously

You may need to add -lcurses to the command line complilation.
ex:  cc test.c -lcurses

Also, you may have to initialize your screen
initscr()

There should be a few good online documents about the proper way to use curses.  Hope this helps.
0
 
_Scotch_Commented:
marcjb is on the right track.  You must

#include <curses.h>

you must also link against the curses library with -lcurses
you must also initialize your screen with initscr()

check the man pages:
man curses
man initscr
man getch
0
 
jos010697Commented:
Darn, I hate these tiny little windows; make that system("stty raw") call
system("stty raw -echo"), otherwise the character just typed is still echoed ...

kind regards,

Jos aka jos@and.nl
0
 
thetaAuthor Commented:
Haaaaa, what a relief.

Thankyou very much Jos.
0
All Courses

From novice to tech pro — start learning today.