Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old May 28, 2009, 07:35 PM   #1
maclover201
macrumors member
 
Join Date: Jul 2007
Very helpful replacement for Windows getch()

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:
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.
__________________
15.4" MBP, 2.2 GHz Core 2 Duo, 2 GB RAM, 300 GB HDD; iPod touch 8 GB; Canon HG10 camcorder; Nikon D80 DSLR

Last edited by maclover201; Jul 17, 2009 at 10:43 PM. Reason: Updated for getch.h r2
maclover201 is offline   0 Reply With Quote
Old Jul 17, 2009, 10:30 PM   #2
maclover201
Thread Starter
macrumors member
 
Join Date: Jul 2007
Update! Please use this version of getch.h as it is much less evil. The last version was really inefficient and would get terminal attributes every time it was called or would loop. The new one persistently stores this information in a class structure and only sets it once for the program's lifetime, so it's much better.

Here's how to upgrade your program to work with this version of getch.h:

-Change getch() to Terminal::Getch()
-Change getch(string) to Terminal::Getch(string)
-Change getch(const char *) to Terminal::Getch(const char *)
-Change getseq(string) to Terminal::GetSeq(string)
-Change getseq(const char *) to Terminal::GetSeq(const char *)

Pretty easy. It's really well-commented this time, so download, learn, and use!

-Morgan [maclover201]

UPDATE: Please use getch.h r3 instead.
__________________
15.4" MBP, 2.2 GHz Core 2 Duo, 2 GB RAM, 300 GB HDD; iPod touch 8 GB; Canon HG10 camcorder; Nikon D80 DSLR

Last edited by maclover201; Jul 17, 2009 at 10:44 PM.
maclover201 is offline   0 Reply With Quote
Old Jul 17, 2009, 10:43 PM   #3
maclover201
Thread Starter
macrumors member
 
Join Date: Jul 2007
Just noticed a glaring error that would affect some people compiling this code. Works the same as revision 2 though, so use it. Please. I beg you.

-Morgan [maclover201]
Attached Files
File Type: zip getch_r3.h.zip (1.9 KB, 99 views)
__________________
15.4" MBP, 2.2 GHz Core 2 Duo, 2 GB RAM, 300 GB HDD; iPod touch 8 GB; Canon HG10 camcorder; Nikon D80 DSLR
maclover201 is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Windows Live Writer Replacement Guardian Enzo Web Design and Development 8 Jan 8, 2014 12:08 AM
Recommend a Quicken for a Windows replacement Resqu2 Mac Applications and Mac App Store 2 Nov 29, 2013 10:38 AM
New to Mac, just looking for some helpful input MatthewMark MacBook Pro 1 May 23, 2013 05:22 PM
Any helpful/useful tricks for iPad? MauerFan iPad Tips, Help and Troubleshooting 3 Apr 9, 2013 08:34 AM

Forum Jump

All times are GMT -5. The time now is 09:07 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC