I found some of this code on another forum but rewrote it so it's more versatile. A lot of people have been asking for a Unix/Mac equivalent, so here it is. Enjoy. Feel free to use it for anything you need.
getch.h:
main.cpp:
-Morgan [maclover201]
UPDATE: Please use getch.h r3 instead.
getch.h:
Code:
(removed due to new version of getch.h)
main.cpp:
Code:
#include <iostream>
#include "getch.h"
using namespace std;
int main (int argc, char * const argv[]) {
/*
Example, updated for getch.h r2
Put only the characters you want getch to
wait for in the function parameter.
It will ignore anything other than those
characters and return which one was pressed.
*/
cout << "Press A for apples, B for bananas, or C for carrots. ";
char c = Terminal::Getch( "AaBbCc" );
cout << endl;
if( c == 'A' || c == 'a' )
cout << "Apples" << endl;
if( c == 'B' || c == 'b' )
cout << "Bananas" << endl;
if( c == 'C' || c == 'c' )
cout << "Oranges. HA!" << endl;
// Regular function example.
// Terminal::Getch will wait for anything and return what character was pressed.
cout << "Press any key. ";
c = Terminal::Getch();
cout << endl << "You pressed " << c << '.' << endl;
// Sequential function example.
// Terminal::GetSeq will wait for a sequence of characters.
// If the wrong character was pressed, the user will
// (unknowingly) have to enter the sequence over again.
cout << "Find the sequence!" << endl;
Terminal::GetSeq( "MacRumors" );
cout << endl << "Well done! Also, you just lost The Game." << endl;
return 0;
}
-Morgan [maclover201]
UPDATE: Please use getch.h r3 instead.