Skip to content

Commit 9b4343b

Browse files
committed
add missing keyboard improvements section
1 parent ae926ad commit 9b4343b

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed

news/2021-08-04-fastj-1-5-0-release.md

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,58 @@ display.modifyRenderSettings(RenderSettings.AntiAliasing.Enable);
106106
`java.awt.Paint` classes can now be compared using `DrawUtil.paintEquals`.
107107

108108

109+
### Keyboard Improvements
110+
FastJ's keyboard input API has improved by leaps and bounds since 1.4.0. Read on!
111+
112+
113+
#### Better Keys Handling
114+
In 1.4.0, checking for key equality was _fine_, but not very intuitive for situations such as determining if the right shift key is pressed:
115+
116+
```java title="Checking for Right Shift Key in FastJ 1.4.0"
117+
if (Keyboard.isKeyDown(Keys.Shift)) {
118+
// not what we want -- if no key location is specified, this type of key defaults to the left shift.
119+
}
120+
121+
if (Keyboard.isKeyDown(Keys.Shift, KeyLocation.Left)) {
122+
// this is what we want, but it makes life for beginners and the unsuspecting harder.
123+
}
124+
```
125+
126+
With FastJ 1.4.0, `Keys.Shift` is just an integer, meaning it can't safely store the key's location on the keyboard. In FastJ 1.5.0, `Keys` is now an enum and handles this well:
127+
128+
```java title="Checking for Right Shift Key in FastJ 1.5.0"
129+
if (Keyboard.isKeyDown(Keys.RightShift)) {
130+
// simpler to understand, and still achieves what we want!
131+
}
132+
```
133+
134+
Furthermore, the new Keys enum allows you to get a `Keys` instance based on a provided `java.awt.KeyEvent` _or_ provided key name, key code, and key location. We can use this to our advantage when dealing with raw `KeyEvent` instances, or building a key from scratch.
135+
136+
```java title="Getting Enter key from KeyEvent"
137+
// assume a KeyEvent keyEvent is present
138+
Keys keyfromKeyEvent = Keys.get(keyEvent);
139+
```
140+
141+
```java title="Getting Enter key from scratch"
142+
Keys keyFromScratch = Keys.get("Enter", KeyEvent.VK_ENTER, KeyEvent.KEY_LOCATION_STANDARD);
143+
```
144+
145+
146+
#### FastJ-Defined Keyboard Events
147+
FastJ's `KeyboardActionListener` no longer directly passes `KeyEvent` instances -- FastJ is now equipped with its own `KeyboardEvent` classes!
148+
149+
These classes (`KeyboardStateEvent` and `KeyboardTypedEvent`) provide easy access to the key event and key name, and `KeyboardStateEvent` provides a `getKey` method which returns the corresponding `Keys` instance for the event.
150+
151+
Both `KeyboardStateEvent` and `KeyboardTypedEvent` also provide a `fromKeyEvent` method which allows you to create one from a `KeyEvent`:
152+
```java title="Creating a KeyboardStateEvent/KeyboardTypedEvent from a KeyEvent"
153+
// assume KeyEvent keyEvent exists
154+
// assume KeyEvent keyTypedEvent exists
155+
156+
KeyboardStateEvent keyboardStateEvent = KeyboardStateEvent.fromKeyEvent(keyEvent);
157+
KeyboardTypedEvent keyboardTypedEvent = KeyboardTypedEvent.fromKeyEvent(keyTypedEvent);
158+
```
159+
160+
109161
### LogicManager Revamp
110162
The `LogicManager` class has been repurposed into an interface containing the basic needs for all logic managers. Two new managers implement this class:
111163
- `SceneManager` -- the old `LogicManager` class.

0 commit comments

Comments
 (0)