Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix broken keyboard input API #517

Open
narodnik opened this issue Jan 24, 2025 · 0 comments
Open

Fix broken keyboard input API #517

narodnik opened this issue Jan 24, 2025 · 0 comments

Comments

@narodnik
Copy link
Contributor

narodnik commented Jan 24, 2025

Proposed API:

fn key_event(KeyEvent, KeyPhase);

KeyEvent then has this API: https://doc.qt.io/qt-6/qkeyevent.html

KeyPhase has Press, Release, Canceled

Check this log:

char_event(") shift=true, repeat=false
char_event(") shift=true, repeat=true
char_event(") shift=true, repeat=true
char_event(') shift=false, repeat=true
char_event(') shift=false, repeat=true
char_event(') shift=false, repeat=true

This is the same key being held, but the API does not give any indication of that.

The proposed API would fix this, since KeyEvent gives access to the native virtual keycodes for that platform. They should not be relied upon to be consistent across platforms but to simply map to unique keys on that device. This would allow us to clearly see key down and release events despite the char/key changing.

Also you can call .chr() to get the actual char, or try to get the KeyCode. This would make existing code simpler too, since right now I have code like:

/// Filter these char events from being handled since we handle them
/// using the key_up/key_down events.
/// Avoids duplicate processing of keyboard input events.
pub static DISALLOWED_CHARS: &'static [char] = &['\r', '\u{8}', '\u{7f}', '\t', '\n'];

/// These keycodes are handled via normal key_up/key_down events.
/// Anything in this list must be disallowed char events.
pub static ALLOWED_KEYCODES: &'static [KeyCode] = &[
    KeyCode::Left,
    KeyCode::Right,
    KeyCode::Up,
    KeyCode::Down,
    KeyCode::Enter,
    KeyCode::Kp0,
    KeyCode::Kp1,
    KeyCode::Kp2,
    KeyCode::Kp3,
    KeyCode::Kp4,
    KeyCode::Kp5,
    KeyCode::Kp6,
    KeyCode::Kp7,
    KeyCode::Kp8,
    KeyCode::Kp9,
    KeyCode::KpDecimal,
    KeyCode::KpEnter,
    KeyCode::Delete,
    KeyCode::Backspace,
    KeyCode::Home,
    KeyCode::End,
];

With this change, I can simply handle all key events in one place directly rather than having to deal with crossover from both key_down(KeyCode::A) and char_event('A').

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant