converting text input to a c char
How would I convert.....
......to a c character or an array of c chars?
Also, where would I find constants for key codes such as return, escape, etc... ?
Code:
NSString *characters = [theEvent characters];
if ([characters length])
{
unichar character = [characters characterAtIndex:0];
}......to a c character or an array of c chars?
Also, where would I find constants for key codes such as return, escape, etc... ?
Text input is a huge minefield. Perhaps you should start by explaining what kind of thing you're making here (FPS? RTS? Typing Tutor? High-score name entry field?).
If the character you've received is ASCII, (char)character is a C char. That may or may not be useful to you.
If the character you've received is ASCII, (char)character is a C char. That may or may not be useful to you.
OneSadCookie Wrote:Text input is a huge minefield. Perhaps you should start by explaining what kind of thing you're making here (FPS? RTS? Typing Tutor? High-score name entry field?).
If the character you've received is ASCII, (char)character is a C char. That may or may not be useful to you.
Sure. I'm working on a text parser for an in-game console, similar in feel to the unix terminal. Basically you type in a line of text, and when you hit enter, the it gets parsed and executed by the console.
So here's roughly how I figure my console will read in text,
Code:
NSString *characters = [theEvent characters];
if ([characters length])
{
unichar character = [characters characterAtIndex:0];
if (is_console_open())
{
char *str = [ characters UTF8String];
if (*str == ENTER_KEYCONSTNANT)// this works if constant
// is a single-digit int
console_execute_input();
else
console_type_char( str);
return;
}
switch (character)
{
// game stuff here
}My console keeps a c char array representing a single line of input and appends the str character to that line whenever a key is down and the console is open.
I've searched over a lot of apple docs and can't find anything on the delete or return character key constants other than the actual integer values
Return and backspace are ASCII ('\n' and '\b' respectively). You can find a list of the nonstandard ones in NSEvent.h ( /System/Library/Frameworks/AppKit.framework/Headers/NSEvent.h ).
It sounds like you want characters, and you want to ignore nonascii characters, so you'll want to do something like:
It sounds like you want characters, and you want to ignore nonascii characters, so you'll want to do something like:
Code:
NSString *s = [event characters];
size_t i, n = [s length];
for (i = 0; i < n; ++i)
{
unichar u = [s characterAtIndex:i];
if (u > 127)
{
// ignore or beep for nonascii characters
}
else if (isprint(u))
{
// append to console (char)u
}
else if (u == '\n')
{
// execute a command
}
else if (u == '\b')
{
// backspace
}
else if (u == '\t')
{
// command completion
}
else
{
// the character's pretty wacky, maybe beep
}
}
Thanks, I managed to get input worked out, but I've run into another minor issue.
Here's what my parser would look like trying to parse the string "MOVE PLAYER", residing in the input_line variable.
the problem is that if the first word read into"word" (which in the example is PLAYER) is longer than the second word(MOVE), the second word ends up being, in this case, "moveer" where the "er" are characters left over from "player" read in from the first call to read_word().
So, is there a way to restore word[] to it's totally null/empty state using only its pointer from within read_word()?
Here's what my parser would look like trying to parse the string "MOVE PLAYER", residing in the input_line variable.
Code:
char input_line[1000]; // a string of chars representing input
BOOL read_word( char* word, int which_word )
{
//parses line_input into individual words, if which_word == 1
// then the parser puts the first word in the input_line into word_str
word = nil;
int first_word = get_word_start( which_word );
int word_len = get_word_length( which_word );
// not a valid word
if (first_word == -1 || word_len == -1)
return FALSE;
strncpy( word, &line_input[first_word], word_len );
return TRUE;
}
void parse()
{
char word[255];
if (read_word(word, 1))
if ( strcmp(word, "PLAYER") == 0)
if (read_word( word, 2))
if ( strcmp(word, "MOVE") == 0)
//-- you get the idea
}the problem is that if the first word read into"word" (which in the example is PLAYER) is longer than the second word(MOVE), the second word ends up being, in this case, "moveer" where the "er" are characters left over from "player" read in from the first call to read_word().
So, is there a way to restore word[] to it's totally null/empty state using only its pointer from within read_word()?
Try:
Code:
...
strncpy( word, &line_input[first_word], word_len );
word[word_len] = '\0'; // add this line
return TRUE;
Got it working, thanks.
Possibly Related Threads...
| Thread: | Author | Replies: | Views: | Last Post | |
| (unsigned) char, bitsets when using - + / * | wyrmmage | 7 | 4,162 |
Jul 21, 2007 04:01 PM Last Post: wyrmmage |
|
| Char* to double, int, and back | wyrmmage | 18 | 6,107 |
Jun 24, 2007 11:09 AM Last Post: akb825 |
|
| Help with converting from void to int. | qoo111 | 5 | 2,729 |
Apr 18, 2007 05:41 AM Last Post: ThemsAllTook |
|
| Return char if input is char, else - int? | Achithyn | 2 | 2,602 |
Aug 6, 2006 03:43 AM Last Post: backslash |
|
| C/C++ Int To Char* | Nick | 4 | 10,195 |
Sep 12, 2005 10:02 AM Last Post: Zekaric |
|

