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

Conversation

@tomassedovic
Copy link
Owner

This fixes #184 because of which we couldn't access certain keys. The KeyState struct is renamed to Key and has fields code and printable that correspond to libtcod's vk and c.

This also adds two convenience fields: alt and ctrl that are true if the left or right variant is pressed.

This is a breaking change. To update your code, convert it to match on the Key struct. I.e. replace this:

match keypress.key {
    Special(Enter) if keypress.left_alt => { // fullscreen }
    Special(Escape) => { // exit game }
    Key::Printable('>') => { // descend }
}

with this:

match keypress {
    Key { code: Enter, alt: true, .. } => { // fullscreen }
    Key { code: Escape, .. } => { // exit game }
    Key { printable: '>', .. } => { // descend }
}

This lets us do the conversion in one place instead of three and let's
us change the struct more easily.
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
This makes it consistent with the Key struct and the Event struct enum.
@zsparal
Copy link
Collaborator

zsparal commented Aug 27, 2015

I like it. My only complaint with the current design is that matching an event is ridiculous:

match event {
    Some(_, Event::Key(Key { code: Enter, .. })) => {},
}

That' s a lot of noise for nothing, but I don't think it's solvable in current Rust. Ideally I'd like to be able to do something like this:

match event { 
    Some(_, Event::Key { code: Enter, .. }) => {},
}

This could be done if we declared Key inside the Event struct, but unfortunately it's used in console as well (Which is the recommended method of getting events, console or input?).

@tomassedovic
Copy link
Owner Author

Yeah, that is a bit silly. I wonder if the upcoming struct & enum unification could help here (haven't followed it too thoroughly yet).

If you're just getting keyboard input, console is the place to go, if you want mouse or both, then input. At least that's how I understand libtcod. It being a roguelike toolkit, mouse is sidelined a bit... Especially since it's more painful to match generic events instead of just the key ones.

@zsparal
Copy link
Collaborator

zsparal commented Aug 27, 2015

Well, other than that it seems good to me. Once it's merged I'll go and see if returning a dedicated Event type would make matching a bit easier (instead of the current tuple of (EventFlags, Event).

tomassedovic added a commit that referenced this pull request Aug 27, 2015
@tomassedovic tomassedovic merged commit d356016 into master Aug 27, 2015
@tomassedovic tomassedovic deleted the key-reform branch August 27, 2015 22:41
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Keyboard structs reform

3 participants