I successfully use NSFileHandle to read keyboard input:
NSFileHandle * keyboard = [NSFileHandle fileHandleWithStandardInput];
NSData *inputData = [keyboard availableData];
NSString * input =[[NSString alloc] initWithData:inputData encoding:NSUTF8StringEncoding];
As the user types, only the first character of input is echoed on the screen. For example, if I type hello
, only h
is echoed on the screen. However the full input is read into the string.
The proper echo behavior works in Xcode 4.2. I'm currently using Xcode 4.5.
UPDATE
Giving up on NSFileHandle
for now, I tried to use scanf
. However there's the same echoing issue. scanf
code:
char word[4];
scanf("%s",word);
NSString * input = [[NSString alloc] initWithBytes:word length:4 encoding:NSUTF8StringEncoding];
This is a bug in Xcode 4.5. When the console is refreshed all echoed input is revealed.