Keyboard input trought allegro
TeachMeHowToPro

Hello. I have a simple question:
How can you input text trought allegro?
If you didn't understand what i was meaning, imagine that in my game the user has to write a name for his game caracter. I need my game to detect when a key is pressed and if that key is between A - Z and a - z the program to add that caracter to a string varible. I was thinking of using the event system and somehow detect the keyboard keycode, but I don't know if the keycode is like the codes in the ASCII table. I want to use 100% allegro beause I can port it to other platforms.

Any help would be welcome!

Thomas Fjellstrom

The easiest way is to find a gui library you like (there's a few). Or you can use the ALLEGRO_EVENT_KEY_CHAR and the unichar field to get the unicode character (and use the allegro unicode string api).

Yodhe23

This is some 'orrible code that works from an old game of mine,

#SelectExpand
1const char * a5_grab_input(int ytextpos) 2{ 3int happen=0; 4char *edittext=malloc (sizeof (char) * 128); 5int caret = 0; 6int running=1; 7al_flush_event_queue(event_queue); 8 9al_draw_filled_rectangle(disp_data.width/5,ytextpos-20,disp_data.width/5*4,ytextpos+50,al_map_rgb(0,0,0)); 10al_draw_rectangle(disp_data.width/5,ytextpos-20,disp_data.width/5*4,ytextpos+50,al_map_rgb(150,150,150),3); 11al_flip_display(); 12 do 13 { 14 al_init_timeout(&timeout, 0.06); 15 bool get_event = al_wait_for_event_until(event_queue, &ev, &timeout); 16 17 if (get_event && ev.type==ALLEGRO_EVENT_KEY_CHAR){ 18 int newkey = ev.keyboard.unichar; 19 char ASCII = newkey & 0xff; 20 char scancode = ev.keyboard.keycode; 21 22 ///* a character key was pressed; add it to the string */ 23 if(scancode == ALLEGRO_KEY_ENTER) running=0; 24 else 25 if (scancode==ALLEGRO_KEY_SPACE) ; 26 else 27 if(scancode == ALLEGRO_KEY_BACKSPACE ){ 28 if (caret > 0) caret--; 29 happen=1; 30 edittext[caret] = '\0'; 31 } 32 else 33 if(ASCII >= 32 && ASCII <= 126){ 34 if (ASCII>=97 AND ASCII <=122 AND (caret==0 OR edittext[caret-1]==' ')) ASCII-=32; 35 if(caret < 20 - 1){ 36 edittext[caret] = ASCII; 37 caret++; 38 edittext[caret] = '\0'; 39 happen=1; 40 } 41 } 42 } 43 44 if (happen==1){ 45 al_draw_filled_rectangle(disp_data.width/5,ytextpos-20,disp_data.width/5*4,ytextpos+50,al_map_rgb(0,0,0)); 46 al_draw_rectangle(disp_data.width/5,ytextpos-20,disp_data.width/5*4,ytextpos+50,al_map_rgb(150,150,150),3); 47 al_draw_text(font,al_map_rgb(250,250,250),disp_data.width/2, ytextpos,ALLEGRO_ALIGN_CENTER,edittext); 48 al_flip_display(); 49 } 50 happen=0; 51 } 52 while(running==1); 53 54 55 //If no name - 0 characters inputed then return random name 56 if (caret==0){ 57 switch (rand()%3){ 58 case 0: 59 strcpy (edittext, "Vasya Pupkin"); 60 break; 61 case 1: 62 strcpy (edittext, "Joe Bloggs"); 63 break; 64 default: 65 strcpy (edittext, "Luther Blissett"); 66 break; 67 } 68 } 69 70return edittext; 71}

StevenVI

That really is 'orrible! :o Never share that snippet with anyone ever again! :-X

Yodhe23

'Orrible but it works, I guess it's the hacker in me.

TeachMeHowToPro

Can anyone recommend a GUI library? I need it to work on Android and IOS also

jmasterx

My GUI API Agui is used in the popular game Factorio: http://www.factorio.com/ and I have been using it for years.

I am currently porting my game to ios and in doing so I am adding some touch compatibility. I have obj-c classes to do keyboard support in ios too.

There's also:
GWEN https://github.com/garrynewman/GWEN
TGUI2 http://nooskewl.ca/more/open-source/

Thread #615157. Printed from Allegro.cc