Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
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, 92 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

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
thread Thread Starter Forum Replies Last Post
Jack Back - Wood Back Replacement for iPhone 4: Review karleaglemac iPhone Accessories 49 Feb 9, 2012 05:23 PM
Mas Design - Ti Rust: Canvas (Titanium Back Replacement for iPhone 4): Review karleaglemac iPhone Accessories 21 Oct 11, 2011 08:04 AM
PSU replacement for PowerMac G5 1.8 DP 2004 >>> PM G5 2005? netsurfz PowerPC Macs 3 Sep 7, 2011 09:26 AM
Handbrake help needed for windows DollarsAndGirls iPod touch 1 Jan 15, 2009 10:45 AM
iTunes replacement for Windows 98SE? Yvan256 Mac Applications and Mac App Store 30 Dec 16, 2006 03:56 PM


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

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

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