Skip to content
This repository has been archived by the owner on Jun 12, 2019. It is now read-only.
pdinklag edited this page Dec 31, 2012 · 5 revisions

In the Windows chapter, a simple event handling loop was introduced that checked whether the user clicked the close button. This chapter will elaborate on this and all of the input event system.

Any input by the user to the window will cause an Event. These events can be polled from the window in the following fashion:

for(Event event : window.pollEvents()) {
    //Handle event
}

So basically, we fetch all events that occured since we checked last (most likely since the last frame) and iterate over them.

To distinguish between events, every event has a type that can be retrieved using the [getType](http://jsfml.org/javadoc/org/jsfml/window/event/Event.html#getType(\)) method. Different types of events have different data attached to them, so it makes sense to dedicate a different handling mechanism to all event types that interest us:

for(Event event : window.pollEvents()) {
    switch(event.type) {
        case CLOSED:
            System.out.println("The user pressed the close button!");
            window.close();
            break();

        case KEY_PRESSED:
            System.out.println("The user pressed a keyboard key!");
            break();

        case MOUSE_WHEEL_MOVED:
            System.out.println("The user moved the mouse wheel!");
            break();
    }
}

How do we get to the type-specific data? JSFML provides a specialization of various event types in the event package.

For instance, if an event's type is KEY_PRESSED, the event object can be converted to a KeyEvent:

// ...
case KEY_PRESSED:
    KeyEvent keyEvent = event.asKeyEvent();
    System.out.println("The user pressed the following key: " + keyEvent.key);
    break;
// ...

What event type can be converted into which event class can be found in the descriptions of the single types on the Event.Type javadoc page. Furthermore, the javadoc pages of the event package provide a good insight as to what special data you can retrieve from the various event types. A few common usecases are described below.

Keyboard

Keyboard events are represented by the KeyEvent class. They contain a key code that represents the key that was pressed or released.

Additionally, key events provide information whether certain control keys were being held down when the event occured:

Key Repeat

When you hold a key down in a text field, you know how after a while, the same character will be entered repeatedly. This feature is called key repeat and can be disabled for a Window using the [setKeyRepeat](http://jsfml.org/javadoc/org/jsfml/window/Window.html#setKeyRepeatEnabled(boolean\)) method. By default, key repeat is enabled.

Global Keyboard State

JSFML also provides access to the global realtime state of the keyboard via the Keyboard class. It provides the method [isKeyPressed](http://jsfml.org/javadoc/org/jsfml/window/Keyboard.html#isKeyPressed(org.jsfml.window.Keyboard.Key\)), which will tell you whether a key is currently being held down. However, this method works independently of your window. So even if your window is currently inactive (e.g. in the background), this method will report a key as pressed.

Text

When the user presses a key, it only sends its key code. The text that is typed using this key, however, depends on the user's keyboard, the system locale and what modifer keys are pressed.

JSFML provides a way to find out which character was typed. Everytime the user types a key (this includes key repeat), a TEXT_ENTERED event is fired. The underlying TextEvent object provides access to the key that was typed.

// ...
case TEXT_ENTERED:
    TextEvent textEvent = event.asTextEvent();
    System.out.println("The user typed the following character: " + textEvent.getChar());
    break;
// ...

Mouse

Mouse events are represented by the MouseEvent class. Every MouseEvent holds the information about where on the screen the mouse cursor was located when the event occured. This position can be retrieved via the public position field.

When the user presses a mouse button, a MouseButtonEvent is created, which provides a way to access the button that was pressed.

In case the user moves the mouse wheel (if available), a MouseWheelEvent is fired, which contains the amount of ticks that the mouse wheel was moved.

Global Mouse State

At any given time, the Mouse class provides methods to find the current position of the mouse cursor as well as which buttons are currently pressed. Note that this class operates independently of the active window, so holding a button in another window will be detected by it.

Joysticks

Joysticks (more commonly refered to as gamepads these days) are covered by the JoystickEvent class. JoystickEvents hold information about what joystick it refers to by providing a joystickId.

A JoystickButtonEvent is fired when a button was pressed, respectively a JoystickMoveEvent is fired when one of the joystick axes (if available) was moved.

When joysticks get connected or disconnected (e.g. via USB), the JOYSTICK_CONNECTED or JOYSTICK_DISCONNECTED event types can be caught.

Joystick treshold

To control when a JoystickMoveEvent is fired, the Window class provides the [setJoystickTreshold](http://jsfml.org/javadoc/org/jsfml/window/Window.html#setJoystickTreshold(float\)) to set a treshold between 0 and 100%. A JoystickMoveEvent will only be fired if the axes' position is greater than this treshold.

For instance, the following code means that a JoystickMoveEvent will only be fired if the axis is halfway moved to the edge:

window.setJoystickTreshold(50);

The default treshold is 0.1%, which means an event for almost every axis movement.

Global Joystick State

At any given time, the Joystick class provides methods to find the current axis positions as well as which buttons are currently being pressed. This, however, works independently of the active window.

Clone this wiki locally