[a5] simulate keystroke?
Mark Oates

Is it possible to simulate (trigger) a ALLEGRO_KEYBOARD_EVENT? or any event for that matter?

I'm trying to make a software keyboard and would like to just fire the key normally like nothing fancy is going on.

Dennis

I guess you could try using:

No guarantees it would work but I don't see why it should not if you make sure to add 1024 to the event.type before emitting it.

Then in the event evaluation, subtract 1024 from the type again if it's > 1024.

e.g. if you want to emit an ALLEGRO_EVENT_KEY_DOWN, you fill an ALLEGRO_KEYBOARD_EVENT and set the type to (ALLEGRO_EVENT_KEY_DOWN + 1024) and then emit that.

see also (snippet from the allegro source):

/* Function: ALLEGRO_EVENT_TYPE_IS_USER
 *
 *    1 <= n < 512  - builtin events
 *  512 <= n < 1024 - reserved user events (for addons)
 * 1024 <= n        - unreserved user events
 */
#define ALLEGRO_EVENT_TYPE_IS_USER(t)        ((t) >= 512)

It uses that in the emit user event function.

Of course, you won't be able to fire the key to other applications that way, only your own code will be able to see/use it.

Audric

(edit: to Mark, about the general strategy)
If you do it for text typing (every key_down has an effect), it will work. But you won't be able to determine faithfully the state of a key (pressed or released) by the last key_down / key_up event received.

Thomas Fjellstrom

In the case of a soft keyboard, you should probably just send pseudo KEY_CHAR events rather than KEY_UP/KEY_DOWN. That is if all you care about is text input.

weapon_S
Dennis said:

al_init_user_event_source
on the
al_get_keyboard_event_source

Bad idea.
I'm just blurting ideas.
In principal you might be able to just use a user event source and emit the event you want. It might be worth investigating whether the keyboard events are smaller than the event (biggest member of) the union. If so, you could add a custom flag/field.
If you keep your up and down events organized, it might misrepresent the (physical) keyboard state, but shouldn't cause much trouble. Then use a counter instead of a flag to represent the keyboard state in your program.

Elias

I'd say a better method is to just use separate user events. Call them FAKE_UP / FAKE_DOWN and adjust your game's input handling to listen to those in addition to the regular keyboard events. Can also add a FAKE_CHAR or whatever the same way.

Dennis' method should work of course, but it's a bit hackish.

Alternatively, we could adapt the event handling to allow sending non-user events I guess. But for now you are supposed to create a user event source and send only user events from it. And someone should update the documentation of al_emit_user_event to explicitly forbid sending of non-user events.

Thomas Fjellstrom
Elias said:

Alternatively, we could adapt the event handling to allow sending non-user events I guess. But for now you are supposed to create a user event source and send only user events from it. And someone should update the documentation of al_emit_user_event to explicitly forbid sending of non-user events.

Injecting input will probably be quite common, an al_inject/simulate_keypress might be something worth looking at.

Thread #610594. Printed from Allegro.cc