From 30b00fcbd241e515fe6088c37f78f96ac267cd68 Mon Sep 17 00:00:00 2001 From: Paul Mertens <50475262+LeStegii@users.noreply.github.com> Date: Thu, 13 Jun 2024 14:20:34 +0200 Subject: [PATCH] Update `@OnKey` documentation (#111) * docs: Update `@OnKey` documentation * docs: Make explanation for `strict` more precise Co-authored-by: Adrian Kunz --------- Co-authored-by: Adrian Kunz --- docs/controller/10-key-events.md | 30 +++++++++++++++++++----------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/docs/controller/10-key-events.md b/docs/controller/10-key-events.md index bead4523..a714940b 100644 --- a/docs/controller/10-key-events.md +++ b/docs/controller/10-key-events.md @@ -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) { @@ -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 @@ -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() { - // ... -} -``` - Rolling a dice ---