This repository was archived by the owner on Jan 2, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 44
This repository was archived by the owner on Jan 2, 2025. It is now read-only.
Keyboard structs reform #184
Copy link
Copy link
Closed
Description
I think I've screwed up the design of the keyboard structs. Observe what happens when you press the number 4 on the US keyboard layout alone and with shift:
$ cargo run --example minimal
Pressed key: KeyState { key: Special(Number4), pressed: true, left_alt: false, left_ctrl: false, right_alt: false, right_ctrl: false, shift: false }
Pressed key: KeyState { key: Special(Number4), pressed: true, left_alt: false, left_ctrl: false, right_alt: false, right_ctrl: false, shift: true }
The latter case should give us some way of knowing that the $ character was entered. There are cases (especially on the number row) where libtcod returns both a printable character and keycode.
So here's how we could fix it:
struct Key {
code: KeyCode,
printable: Option<char>, // None if TCOD_key_t.c == 0
left_alt, right_alt, left_ctrl, right_ctrl, shift, // unchanged
alt: bool, // proposed for easier matching: true if left_alt || right_alt
ctrl: bool, // proposed for easier matching: true if left_ctrl || right_ctrl
}Questions:
- Am I just missing something obvious that would not require the rewrite of every codebase using tcod?
- Do we want
Option<char>or justcharwith a potential0value? (the latter is what libtcod does) - alt/ctrl convenience fields: yay or nay? We could have them as methods instead, but that would make for a more verbose matching.
Here's how the new struct could be used:
use tcod::input::KeyCode::*;
match root.wait_for_keypress(false) {
Key { code: Enter, alt: true, .. } => { // maximise the window }
Key { printable: '$', .. } => { // pay for items in a shop }
Key { printable: 'w', .. } | Key { code: NumPad5, .. } => { // wait a turn }
}What do you folks think?