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 shifted keys leaking modifiers #136

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/action.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,21 @@ where
Trans,
/// A key code, i.e. a classic key.
KeyCode(K),
/// TOOD document
ModifiedKeyCode(
&'static (
// The modifiers to be applied to the key code. Unlike with
// MultipleKeyCodes, these modifiers will be released right after
// pressing and re-pressed before releasing the key. This ensures
// modifiers are not incorrectly applied to other keys that might be
// pressed while a modified key is still being held down, e.g. when
// rolling "&[". With MultipleKeyCodes, that would result in "&{",
// because the shift modifier is applied to "[" as well.
&'static [K],
// A key code, i.e. a classic key.
K,
),
),
/// Multiple key codes sent at the same time, as if these keys
/// were pressed at the same time. Useful to send a shifted key,
/// or complex shortcuts like Ctrl+Alt+Del in a single key press.
Expand Down
44 changes: 39 additions & 5 deletions src/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,9 +179,23 @@ impl<T> CustomEvent<T> {

#[derive(Debug, Eq, PartialEq)]
enum State<T: 'static, K: 'static + Copy> {
NormalKey { keycode: K, coord: (u8, u8) },
LayerModifier { value: usize, coord: (u8, u8) },
Custom { value: &'static T, coord: (u8, u8) },
NormalKey {
keycode: K,
coord: (u8, u8),
},
ModifiedKey {
modifiers: &'static [K],
keycode: K,
coord: (u8, u8),
},
LayerModifier {
value: usize,
coord: (u8, u8),
},
Custom {
value: &'static T,
coord: (u8, u8),
},
}
impl<T: 'static, K: 'static + Copy> Copy for State<T, K> {}
impl<T: 'static, K: 'static + Copy> Clone for State<T, K> {
Expand All @@ -194,6 +208,7 @@ impl<T: 'static, K: 'static + Copy> State<T, K> {
fn keycode(&self) -> Option<K> {
match self {
NormalKey { keycode, .. } => Some(*keycode),
ModifiedKey { keycode, .. } => Some(*keycode),
_ => None,
}
}
Expand All @@ -202,7 +217,11 @@ impl<T: 'static, K: 'static + Copy> State<T, K> {
}
fn release(&self, c: (u8, u8), custom: &mut CustomEvent<T>) -> Option<Self> {
match *self {
NormalKey { coord, .. } | LayerModifier { coord, .. } if coord == c => None,
NormalKey { coord, .. } | ModifiedKey { coord, .. } | LayerModifier { coord, .. }
if coord == c =>
{
None
}
Custom { value, coord } if coord == c => {
custom.update(CustomEvent::Release(value));
None
Expand Down Expand Up @@ -351,7 +370,14 @@ impl<const C: usize, const R: usize, const L: usize, T: 'static, K: 'static + Co
}
/// Iterates on the key codes of the current state.
pub fn keycodes(&self) -> impl Iterator<Item = K> + '_ {
self.states.iter().filter_map(State::keycode)
let additional_modifiers = match self.states.last() {
Some(ModifiedKey { modifiers, .. }) => *modifiers,
_ => &[],
};
self.states
.iter()
.filter_map(State::keycode)
.chain(additional_modifiers.iter().copied())
}
fn waiting_into_hold(&mut self) -> CustomEvent<T> {
if let Some(w) = &self.waiting {
Expand Down Expand Up @@ -489,6 +515,14 @@ impl<const C: usize, const R: usize, const L: usize, T: 'static, K: 'static + Co
self.tap_hold_tracker.coord = coord;
let _ = self.states.push(NormalKey { coord, keycode });
}
&ModifiedKeyCode(&(modifiers, keycode)) => {
self.tap_hold_tracker.coord = coord;
let _ = self.states.push(ModifiedKey {
coord,
modifiers,
keycode,
});
}
&MultipleKeyCodes(v) => {
self.tap_hold_tracker.coord = coord;
for &keycode in *v {
Expand Down