We help IT Professionals succeed at work.

Write me getche()

Spenny
Spenny asked
on
1,159 Views
Last Modified: 2013-12-26
I need someone to write me the function "getche()" which can be used on the Mac (i.e. input a key without having to press Return). I can't find console.h on CW. I can't find conio.h. I'm trying to learn C using Teach Yourself C by Herb Schildt and this function is used a lot.

So, please either write me "getche()" or tell me where to find/download the library which contains conio.h which contains getche().

Confused? That's why I'm offering a wad of points for this.
Comment
Watch Question

Commented:
Are you using SIOUX within CodeWarrior?  SIOUX is the standard code for porting console-based apps.

BTW, console.h and conio.h are DOS-specific.  Look for the functions provided in stdio.h instead, which *all* ANSI C implementations must implement.

Author

Commented:
I am using SIOUX. The only thing is I don't know which function is a decent alternative, or how I can find a list of all the functions that SIOUX offers.

You're right. These functions are DOS-based, as are the books. But the books have loads of examples in them and exercises, something which the Mac books do not. And you know what they say about practice makes perfect...
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
Twall, this still hasn't worked for me. I can't seem to find the MSL libraries. I have included all the Runtime libraries, but to no avail. I have MPW libraries, and all sorts, but no MSL libraries with RuntimePPC.Lib. So getch() didn't work.

Believe me, no one more than me wants to solve this. Please keep helping.

Commented:
What version of CW are you using? If it's one of the Lite versions it may not include MSL.

Ordinarily, MSL Runtime is found at
Metrowerks Codewarrior:MacOS Support:Libraries:Runtime
and console.h is at
Metrowerks Codewarrior:Metrowerks Standard Library:MSL C:MSL Mac:Public Includes


At any rate, here are some implementations you can use:
/*
*
*    int kbhit()
*
*    returns true if any keyboard key is pressed without retrieving the key
*    used for stopping a loop by pressing any key
*/
int kbhit(void)
{
      EventRecord event;  
      return EventAvail(keyDownMask,&event);
}

/*
*
*    int getch()
*
*    returns the keyboard character pressed when an ascii key is pressed  
*    used for console style menu selections for immediate actions.
*/
int getch(void)
{
   int c;
   EventRecord event;
   fflush(stdout);
         /* Allow SIOUX response for the mouse, drag, zoom, or resize. */
   while(!GetNextEvent(keyDownMask,&event)) {
            if(GetNextEvent(mDownMask|mUpMask,&event))SIOUXHandleOneEvent(&event);
   }
   c=event.message&charCodeMask;
   if(c=='.' && (event.modifiers&cmdKey))exit(1);
 
   return c;
}

Author

Commented:
Seeing as these functions will be used continuously by me, I decided to put them in a header file (called "headers.h") and included in each programme. Here's the syntax:

"headers.h"

// int khbit()
// returns true if any keyboard key is pressed without retrieving the key.
// Used for stopping a loop by pressing a key.

int khbit( void );
int getch( void );

int khbit( void )
{
      EventRecord event;
      return EventAvail(keyDownMask, &event);
}

// int getch()
// returns the keyboard character pressed when an ascii key is pressed.
// used for console style menu selections for immediate actions.

int getch( void )
{
      int c;
      EventRecord event;
      fflush( stdout );
      // Allow SIOUX response for the mouse, drag, zoom or resize
      while( !GetNextEvent( keyDownMask, &event) )
      {
            if( GetNextEvent( mDownMask|mUpMask, &event)) SIOUXHandleOneEvent (&event );
      }
      
      c = event.message&charCodeMask;
      if( c=='.' && (event.modifiers&cmdKey) ) exit(1);
      
      return c;
}

Then my main programme, say:

"main.c"

#include <stdio.h>
#include "headers.h"

int main( void )
{
      int i;
      char ch;
      
      for( i=1; i<10000; i++)
      {
            if( !(i%6) )
            {
                  printf( "%d. More?", i );
                  ch = getch();
                  if( ch=='n' ) break;
                  printf( "\n" );
            }
      }
      
      return 0;
}


But, on trying to compile "headers.h" it gives me the following messages:

Error   : function has no prototype
headers.h line 26   if( GetNextEvent( mDownMask|mUpMask, &event)) SIOUXHandleOneEvent (&event );

Error   : function has no prototype
headers.h line 30   if( c=='.' && (event.modifiers&cmdKey) ) exit(1);

Have I typed these correctly or is there something I'm missing?

Author

Commented:
Oops, the tabs didn't come out.

Commented:
1) Don't put code in a header file.  That's bad practice.  Make a header which contains prototypes for the functions, then put the functions in a separate source file and add it to your project, or add the functions to an existing source file.  

2) Error: function has no prototype
If this is complaining about one of *your* functions, it means you need to declare the function before using it, i.e. put a prototype for it in a header or somewhere in the code stream *before* it is used.

If this is complaining about some other function, then it means you need to #include the header which contains a prototype for that fucntion.


Author

Commented:
Twall, the errors given were in the following functions:

exit(1)

SIOUXHandleOneEvent( &event ).

Even when I included these functions in the main programme.

BTW, I'm using CW Gold 8.

Commented:
The SIOUXHandleOneEvent declaration is in SIOUX.h, while the definition is in SIOUX.c.  (declaration is a prototype, while definition is the actual code)

exit() is declared in stdlib.h

Unfortunately, CW is not very good at looking things up like this.  It's a lot easier to search headers and lookup documentation on a UNIX system.

Author

Commented:
It worked! Thanks for your time and help, Twall.

Look out for more things in the future, I'm sure. For now, that's really solved a huge problem. The programmes now WORK! (With getchar() they did strange things).

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.