1

Possible Duplicate:
How to avoid press enter with any getchar()

I need to get keyboard pressed key in console Objective-C app without pressing 'Enter' button

This code reads pressed key code only after I press 'Enter'

int key;
key=getchar();
NSLog(@"%i", key);

I need something like this but without pressing 'Enter'. How can I do this?

Community
  • 1
  • 1
drysdgsdg
  • 61
  • 2
  • 10

1 Answers1

2

By default, consoles are in 'canonical' mode which does a load of processing on the input and doesn't pass it to a process's open file descriptor until a new line is encountered. You want to put stdin into 'noncanonical' mode, to get the bytes as they arrive:

#include <termios.h>

struct termios terminal_info;
tcgetattr(STDIN_FILENO, &terminal_info);
terminal_info.c_lflag &= ~ICANON;
tcsetattr(STDIN_FILENO, TCSANOW, terminal_info);

(you should check for errors returned by tcgetattr() and tcsetattr(), too.)