Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

anthonyme

macrumors newbie
Original poster
Nov 2, 2016
5
0
Hello,
I was programming on xcode and trying to enter a character without pressing de 'Enter' key, but the the command _getch() doesn't work. Is there another way of doing it?
thank you
 
i´m not sure, but you need to implement something with kbhit() instead of using getch()
 
I've tried to use it but xcode tells me it is undeclared. Do you know the library it comes with?
 
This is how I implemented getch for a ported MS/DOS program (!) that needed it. As a bonus, this also works for Linux.

#include <stdlib.h>

#include <sys/ioctl.h>

#include <termios.h>

int getch(void) {

char chbuf[1];

struct termios oldstate, newstate;

tcgetattr(0, &oldstate);

newstate = oldstate;

newstate.c_lflag &= ~ICANON;

newstate.c_lflag &= ~ECHO;

tcsetattr(0, TCSANOW, &newstate);

read(0, &chbuf, 1);

tcsetattr(0, TCSANOW, &oldstate);

}
 
  • Like
Reactions: hostins
Is there some reason not to use the standard C function: int getchar(void)?
It uses line buffering -- you only get keystrokes after an entire line has been typed in. getch returns a single character typed (and without echoing it).
 
There is the fact that it is NOT part of any standard!

If I remember correctly (and I may not) it was a Borland provide routine on DOS/Windows.

You have it backwards. getch() was by Borland. getchar() is in the Standard C Library and equivalent to getc(stdin).
 
  • Like
Reactions: Krevnik
This is how I implemented getch for a ported MS/DOS program (!) that needed it. As a bonus, this also works for Linux.

#include <stdlib.h>

#include <sys/ioctl.h>

#include <termios.h>

int getch(void) {

char chbuf[1];

struct termios oldstate, newstate;

tcgetattr(0, &oldstate);

newstate = oldstate;

newstate.c_lflag &= ~ICANON;

newstate.c_lflag &= ~ECHO;

tcsetattr(0, TCSANOW, &newstate);

read(0, &chbuf, 1);


tcsetattr(0, TCSANOW, &oldstate);

}
Thanks a lot! The only problem is that it tells me that the identifier "read" is undeclared so the program does not work... Would it be because of a library or something?
 
Thanks a lot! The only problem is that it tells me that the identifier "read" is undeclared so the program does not work... Would it be because of a library or something?

It's in the standard C library. libc. Do "man 2 read" on the command line to get the documentation.
 
I am working on c++ does it change something?
It shouldn't as long as the correct header file is specified. The man page says you need:
#include <sys/types.h>

#include <sys/uio.h>

#include <unistd.h>

so add those in and see if it fixes it.
 
Since this is a Mac programming forum, the right way to do this is to create an XCode project with a single window. Add an NSTextField to the window. Create a custom class to be the window.xib's file owner. Create a delegate method
Code:
- (void) controlTextDidChange:(NSNotification *) aNotification;
NSTextField delegate method and link to it. Every time a user types a character this method will be invoked. In the code for this method you can get the string from the text field using a call to some method like
Code:
NSString * myString = [TextFieldName stringValue];

This is very oversimplified but it's really the way to get user input for the Mac.

Yes, you can create a console app as well.
 
  • Like
Reactions: _circledancer
Since this is a Mac programming forum, the right way to do this is to create an XCode project with a single window. Add an NSTextField to the window. Create a custom class to be the window.xib's file owner. Create a delegate method
Code:
- (void) controlTextDidChange:(NSNotification *) aNotification;
NSTextField delegate method and link to it. Every time a user types a character this method will be invoked. In the code for this method you can get the string from the text field using a call to some method like
Code:
NSString * myString = [TextFieldName stringValue];

This is very oversimplified but it's really the way to get user input for the Mac.

Yes, you can create a console app as well.




HI HERE IS MY RESPONSe...
so this is my first macrumor post I think. I'm coding. Trying to figure out these header files and libraries. Can you elaborate on how to 'add an NSTextField to the window and create a custom class to be the window.xib's file owner" ... it sounds like you know what you're doing and all my peers and instructor use WINDOWS and no one can help me .
 
Create a single window application with XCode. Drag an NSTextField from the options in the right pane into the window.

You need to look at examples of how to do this in a programming book like Cocoa Programming for OS X: The Big Nerd Ranch Guide (5th Edition). Search for others at Amazon.com.

Or you can create a C command line tool.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.