I am writing a Tetris clone in WPF. If I hold down the right arrow key, the current piece shifts right. For playability, I want to allow the user to press another key (i.e. F-key) and rotate the moving piece without having to let go of the right arrow key first. Currently when I do this, the piece stops shifting.
My first basic attempt at this was hooking into
Window_PreviewKeyDown(object sender, KeyEventArgs e)
and then sending a message to the controller layer.
How do I structure my input-listening code to allow this?
My current code Here
There should exist a KeyUp-event as well and by using both KeyDown and KeyUp you can achieve the effect you’re looking for. It can be done is several ways.
One would be to let your KeyDown event initiate a repeating action and let KeyUp cancel it.
Another would be to store whether or not certain keys are being held down. Keep a list of keys and toggle them on when KeyDown and off on KeyUp. In the games main loop, you’ll fire events corresponding to all the keys currently down.
An example:
And then you go through “heldDown” in your main loop and fire events for every meaningful key that’s being held down: