Mysterious text input issues
TylerD

Hello.

I'm trying to code some text input, but after a long while of attempts and reading, I don't know what's the problem in my code.

#SelectExpand
1... 2ALLEGRO_USTR_INFO *text_typed = al_ustr_new("A"); 3... 4 5while(1) 6 { 7 al_wait_for_event(queue, &event); 8 9 switch(event.type) 10 { 11 case ALLEGRO_EVENT_TIMER: 12 // game logic goes here. 13 if(key[ALLEGRO_KEY_ESCAPE]) 14 done = true; 15 16 for(int i = 0; i < ALLEGRO_KEY_MAX; i++) 17 key[i] &= KEY_SEEN; 18 19 switch(event.keyboard.keycode) 20 { 21 case ALLEGRO_KEY_ENTER : 22 // Enter string, process 23 break; 24 case ALLEGRO_KEY_BACKSPACE : 25 // Remove character before caret 26 break; 27 case ALLEGRO_KEY_DELETE : 28 // Remove character at caret 29 break; 30 default : 31 // Add character to our string 32 al_ustr_append_chr(text_typed,event.keyboard.unichar); 33 break; 34 } 35 36 redraw = true; 37 break; 38 39 case ALLEGRO_EVENT_MOUSE_AXES: 40 x = event.mouse.x; 41 y = event.mouse.y; 42 break; 43 44 case ALLEGRO_EVENT_KEY_DOWN: 45 key[event.keyboard.keycode] = KEY_SEEN | KEY_RELEASED; 46 break; 47 case ALLEGRO_EVENT_KEY_UP: 48 key[event.keyboard.keycode] &= KEY_RELEASED; 49 break; 50 51 case ALLEGRO_EVENT_DISPLAY_CLOSE: 52 done = true; 53 break; 54 }

In short, text_typed is getting no data (chars) appended to it. I know it's rendering properly because I can see the "A". Also, because I've once managed to accidentally append garbage, I know it is appending, so it's not a rendering issue. It is catching the key presses, I've tested with printfs to the console. The default for switch(key[ALLEGRO_EVENT_KEY_CHAR]) also does trigger, using the same trick.
Now what?
PS: As implied in the code, it'll have a caret and backspace/delete and enter support, but it's pointless to code now given I can't input any data.
PPS: Yes, part of the code is from https://www.allegro.cc/forums/thread/617234 .

Mark Oates
switch(event.type)
  case ALLEGRO_EVENT_TIMER:
    switch(event.keyboard.keycode)

^^ I think you meant the .keycode switch to be under ALLEGRO_EVENT_KEY_CHAR?

TylerD

I've done so many changes at the code by now... Yes, that's not quite "proper", given the esc key is outside, but it's in that way for rendering, via the timer. The part above case ALLEGRO_EVENT_DISPLAY_CLOSE: makes sure it can track whether keys were pressed anyway.

I tried to separate, as suggested, without much success. Unfortunately.

EDIT: Oh, now I got it!

#SelectExpand
1case ALLEGRO_EVENT_KEY_CHAR: 2 al_ustr_append_chr(text_typed,event.keyboard.unichar); 3 break;

I put this in separate, before the mouse event catcher, and it works!

DanielH

Separate input and logic

The timer should control the logic only and not the input.

Thread #618659. Printed from Allegro.cc