Mail Archives: djgpp/1999/02/16/06:53:46
Andrew Davidson writes:
> I'm a bit stuck trying to read a number of simultaneous key presses
> using Allegro's key[] structure. The code goes roughly as follows:
>
> if(keypressed()) {
> if(key[KEY_A]) set a flag;
> if(key[KEY_B]) set b flag;
> ...
> clear_keybuf();
> }
It doesn't make sense to mix calls to keypressed() and use of the
key[] array within the same piece of code: these are totally different
ways to represent the input, and don't mix well. The keypressed()
routine, along with readkey(), provide a buffered input queue similar
to the BIOS functions or stdin, wheras the key[] array provides raw
access to the current up/down state of each button. Checking the
keypressed() return value will not tell you whether any of the
key[] flags are currently set, but only if a keypress is currently
waiting in the readkey() input queue.
You probably don't want to be calling clear_keybuf() here, either.
That both flushes the input queue and resets all the key[] flags,
after which they won't correctly reflect the hardware state. Don't
do this in the middle of your input processing code. Just look at
the key[] values on their own: they contain all the info you will
need.
> So how can I get it to recognise that key 'a' may be held down
> continuously whilst keys 'b' through 'z' are being pressed?
Use the key[] array. If you want to detect change events rather than
just knowing the current state of a key, you'll need to build some
code of your own over the top of this, to store the previous state
and notice when it changes. It's up to you to process the information
into whatever form you need.
> Basically a funcition to sync the key[] array with the current
> state of the keys pressed on the keyboard is what I'm after.
The key[] array is always in sync with the hardware state: that is
the whole point of it existing in the first place!
Shawn Hargreaves.
- Raw text -