AWT GUI Facade (8): Keyboard and key sequences

Published October 30, 2019
Advertisement

In this post, I add the keyboard management to the facade, and I propose a way to detect key sequences. These sequences are used, for example, in games like Street Fighter II, where characters move according to specific key combos.

Keyboard facade

I first design the keyboard facade. As I did for the mouse, I use a Factory Method pattern: I add a getKeyboard() method to the GUIFacade class that returns an instance of a Keyboard interface. This interface is defined as follow, and uses two other interfaces KeyEvent and KeyEventType:

awtfacade08a-1024x486.png

The method isKeyPressed() returns true if the key corresponding to keyCode is pressed. The key codes are the ones defined in java.awt.event.KeyEvent, for instance java.awt.event.KeyEvent.VK_UP. It is part of the Java standard library, so they are always available, we don’t need to create new ones. This method could be the only one in the Keyboard facade if we don’t need to detect key sequences.

The three other methods of Keyboard and the two interfaces KeyEvent and KeyEventType are dedicated to the detection of key sequences. They assume that there is a buffer that holds a list of key events. The user can access this buffer thanks to the following methods:

  • setMaxKeyEventLifeTime() defines the maximum lifetime of a key in the buffer (in nanoseconds). It means that key combos must happen in limited time; for instance for fast games, we can ask for combos succeeded in less than 200ms.
  • getKeyEvents() returns the list of last key events. I show below examples of parsing of this list.
  • clearKeyEvents() empties the event buffer. Once a key sequence is detected, we must remove it; otherwise, we will detect it at every game update.

The KeyEvent interface returns information about a key event: the key code with getKeyCode(), the event type with getType(), and the time when the event happens with getKeyTime(). The time is relative, it can only be used for comparison, for instance, for computing the delay between two key events.

Keyboard state

If we only need to know which key is currently pressed (or not), we only need the isKeyPressed() method of the Keyboard interface. For example, if we want to know the status of the arrow keys:


Keyboard keyboard = gui.getKeyboard(); # gui points to a GUIFacade
rightArrow = keyboard.isKeyPressed(java.awt.event.KeyEvent.VK_RIGHT);
leftArrow = keyboard.isKeyPressed(java.awt.event.KeyEvent.VK_LEFT);
upArrow = keyboard.isKeyPressed(java.awt.event.KeyEvent.VK_UP);
downArrow = keyboard.isKeyPressed(java.awt.event.KeyEvent.VK_DOWN);

 

This code should be put in the input handling part of the Game Loop pattern, for instance, the processInput() method I introduced in the mouse post.

Then, in the game update part of the Game Loop pattern, we can move a character:


if (rightArrow) {
    characterX += 1;
}
if (leftArrow) {
    characterX -= 1;
}
if (upArrow) {
    characterY -= 1;
}
if (downArrow) {
    characterX += 1;
}

 

Finally, in the rendering part of the Game Loop pattern, we draw the character


gui.drawSprite(characterX,characterY,spriteId);

 

I assume that drawSprite() is a method that draws the sprite spriteId at location (characterX, characterY).

With this approach, we can move a character at a speed that depends on several refresh rates (display, game state). It leads to many issues I’ll address in future posts.

Detect key sequences

Now let me present how to detect key sequences using the Keyboard facade I propose...

Continue reading on learngameprog.com...

0 likes 0 comments

Comments

Nobody has left a comment. You can be the first!
You must log in to join the conversation.
Don't have a GameDev.net account? Sign up!
Advertisement