Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » [AG4.4] Correct Way to 'Unpress' a Key (Block Input)

This thread is locked; no one can reply to it. rss feed Print
[AG4.4] Correct Way to 'Unpress' a Key (Block Input)
ZoriaRPG
Member #16,714
July 2017
avatar

Would someone remind me: Is there a 'correct' way to remove a keypress from the key input buffer?

I seem to recall that I could write key[scancode] = 0, but the docs claim otherwise.

I'm finishing up some overdue overhauls of my script-system KB routines, by adding key_shifts and simulate_keypress().

IIRC, writing key[scancode] = ( > 0 ) does nothing, but writing 0 to it killed that input. If not, is there a good way to do that, or should I just document it as undefined?

extern volatile char key[KEY_MAX]; 
//Array of flags indicating key state.

P.S. I know all of the scancodes, but what are the legal flags? I suppose that I can skim through keyboard.c.

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

ZoriaRPG said:

Is there a 'correct' way to remove a keypress from the key input buffer?

Yes, ignore it? Or are you trying to correct something?

ZoriaRPG said:

I seem to recall that I could write key[scancode] = 0, but the docs claim otherwise.

Well that can't be true otherwise !key[KEY_ESC] would never work. :/

It's a global array. It's subject to modification by anyone anytime.

A quick glance through the code shows assignment of the value -1 or 0 to the 'key' array.

ZoriaRPG said:

remove a keypress from the key input buffer?

Do you mean from the buffer held by readkey? Not as far as I know.

Audric
Member #907
January 2001

I think that whenever I saw a key[x]=0 hack, it was an attempt to simulate the keypressed() and readkey() functions : The key[x] would only become true again when the keyboard repetition system sets it again.

If you're actually using on the keyboard buffer (not the key[] array), and you want to filter out one keypress in the middle, then you you have to rebuild the whole buffer:

// Initialize a list of keypresses
 while (!keypressed())
{
   // readkey(), add the reult to the list
}
clear_keybuf();
// Loop over the list
  // if the keypress must be kept
    simulate_keypress(k);

ZoriaRPG
Member #16,714
July 2017
avatar

I have access tp readkey() and simulate_keypress() in the scripting interface, to allow the user to script games that allow typing input. I thought it'd be nice to be able to un-set a keypress, including system keys.

(This differs from ignoring keypresses, as those keys do things in the core system, not in the users' scripts.)

Audric
Member #907
January 2001

System keys from operating system will not ever reach the game (The M of Windows-M, the F4 of Alt-F4, the TAB of Alt-TAB...)

If you mean keys that have a meaning for your own game engine (Esc,...) then the pseudo-code that I proposed will remove one or more keypress.
If you put it after the keys have been pressed or simulated, and before your own input code, it should act as if those keypresses didn't happen.

This does look a bit hackish though, especially if the scripts hard-code keyboard keys, while the game allows remapping. Creating a queue of game-controls would look much clearer (moveup, sword-button, shield-button).

Go to: