Skip to content
This repository was archived by the owner on Jan 2, 2025. It is now read-only.
This repository was archived by the owner on Jan 2, 2025. It is now read-only.

Keyboard structs reform #184

@tomassedovic

Description

@tomassedovic

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:

  1. Am I just missing something obvious that would not require the rewrite of every codebase using tcod?
  2. Do we want Option<char> or just char with a potential 0 value? (the latter is what libtcod does)
  3. 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?

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions