Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Text input in Allegro 5

This thread is locked; no one can reply to it. rss feed Print
Text input in Allegro 5
Andrew Dvornik
Member #13,806
December 2011

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
Member #2,727
September 2002
avatar

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
Member #11,410
October 2009

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
Member #13,806
December 2011

"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
Member #2,727
September 2002
avatar

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
Member #13,806
December 2011

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
Member #907
January 2001

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

torhu
Member #2,727
September 2002
avatar

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.

Go to: