Skip to content

Commit

Permalink
Update @OnKey documentation (#111)
Browse files Browse the repository at this point in the history
* docs: Update `@OnKey` documentation

* docs: Make explanation for `strict` more precise

Co-authored-by: Adrian Kunz <[email protected]>

---------

Co-authored-by: Adrian Kunz <[email protected]>
  • Loading branch information
LeStegii and Clashsoft authored Jun 13, 2024
1 parent 396272e commit 30b00fc
Showing 1 changed file with 19 additions and 11 deletions.
30 changes: 19 additions & 11 deletions docs/controller/10-key-events.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,18 @@
The framework provides an easy way of registering keybinds for a controller. By using the `@OnKey` annotation, you can
define methods that will be called when a key is pressed.

The annotation has multiple parameters for specifying the key or additional keys that have to be pressed.
The annotation has multiple parameters for specifying the key or additional keys (e.g. `shift`) that have to be pressed.
If such an option is set to `true`, the key has to be pressed.
If it is not specified or set to `false`, the event will trigger regardless of whether it is pressed or not, unless the `strict` option is used.
If it is set to `false` while `strict` is enabled, the additional key **must not** be pressed.
That means the `strict` is useful when you have many different keyboard shortcuts with the same key but different modifiers.

For more control, the annotated method can also take a `KeyEvent` as a parameter which will be passed to the method when
the key is pressed.

The annotation can be repeated to allow for multiple input options.

```java
import java.security.Key;

@OnKey()
public void keyPressed(KeyEvent event) {
Expand All @@ -27,6 +33,17 @@ public void onShiftP() {
// This method will be called when the shift and p key are pressed
// Also works with ctrl, alt and meta (e.g. windows key)
}

@OnKey(code = KeyCode.P, shift = false, strict = true)
public void onNotShiftP() {
// This method will be called when p is pressed and shift is not pressed
}

@OnKey(code = KeyCode.F)
@OnKey(code = KeyCode.G)
public void onFG() {
// This method will be called if f or g is pressed
}
```

Other parameters that can be used are `type` (e.g. `KEY_PRESSED` or `KEY_RELEASED`) and `target` which specifies where the event should be
Expand All @@ -35,15 +52,6 @@ captured. The target can be `STAGE` or `SCENE`.
Using `character` and `text` one can access the raw character that was pressed. `character` will be the character that
would result by pressing the key(s) (e.g. SHIFT + 'a' --> 'A') and `text` will be name of the key that was pressed (e.g. "CTRL" for the ctrl key).

See below for an example in action.

```java
@OnKey(code = KeyCode.R)
public void rollDice() {
// ...
}
```

<img width="640" height="360" src="../assets/key-event.gif" alt="Rolling a dice">

---
Expand Down

0 comments on commit 30b00fc

Please sign in to comment.