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

insert_char() and key_down/key_up should be unified, and use keysyms directly #467

Open
narodnik opened this issue Jul 4, 2024 · 1 comment

Comments

@narodnik
Copy link
Contributor

narodnik commented Jul 4, 2024

In my code, I need smooth repeating keys. We do this with a special gadget that takes miniquad key events, and tracks the time. This allows add an initial delay and time between actions.

struct PressedKeysSmoothRepeat {
    /// When holding keys, we track from start and last sent time.
    /// This is useful for initial delay and smooth scrolling.
    pressed_keys: HashMap<String, RepeatingKeyTimer>,
    /// Initial delay before allowing keys
    start_delay: u32,
    /// Minimum time between repeated keys
    step_time: u32,
}

impl PressedKeysSmoothRepeat {
    fn new(start_delay: u32, step_time: u32) -> Self {
        Self { pressed_keys: HashMap::new(), start_delay, step_time }
    }

    fn key_down(&mut self, key: &str, repeat: bool) -> u32 {
        if !repeat {
            return 1;
        }

        // Insert key if not exists
        if !self.pressed_keys.contains_key(key) {
            self.pressed_keys.insert(key.to_string(), RepeatingKeyTimer::new());
        }

        let repeater = self.pressed_keys.get_mut(key).expect("repeat map");
        repeater.update(self.start_delay, self.step_time)
    }

    fn key_up(&mut self, key: &str) {
        self.pressed_keys.remove(key);
    }
}

struct RepeatingKeyTimer {
    start: Instant,
    actions: u32,
}

impl RepeatingKeyTimer {
    fn new() -> Self {
        Self { start: Instant::now(), actions: 0 }
    }

    fn update(&mut self, start_delay: u32, step_time: u32) -> u32 {
        let elapsed = self.start.elapsed().as_millis();
        if elapsed < start_delay as u128 {
            return 0
        }
        let total_actions = ((elapsed - start_delay as u128) / step_time as u128) as u32;
        let remaining_actions = total_actions - self.actions;
        self.actions = total_actions;
        remaining_actions
    }
}

We need the key_up event to remove the key from the hashmap - see PressedKeysSmoothRepeat::key_up(), but there is no key up event for insert_char.

What if insert_char is deprecated, and we replace KeyCodes in key_up/key_down with xkb keysyms?

@narodnik narodnik changed the title inset_char() and key_down/key_up should be unified, and use keysyms directly insert_char() and key_down/key_up should be unified, and use keysyms directly Jul 4, 2024
@narodnik
Copy link
Contributor Author

narodnik commented Jul 4, 2024

Right now I'm using the repeat flag to detect key ups.

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