Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » [A5] simulate_keypress()?

This thread is locked; no one can reply to it. rss feed Print
[A5] simulate_keypress()?
Renato Riolino
Member #12,870
May 2011

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.

Edgar Reynaldo
Member #8,592
May 2007
avatar

Trent Gamblin
Member #261
April 2000
avatar

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.

Peter Wang
Member #23
April 2000

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.

Trent Gamblin
Member #261
April 2000
avatar

Oh, good to know.

Edgar Reynaldo
Member #8,592
May 2007
avatar

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?

#SelectExpand
1ALLEGRO_EVENT_SOURCE evt_src; 2al_init_user_event_source(evt_src); 3ALLEGRO_EVENT_QUEUE* queue = al_create_event_queue(); 4 5al_register_event_source(queue , &evt_src); 6 7// later 8 9// Simulate escape key press 10ALLEGRO_EVENT ev; 11ev.type = ALLEGRO_EVENT_KEY_DOWN; 12ev.source = &evt_src; 13ev.keyboard.keycode = ALLEGRO_KEY_ESCAPE; 14ev.keyboard.display = &evt_src; 15 16al_emit_user_event(&evt_src , &ev , NULL);

Trent Gamblin
Member #261
April 2000
avatar

That wasn't my concern, but the memory leak but Peter cleared that up.

SiegeLord
Member #7,827
October 2006
avatar

Would this work?

From the source of al_emit_user_event:

#SelectExpand
1bool al_emit_user_event(ALLEGRO_EVENT_SOURCE *src, 2 ALLEGRO_EVENT *event, void (*dtor)(ALLEGRO_USER_EVENT *)) 3{ 4 size_t num_queues; 5 bool rc; 6 7 ASSERT(src); 8 ASSERT(event);
9 ASSERT(ALLEGRO_EVENT_TYPE_IS_USER(event->any.type));
10 11 if (dtor) { 12 ALLEGRO_USER_EVENT_DESCRIPTOR *descr = al_malloc(sizeof(*descr)); 13 descr->refcount = 0; 14 descr->dtor = dtor; 15 event->user.__internal__descr = descr; 16 } 17 else {
18 event->user.__internal__descr = NULL;
19 }

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:

#SelectExpand
1typedef struct ALLEGRO_KEYBOARD_EVENT 2{ 3 _AL_EVENT_HEADER(struct ALLEGRO_KEYBOARD) 4 struct ALLEGRO_DISPLAY *display; /* the window the key was pressed in */ 5 int keycode; /* the physical key pressed */ 6 int unichar; /* unicode character or negative */ 7 unsigned int modifiers; /* bitfield */ 8 bool repeat; /* auto-repeated or not */ 9} ALLEGRO_KEYBOARD_EVENT; 10 11struct ALLEGRO_USER_EVENT 12{ 13 _AL_EVENT_HEADER(struct ALLEGRO_EVENT_SOURCE) 14 struct ALLEGRO_USER_EVENT_DESCRIPTOR *__internal__descr; 15 intptr_t data1; 16 intptr_t data2; 17 intptr_t data3; 18 intptr_t data4; 19};

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.

"For in much wisdom is much grief: and he that increases knowledge increases sorrow."-Ecclesiastes 1:18
[SiegeLord's Abode][Codes]:[DAllegro5]:[RustAllegro]

Go to: