-
Notifications
You must be signed in to change notification settings - Fork 45
Keyboard structs reform #184
Comments
I've been thinking on this, but I don't think there's a better solution than this: struct Key {
code: KeyCode,
printable: char,
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
} Yes, I'm proposing dropping the |
Yeah, agreed on both counts (zero char and not being happy about duplicating alt/ctrl but they're convenient). |
The only other solution for the use tcod::input::KeyCode::*;
match root.wait_for_keypress(false) {
Key { code: Enter, modifiers: mods, .. } if mods.pressed(LeftAlt|RightAlt) => { // maximise the window }
Key { printable: '$', .. } => { // pay for items in a shop }
Key { printable: 'w', .. } | Key { code: NumPad5, .. } => { // wait a turn }
} Or we could return a tuple of use tcod::input::KeyCode::*;
match root.wait_for_keypress(false) {
(Key { code: Enter, .. }, mods) if mods.pressed(LeftAlt|RightAlt) => { },
(Key { printable: '$', .. }, _) => { // pay for items in a shop }
(Key { printable: 'w', .. } | Key { code: NumPad5, .. }, _) => { // wait a turn }
} Unfortunately this is not much more idiomatic: bitflags are not really used in idiomatic Rust code (as far as I know). But I can't think of an alternative (except for defining all possible combinations in an enum and matching on that): enum Modifiers {
LeftAlt,
RightAlt,
Alt,
LeftCtrl,
RightCtrl,
Ctrl,
Shift,
LeftAltLeftCtrl,
LeftAltRightCtrl,
LeftAltShift,
// ...
} Not very elegant (and hand-coding |
Yeah. Piston does something similar: it has all the distinct modifier keys in the KeyCode enum among all other keys and then a separate Modifiers bitflags struct that has the combinations. But I'd like to keep this reasonably close to libtcod (although implementing Let's just go with this for now, though. If you don't mind: struct Key {
code: KeyCode,
printable: char,
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
} |
I don't mind at all, even with my other suggestions I think that's the best idea. |
We need to expose both key code and the printable characters. With the previous design, it was impossible to match against characters on the number key row (e.g. '$'). This is a [breaking-change]. Fixes #184
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 withshift
: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:
Questions:
Option<char>
or justchar
with a potential0
value? (the latter is what libtcod does)Here's how the new struct could be used:
What do you folks think?
The text was updated successfully, but these errors were encountered: