We will assume that
you know how to import Minueto into your project and how to
create a MinuetoWindow
.
If this is not the case, please read How
to build your first game
Window?.
Minueto handles input
using a combination of event queues
and handlers. When input is received, it is placed in an event queue.
The appropriate handler can then be called to process
the event.
To process input from
the keyboard, you must first build a
class that implements the MinuetoKeyboardHandler
interface. In other words, that class must have the following two
functions: handleKeyPress
and handleKeyRelease
.
Those functions will be invoked each time a keyboard event is processed
Do not forget to add the implement
MinuetoKeyboardHandler
keywords in the class declaration.
The following example describes a class to handle keyboard input for our Demo. When the user press the Q key, our demo quits. All other input is ignored.
public class DemoKeyboardHandler implements MinuetoKeyboardHandler { public void handleKeyPress(int value) { switch(value) { case MinuetoKeyboard.KEY_Q: System.exit(0); break; default: // Ignore all other keys } } public void handleKeyRelease(int value) { // Do nothing on key release } public void handleKeyType(char key) { // Do nothing on key type } }
When a user presses a key on the keyboard, he generates a keyPress
event. When a user
releases a key, a keyRelease
event is generated. If a user presses a key but doesn't release it,
multiple keyPress
events are
generated. However, only one keyRelease
will be generated when he release the key. After a user releases the key, a
keyType
event is generated.
When building
a game, it is much better to use the keyPress
and the keyRelease
events. The
keyType
should be reserved for text-based
task, such as building a text box.
The second step is to build the event queue that will receive the
keyboard events. New events are store in a MinuetoEventQueue
,
but are only
processed when the handle
method is invoked.
MinuetoWindow window; MinuetoEventQueue queue; window = new MinuetoFrame(640, 480, true); queue = new MinuetoEventQueue(); window.registerKeyboardHandler(new DemoKeyboardHandler(), queue);
Notice that the registerKeyboardHandler
method also requires a MinuetoEventQueue
as a parameter. This will allow programmers to create different
event queues for different types of events.
Only one task is left. As mentioned before, events are not handled automatically. We need to add some code to process the events. The rendering loop is an ideal candidate since we want to process keyboard input before rendering each frame.
The following code checks to see if the event queue is empty. If it is
not empty, then the events are processed one at a time until the queue
is empty. Notice that you only need to invoke the handle
method. Minueto takes care
of figuring out what method from which handler should be used.
while(true) { while (queue.hasNext()) { queue.handle(); } window.render(); Thread.yield(); }
So far, we have a handler class, an event queue and a modified rendering loop. If we put all of these pieces together, we can build a keyboard demo.
In the beginning of this howto, I create a separate class to handle the keyboard input. However, nothing prevents me from using my Demo class as both the starting class and the keyboard handler.
import org.minueto.*; import org.minueto.handlers.*; import org.minueto.image.*; import org.minueto.window.*; public class Demo implements MinuetoKeyboardHandler { public static void main(String[] args) { MinuetoWindow window; MinuetoEventQueue queue; window = new MinuetoFrame(640, 480, true); queue = new MinuetoEventQueue(); window.registerKeyboardHandler(this, queue); window.setVisible(true); while(true) { while(queue.hasNext()) { queue.handle(); } window.render(); Thread.yield(); } } public void handleKeyPress(int value) { switch(value) { case MinuetoKeyboard.KEY_Q: System.exit(0); break; default: // Ignore all other keys } } public void handleKeyRelease(int value) { // Do nothing on key release } public void handleKeyType(char key) { // Do nothing on key type } }
You can learn more about handling input by looking at the handle demos found in the sample directory.