Quick question: How do I simulate a keypress on Allegro5 (ex: make a specific key goes into keyboard buffer so I can grab it later from keyboard event source?
Thanks.
Use al_init_user_event_source to initialize an ALLEGRO_EVENT_SOURCE which you can then register with al_register_event_source. Then you can have it emit an event to any registered queues with al_emit_user_event.
Will that really work though? The only way I see it working is if you have a custom event type, not ALLEGRO_EVENT_KEY_*. Which might be fine for the OP. But for user events you have to call al_unref_user_event on all of them or you'll be leaking memory.
But for user events you have to call al_unref_user_event on all of them or you'll be leaking memory.
Only if it's reference counted, i.e. if you use al_emit_user_event with a non-null destructor argument.
Oh, good to know.
Will that really work though? The only way I see it working is if you have a custom event type, not ALLEGRO_EVENT_KEY_*. Which might be fine for the OP. But for user events you have to call al_unref_user_event on all of them or you'll be leaking memory.
Well, it depends on whether or not allegro changes any fields of an event when it is emitted. If not, then you can just set the keyboard fields of the ALLEGRO_EVENT, as an ALLEGRO_USER_EVENT is part of the event union.
Would this work?
That wasn't my concern, but the memory leak but Peter cleared that up.
Would this work?
From the source of al_emit_user_event:
Firstly, there is an assertion there which will trigger in debug mode if you try to emit a non-user event. Secondly, al_emit_user_event assigns one field of the passed event. Looking at the two event types:
So that assignment clobbers the value of event.keyboard.display. It'll clobber useful values for other events too. So, in general, al_emit_user_event is for user events only. If this is a real need for emitting fake Allegro events, then such functionality remains to be implemented.