Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » best way to handle multiple keypresses

This thread is locked; no one can reply to it. rss feed Print
best way to handle multiple keypresses
Neil Walker
Member #210
April 2000
avatar

I ripped some code from one of the examples, but it only works when you have one key pressed at a time, i.e.

 if (event.type == ALLEGRO_EVENT_KEY_CHAR) 
    {
            if (event.keyboard.keycode == ALLEGRO_KEY_ESCAPE)
                break;
            if (event.keyboard.keycode==ALLEGRO_KEY_LEFT)
                mapxoff-=MAPINCREMENT;
            if (event.keyboard.keycode==ALLEGRO_KEY_RIGHT)
                mapxoff+=MAPINCREMENT;
            if (event.keyboard.keycode==ALLEGRO_KEY_UP)
                mapyoff-=MAPINCREMENT;
            if (event.keyboard.keycode==ALLEGRO_KEY_DOWN)
                mapyoff+=MAPINCREMENT;
        }

What's the A5 route, do you use another event or is there a global key array, etc? and what's the difference between EVENT_KEY_CHAR and KEY_UP/KEY_DOWN

Thanks.

Neil.
MAME Cabinet Blog / AXL LIBRARY (a games framework) / AXL Documentation and Tutorial

wii:0356-1384-6687-2022, kart:3308-4806-6002. XBOX:chucklepie

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

Neil Walker
Member #210
April 2000
avatar

But those bypass events?

Neil.
MAME Cabinet Blog / AXL LIBRARY (a games framework) / AXL Documentation and Tutorial

wii:0356-1384-6687-2022, kart:3308-4806-6002. XBOX:chucklepie

Thomas Fjellstrom
Member #476
June 2000
avatar

Just store your own key[] array. Then the keyboard events just fill in the key array, and your logic code accesses the key[] array instead.

append: Of course you should be using the KEY_PRESS/RELEASE events instead as well.

--
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

Audric
Member #907
January 2001

ALLEGRO_EVENT_KEY_CHAR is pretty much the same as using the keypressed() / readkey() interface from Allegro 4.
It automatically gets the behavior of keyboard repetition rate, so it's pretty handy behavior for applications that are like game editors.

These events are also the only good ones to get real text input: For example on French-located Windows I press:
^ key:
one ALLEGRO_EVENT_KEY_DOWN
no ALLEGRO_EVENT_KEY_CHAR
then 'a' key:
one ALLEGRO_EVENT_KEY_DOWN : key 'a'
one ALLEGRO_EVENT_KEY_CHAR : character 'â'

Matthew Leverton
Supreme Loser
January 1999
avatar

and what's the difference between EVENT_KEY_CHAR and KEY_UP/KEY_DOWN

KEY_CHAR essentially means you can look at the unichar field.

KEY_UP/KEY_DOWN represent physical key presses.

One KEY_DOWN could translate to multiple KEY_CHARs. e.g., Maybe you have a macro set up such that F1 generates the keys f,o,o.

Or multiple KEY_DOWNs could translate to a single KEY_CHAR. e.g., maybe pressing 'e (two KEY_DOWNs) gives a single KEY_CHAR é.

Arthur Kalliokoski
Second in Command
February 2005
avatar

Does it handle PAUSE/BREAK?

“Throughout history, poverty is the normal condition of man. Advances which permit this norm to be exceeded — here and there, now and then — are the work of an extremely small minority, frequently despised, often condemned, and almost always opposed by all right-thinking people. Whenever this tiny minority is kept from creating, or (as sometimes happens) is driven out of a society, the people then slip back into abject poverty. This is known as "bad luck.”

― Robert A. Heinlein

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

Just store your own key[] array. Then the keyboard events just fill in the key array, and your logic code accesses the key[] array instead.

Shouldn't this have the same results as if you used al_get_keyboard_state? Why do it yourself when Allegro 5 supposedly does it for you?

Matthew Leverton
Supreme Loser
January 1999
avatar

The benefit of doing it yourself is that you are guaranteed to never miss a state change.

If you use the Allegro state, then you may miss something if you don't check often enough. Even if you check the Allegro state when you receive a KEY_DOWN event, there's no guarantee that it is in sync with the event.

Neil Walker
Member #210
April 2000
avatar

It works a treat. Thanks.

Neil.
MAME Cabinet Blog / AXL LIBRARY (a games framework) / AXL Documentation and Tutorial

wii:0356-1384-6687-2022, kart:3308-4806-6002. XBOX:chucklepie

Go to: