Hey there. Is there a better way to get the key a user typed and save it to a string than to use al_keycode_to_name? As a test, I want to create a high score table that saves the user's name. This is what I've got so far:
The above is self-explanatory, but it takes the user's keystrokes and saves it to a string named "name", then displays it as the user types. The problem with the above, however, is that it saves shift keys and caps lock (and others) as "SHIFT_*" and "CAPS_LOCK", which isn't what I want.
What's a better way to just get alphanumeric input and to ignore all the rest without writing a bunch of if statements to continue for each?
Thanks in advance.
]]>Monitor the event queue for ALLEGRO_EVENT_KEY_CHAR events and store their keycode in a string.
http://liballeg.org/a5docs/trunk/events.html#allegro_event_key_char
http://liballeg.org/a5docs/trunk/keyboard.html#key-codes
do { ALLEGRO_EVENT ev; al_wait_for_event(queue , &ev); if (ev.type == ALLEGRO_EVENT_KEY_CHAR) { if (ev.keyboard.keycode >= ALLEGRO_KEY_A && ev.keyboard.keycode <= ALLEGRO_KEY_Z) { name += 'a' += (ev.keyboard.keycode - ALLEGRO_KEY_A); } if (ev.keyboard.keycode >= ALLEGRO_KEY_0 && ev.keyboard.keycode <= ALLEGRO_KEY_9) { name += '0' + (ev.keyboard.keycode - ALLEGRO_KEY_0) } } } while (!al_is_event_queue_empty(queue));
]]>
That worked like a charm, Edgar. Thanks a bunch.
Could you explain the significance of 'a' and '0'? I noticed that changing 'a' to 'A' results in capturing the upper-case version of letters. Does this have something to do with encoding the characters?
]]>A character literal is just a number
]]>A traditional 'char' literal is just an ASCII number from -128 to 127. I'm taking advantage of the fact that the distance between ev.keyboard.keycode and ALLEGRO_KEY_A and ALLEGRO_KEY_0 is the same as the distance between 'a' and the character pressed and '0' and the character pressed.
ASCII chart :
Click Me!
Wow, that's slick! Thanks a bunch!
]]>