Text input in Allegro 5
Andrew Dvornik

I would like to know how to input text using allegro. I am able to print it out, but can't figure out how to take it out from keyboard.

Please don't recommend to write 60 or more "cases" for "ALLEGRO_KEY_DOWN_A .. Z" and Shift/CapsLock, etc... And also GUI libraries.

torhu

You can use ALLEGRO_EVENT_KEY_CHAR, the unichar field contains the character that the user typed. If he holds down Shift and presses the A key, event.keyboard.unichar will be 'A', etc.

jmasterx

When you get the aforementioned event, the text itself is in UTF32 format. If you know you're only getting ascii, then you can just do
string += (char)event->keybard.unichar .

http://www.liballeg.org/a5docs/refman/events.html#allegro_event_key_char

Andrew Dvornik

"torhu:
You can use ALLEGRO_EVENT_KEY_CHAR, the unichar field contains the character that the user typed. If he holds down Shift and presses the A key, event.keyboard.unichar will be 'A', etc."

Could you please give an example of code? And what data type of variable should I use to store input characters?

torhu

jmasterx's trick will work for ASCII and Latin-1 strings, but you could also do it roughly like this:

First:
ALLEGRO_USTR *input = al_ustr_new("");

Then, in your event loop:

case ALLEGRO_EVENT_KEY_CHAR: {
    int unichar = event.keyboard.unichar;
    if (unichar >= 32)
        al_ustr_append_chr(input, unichar);
    break;
}

And when you're done with the string:
al_ustr_free(input);

If you want just one character at a time instead of the whole string, you can use al_utf8_encode.

Andrew Dvornik

to torhu

Thanks a lot for your advice!

I'll try it later. And one more question: if I need to delete char from word how should I write code?

Audric

At first sight in the manual, I'd say: al_ustr_truncate( ... )
See the rest of manual to get current string length etc.

torhu

Instead of using the manual on this site, you should use this one, which makes it a a bit easier to find what you're looking for by just browsing.

Thread #609117. Printed from Allegro.cc