Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » [a5] simulate keystroke?

This thread is locked; no one can reply to it. rss feed Print
[a5] simulate keystroke?
Mark Oates
Member #1,146
March 2001
avatar

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
Member #1,090
July 2003
avatar

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
Member #907
January 2001

(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
Member #476
June 2000
avatar

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.

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

weapon_S
Member #7,859
October 2006
avatar

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
Member #358
May 2000

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.

--
"Either help out or stop whining" - Evert

Thomas Fjellstrom
Member #476
June 2000
avatar

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.

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

Go to: