Solved

getch for Unix-C

Posted on 1999-01-21
23
4,221 Views
Last Modified: 2008-02-20
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.
0
Comment
Question by:theta
  • 7
  • 6
  • 3
  • +5
23 Comments
 

Expert Comment

by:tato
ID: 1258262
getch() is a define not a function,
#define getch() getc(stdin)
try with a function getc(FILE *fp).

xcuse my english
0
 
LVL 3

Expert Comment

by:marcjb
ID: 1258263
tato's answer will unfortunately not work.  getc requires you to press ENTER when rading from stdin
0
 

Author Comment

by:theta
ID: 1258264
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
 
LVL 10

Expert Comment

by:viktornet
ID: 1258265
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
 
LVL 3

Expert Comment

by:marcjb
ID: 1258266
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
 
LVL 10

Expert Comment

by:viktornet
ID: 1258267
are you allowed to use Intel assembler in Unix? If so, i'll write a kbhit() function for you to use....
0
 
LVL 84

Expert Comment

by:ozo
ID: 1258268
Only if that Unix is running on an Intel machine.
0
 

Expert Comment

by:tato
ID: 1258269
try to include conio.h
0
 
LVL 10

Expert Comment

by:viktornet
ID: 1258270
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
 

Author Comment

by:theta
ID: 1258271
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
 
LVL 10

Expert Comment

by:viktornet
ID: 1258272
I could send conio.h to you if you want me to... it will work then I think...
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:theta
ID: 1258273
Ok, send it, and I will let you know if it works.

Thanks.

0
 
LVL 10

Expert Comment

by:viktornet
ID: 1258274
well?? e-mail address?
0
 

Author Comment

by:theta
ID: 1258275
amirthe@hotmail.com

0
 
LVL 10

Expert Comment

by:viktornet
ID: 1258276
I did....tell me if I can answer the question (and if it works at all)
0
 
LVL 2

Expert Comment

by:bertvermeerbergen
ID: 1258277
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
 

Author Comment

by:theta
ID: 1258278
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
 
LVL 10

Expert Comment

by:viktornet
ID: 1258279
ok, no problem...
0
 
LVL 3

Expert Comment

by:marcjb
ID: 1258280
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
 
LVL 3

Expert Comment

by:_Scotch_
ID: 1258281
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
 
LVL 4

Accepted Solution

by:
jos010697 earned 50 total points
ID: 1258282
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
 
LVL 4

Expert Comment

by:jos010697
ID: 1258283
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
 

Author Comment

by:theta
ID: 1258284
Haaaaa, what a relief.

Thankyou very much Jos.
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now