How to ckeck for a sequence of key presses ?

Blitz3D Forums/Blitz3D Programming/How to ckeck for a sequence of key presses ?

eye(Posted 2005) [#1]
Hi,
Does anyone know how to do or have code to check if a key sequence has been pressed in quick succesion.

For example, like how you would execute a special move in MK/Street Fighter.

Thanks.


ErikT(Posted 2005) [#2]
Have a counter which updates every tenth of a second: if the counter is bigger than zero then counter = counter - 1. Now on keyhit <space> set counter to 20. Now you can check the counter to figure out if <space> was pressed within the last two seconds.


Techlord(Posted 2005) [#3]
eye

From what i understand, theres a queue for holding the keyboard, mouse, and joystick inputs. Which is why you have to use FlushKeys, FlushMouse, and FlushJoy commands to clear them. However, I haven't seen a easy means to access the contents of this 'queue'.

So as Erik suggested, you will have to devise a timed input - string mechanism. You are probably more interested in some code. I recommend checking out the Code Archives Input Section.

I'm interested in this as well, so I'll play with code and post what I put together.


eye(Posted 2005) [#4]
Thanks Erik and Frank.

I will too look at putting some code together and post it.


Diablo(Posted 2005) [#5]
you would be better using getkey and converting it into the string, adding it to a total string and then once the key ess timer has run to zero you can analyize the string and pick bits out, and then you could have an array of 'moves' and check if the strings equal each other

hope that wasn't to confusing,
i would write somr code but i'm at school :(


Diablo(Posted 2005) [#6]
heres some code



eye(Posted 2005) [#7]
Thanks Diablo,
I will give your code a try,
many thanks again.


_PJ_(Posted 2005) [#8]
Hey DIABLO, that's really quite neat. Your repeated-keypress idea seems to be the best way to do it. Makes so much sense once you know, eh?!

Gotta say your website's looking better now too! (Hadnt seen it since you first put a link on here!)


Techlord(Posted 2005) [#9]
eye,

I apologize. I havent been able to write any code for this. I would like to account for mouse/joystick movement. DIABLO's code looks ineresting. Perhaps I can build on it.


sswift(Posted 2005) [#10]
Just record the keys the player has pressed in a buffer which can hold X number of kays, where X is your most complicated move.

This buffer should rotate each time a key is added. In other words, when you want to add a key, move every key one to the left, which deletes the oldest key press, and then stick the new key on the end.

Ie:

Const KEYBOARD_BUFFER_SIZE = 10
Dim Keyboard_Buffer(KEYBOARD_BUFFER_SIZE)


; When a key you are scanning for is pressed:

; Rotate buffer to left.  Oldest key is lost.
For Loop = 1 to KEYBOARD_BUFFER_SIZE-1
   Keyboard_Buffer(Loop-1) = Keyboard_Buffer(Loop)
Next

; Add new key to end of buffer.
Keyboard_Buffer(KEYBOARD_BUFFER_SIZE) = NewKey



Then every loop, scan through that buffer, and determine if there are any groups of keys which represent a special move. If there are, activate the move, and clear the part of the buffer that contained it so it doesn't get triggered again.


You can get a lot more complicated than this if you want moves which are based on how long you hold down a particular direction, but for basic moves, even joystick based, this method should suffice.


It also occurs to me that can make things a little easier for yourself if you store the keys in a string. Then you can use INSTR() to find a group of moves in the string that matches the ones available. You don't even need to use ascii characters, because you can store scan codes in a string with chr$(scancode)


Techlord(Posted 2005) [#11]
sswift , does it again. Looking good.


sswift(Posted 2005) [#12]
Oh I should note that with this method, I assume you'll be using scancodes to determine when a key is pressed.

As such, things might be a little bit more complicated than I made it out above.

You will need to write some code which only returns a keypress as having happened the first time it changed state from up to down.

In other words, you might create two arrays:
Dim NewKeyState(256)
Dim OldKeyState(256)

In the first array, for each key you are scanning, you stick the result in there. Ie:

NewKeyState(200) = KeyDown(200) ; Up
NewKeyState(201) = KeyDown(201) ; Down
NewKeyState(202) = KeyDown(202) ; Left

Note the numbers are the same in both.
(I made those numbers up, they're not the real scancodes.)


Now you have an array which contains the current state of every key during a particular loop of the game.

Now, to see if a key has been pressed, loop through the two arrays and compare them.

For Loop = 0 to 255
   If NewKeyState(Loop) <> OldKeyState(Loop)
      If NewKeyState(Loop) = True Then the key went from up to down.  Add it to the keyboard buffer I descibed before.
      If NeyKeyState(Loop) = False Then the key went from down to up.  You might also want to add this to the keyboard buffer, but probably not, so ignore it.
   Endif
Next


And finally, after you've done that, you'll want to copy the new array over the old array to update it for the next loop.