Input
Open JavadocIn Pine, input handling is done by input systems.
Keyboard
Section titled “Keyboard”Keyboard keys are represented as values of the Key
enum, which maps keys to GLFW keyboard key tokens.
// Returns true in every frame while the space key is being pressedboolean spacePressed = input.getKey(Key.SPACE);
// Returns true in the first frame the space key is downboolean spaceDown = input.getKeyDown(Key.SPACE);
// Returns true in every frame while both the left control and space keys are being pressedboolean controlSpacePressed = input.getKeys(Key.L_CONTROL, Key.SPACE)
// Returns true in every frame while either the left or right control keys are being pressedboolean controlPressed = input.getAnyKey(Key.L_CONTROL, Key.R_CONTROL)
Stopping propagation
Section titled “Stopping propagation”To prevent later systems from reading the same keyboard input, use the following methods:
boolean spacePressed = input.getKey(Key.SPACE, true);boolean spaceDown = input.getKeyDown(Key.SPACE, true);
Using GLFW keyboard key tokens
Section titled “Using GLFW keyboard key tokens”Instead of using the Key
enum, you can also use GLFW keyboard key tokens directly.
import static org.lwjgl.glfw.GLFW.*;
boolean spacePressed = input.getKey(GLFW_KEY_SPACE);
This is the equivalent of:
boolean spacePressed = input.getKey(Key.SPACE);
Cursor position
Section titled “Cursor position”The position of the cursor can be retrieved using the following methods. These methods will return null
if the cursor was already blocked by an entity.
// Returns the screen coordinates of the cursorVector2i cursorPosition = input.getCursor();
// Returns the position of the cursor transformed to a global world position, relative to the cameraVector2f cursorWorldPosition = input.getWorldCursor();
Blocking
Section titled “Blocking”The cursor can be blocked by an entity to prevent later systems from reading the cursor position. This is only possible if the entity is currently active.
// Block the cursor using an entity, if it hasn't been blocked yetinput.blockCursor(entity);
// Block the cursor using an entity, regardless of whether it has already been blocked yetinput.blockCursor(entity, true);
// Returns true if the cursor has been blocked by an entityboolean isCursorBlocked = input.isCursorBlocked();
// Returns the entity that is blocking the cursor, or null if there isn't oneEntity cursorBlocker = input.getCursorBlocker();
Buttons
Section titled “Buttons”Mouse buttons are represented as values of the MouseButton
enum, which maps buttons to GLFW mouse buttons.
// Returns true in every frame while the left mouse button is being pressedboolean leftMouseButtonPressed = input.getMouseButton(MouseButton.LEFT);
// Returns true in the first frame the left mouse button is downboolean leftMouseButtonDown = input.getMouseButton(MouseButton.LEFT);
Stopping propagation
Section titled “Stopping propagation”To prevent later systems from reading the same mouse button input, use the following methods:
boolean leftMouseButtonPressed = input.getMouseButton(MouseButton.LEFT, true);boolean leftMouseButtonDown = input.getMouseButton(MouseButton.LEFT, true);
Using GLFW mouse buttons
Section titled “Using GLFW mouse buttons”Instead of using the MouseButton
enum, you can also use GLFW mouse buttons directly.
import static org.lwjgl.glfw.GLFW.*;
boolean leftMouseButtonPressed = input.getMouseButton(GLFW_MOUSE_BUTTON_1);
Scroll
Section titled “Scroll”Horizontal and vertical scroll values can be read using the following methods.
float scrollX = input.getScrollX();float scrollY = input.getScrollY();
Cursor type and image
Section titled “Cursor type and image”The appearance of the cursor can be changed using the following methods. Cursors can either have a standard appearance (cursor type) or a custom appearace (cursor image).
// Show the default hand (or pointer) cursorinput.setCursorType(CursorType.HAND);// Orimport static org.lwjgl.glfw.GLFW.*;input.setCursorType(GLFW_ARROW_CURSOR);
// Show a custom cursor imageImage cursorImage = ResourcePool.loadImage("textures/cursor.png");input.setCursorImage(cursorImage);
// Show a custom cursor image with an offset of 16x16 pixelsinput.setCursorImage(cursorImage, 16, 16);input.setCursorImage(cursorImage, new Vector2i(16, 16));