Skip to content

Input forwarding

JOID reads no input device. The window, or the host, receives the events and forwards each one to BridgeHandler.UI, the UIBridgeRegistry, which answers whether JOID consumed it. An event that JOID consumed must not reach the host.

This forwarder listens to the events of an imaginary engine, whose listeners return true to stop an event, and whose key codes are those of GLFW:

public final class EngineInputForwarder implements EngineListener {
private final KeyLayout layout;
public EngineInputForwarder(final Engine engine) {
this.layout = KeyLayout.from(key -> engine.getKeyName(GlfwKeys.getCode(key)));
}
@Override
public boolean keyPressed(final int code) {
final Key physicalKey = GlfwKeys.getPhysicalKey(code);
return BridgeHandler.UI.keyPressed(physicalKey, this.layout.translate(physicalKey));
}
@Override
public boolean keyRepeated(final int code) {
return BridgeHandler.UI.isKeyConsumed(GlfwKeys.getPhysicalKey(code));
}
@Override
public boolean keyReleased(final int code) {
return BridgeHandler.UI.keyReleased(GlfwKeys.getPhysicalKey(code));
}
@Override
public boolean charTyped(final int codepoint) {
return BridgeHandler.UI.charTyped(codepoint);
}
@Override
public boolean mouseMoved() {
return BridgeHandler.UI.mouseMoved();
}
@Override
public boolean mousePressed(final int button) {
return BridgeHandler.UI.mousePressed(MouseButton.of(button));
}
@Override
public boolean mouseReleased(final int button) {
return BridgeHandler.UI.mouseReleased(MouseButton.of(button));
}
@Override
public boolean mouseScrolled(final double x, final double y) {
return BridgeHandler.UI.mouseScroll(x, y);
}
}

Full file: EngineInputForwarder.java

The backend connects it in attachInput() (Writing a backend). On GLFW and on LWJGL 2, the base modules translate one event of the library into these calls and return the same answer: GlfwInputForwarder.forwardKey and GlfwInputForwarder.forwardMouseButton in base-lwjgl3-glfw, Lwjgl2InputForwarder.forwardMouseEvent and Lwjgl2InputForwarder.forwardKeyboardEvent in base-lwjgl2.

Event of your windowCallArguments
Key pressedUIBridgeRegistry.keyPressedthe Key at that physical position, then the key that the layout prints there
Key repeatedUIBridgeRegistry.isKeyConsumedthe physical key
Key releasedUIBridgeRegistry.keyReleasedthe physical key
Character typedUIBridgeRegistry.charTypedthe Unicode code point, a whole code point even outside the BMP
Mouse movedUIBridgeRegistry.mouseMovednone: JOID reads the position from the window bridge
Button pressedUIBridgeRegistry.mousePressedMouseButton.of of the button number: 0 left, 1 right, 2 middle, 3 back, 4 forward
Button releasedUIBridgeRegistry.mouseReleasedthe same button
WheelUIBridgeRegistry.mouseScrollhorizontal and vertical notches as GLFW reports them (positive is up), fractions allowed

Each call returns true when JOID consumed the event.

JOID records which presses it consumed, so that the host receives each key and button either completely or not at all: the release of a press that JOID consumed is consumed, and the release of a press that the host received is never consumed, even when a UI consumes the release itself.

EventGoes toConsumed when
Mouse releaseThe UI bridges that received the press of that button.One of them consumed the press.
Key releaseEvery UI bridge, and every UI that received its press, even a node hidden since.A UI bridge consumed the press of that key.
Escape on the top closeable UI that is not an overlayThat UI, which closes when no node handles the key and the UI accepts to close.Always: the host never receives it.
A press that an overlay lets through (its interaction does not cancel it)The UIs below when no node of the overlay consumed it; the lower bridges and the host in every case.Never by that overlay, and its release neither.
RuleWhy
Forward each event to JOID first; drop it when the call returns true, and give it to the host as if JOID did not exist when it returns false.A consumed event that reaches the host acts twice: a click on a button also fires the weapon of a game behind it.
Keep the handlers that the window had before the backend registered, and call them only for the events that JOID did not consume.The application keeps its own input behind JOID; the GLFW forwarder of base-lwjgl3-glfw does exactly that with the handlers set before register().
While you forward a mouse event, make the window bridge report the position of that event, not the newest position of the device.At every mouse call, JOID reads the position from the window bridge and finds the hovered nodes again: a move and a press that arrive in the same frame each reach the node under their own position. The forwarders of base-lwjgl2 and base-lwjgl3-glfw do it.
Send to keyPressed the physical position, then the key that the layout prints there; on a layout that your library cannot name, send the position twice.Games read the position to keep their movement keys under the hand, shortcuts and text read the printed key (Keyboard and shortcuts); KeyLayout computes the printed key from the name that your library gives each physical key.
Send the position alone to keyReleased.JOID keeps the printed key of every held position in a KeyState, so the release reaches the UIs with the key of its press, even when the layout changed in between; that state also answers Key.isDown for the layout keys, and the window bridge answers it for the others.
Never forward a key repeat of the system as a keyPressed: ask UIBridgeRegistry.isKeyConsumed, and drop the repeat when it returns true.The text fields of JOID repeat the keys they handle themselves, so a forwarded repeat types twice.
Send a key press and, when the key types a character, a separate character event.charTyped ignores the control characters (Enter U+000D, Backspace U+0008, the codes of Ctrl combinations) and returns false for them: Enter, Backspace and shortcuts arrive as key presses.
Forward every release, even when your host ignores them.Without the release of a button, JOID keeps a drag running.
SymptomCause
A click on a JOID button also acts in the hostThe host handles the event before it asks JOID, or ignores the return value.
A slider keeps following the mouse after the button is upThe release is not forwarded.
A fast click reaches the node where the pointer went right after itThe window bridge reports the newest position of the device while the event is forwarded, instead of the position of the event.
A held Backspace erases two characters at a timeThe repeats of the system reach keyPressed.
Typing in a text field also triggers shortcuts of the hostThe key press and the character are not both forwarded to JOID first.
A shortcut on Key.Z does not follow an AZERTY keyboardThe forwarder sends the position as the printed key.

Report a problem with this page