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

feat!: clear non-oneshot output chord on next action #552

Merged
merged 1 commit into from
Aug 30, 2023
Merged
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
8 changes: 6 additions & 2 deletions cfg_samples/kanata.kbd
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ be able to configure kanata.

If you follow along with the examples, you should be fine. Kanata should
also hopefully have helpful error messages in case something goes wrong.
If you need help, you are welcome to ask.
If you need help, please feel welcome to ask in the GitHub discussions.
|#

;; One defcfg entry may be added if desired. This is used for configuration
;; key-value pairs that change kanata's behaviour at a global level.
;; All configuration items are optional.
;; All defcfg options are optional.
(defcfg
;; Your keyboard device will likely differ from this. I believe /dev/input/by-id/
;; is preferable; I recall reading that it's less likely to change names on you,
Expand Down Expand Up @@ -384,6 +384,10 @@ If you need help, you are welcome to ask.
;; same and only one is allowed in a single chord. This chord can be useful for
;; international layouts.
;;
;; A special behaviour of output chords is that if another key is pressed,
;; all of the chord keys will be released. For the explanation about why
;; this is the case, see the configuration guide.
;;
;; This use case for multi is typing an all-caps string.
alp (multi lsft a b c d e f g h i j k l m n o p q r s t u v w x y z)

Expand Down
14 changes: 14 additions & 0 deletions docs/config.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -932,6 +932,8 @@ These modifiers may be combined together if desired.
[source]
----
(defalias
;; Type exclamation mark (US layout)
ex! S-1
;; Ctrl+C: send SIGINT to a Linux terminal program
int C-c
;; Win+Tab: open Windows' Task View
Expand All @@ -942,6 +944,18 @@ These modifiers may be combined together if desired.
)
----

A special behaviour of output chords is that if another key is pressed,
all of the chord keys will be released
before the newly pressed key action activates.
Output chords are typically used do one-off actions such as:

- type a symbol, e.g. `S-1`
- type a special/accented character, e.g. `RA-a`
- do a special action like `C-c` to send `SIGTERM` in the terminal

The modifier pressed with these one-off action
is usually not desired for subsequent actions.

[[repeat-key]]
=== Repeat key
<<table-of-contents,Back to ToC>>
Expand Down
54 changes: 52 additions & 2 deletions keyberon/src/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,11 +169,24 @@ impl<'a, T> CustomEvent<'a, T> {
}
}

/// Metadata about normal key flags.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
pub struct NormalKeyFlags(u16);

const NORMAL_KEY_FLAG_CLEAR_ON_NEXT_ACTION: u16 = 0x0001;

impl NormalKeyFlags {
pub fn clear_on_next_action(self) -> bool {
(self.0 & NORMAL_KEY_FLAG_CLEAR_ON_NEXT_ACTION) == NORMAL_KEY_FLAG_CLEAR_ON_NEXT_ACTION
}
}

#[derive(Debug, Eq, PartialEq)]
pub enum State<'a, T: 'a> {
NormalKey {
keycode: KeyCode,
coord: KCoord,
flags: NormalKeyFlags,
},
LayerModifier {
value: usize,
Expand Down Expand Up @@ -1167,6 +1180,10 @@ impl<'a, const C: usize, const R: usize, const L: usize, T: 'a + Copy + std::fmt
self.last_press_tracker.tap_hold_timeout = 0;
}
use Action::*;
self.states.retain(|s| match s {
NormalKey { flags, .. } => !flags.clear_on_next_action(),
_ => true,
});
match action {
NoOp | Trans => {
if !is_oneshot {
Expand Down Expand Up @@ -1303,7 +1320,11 @@ impl<'a, const C: usize, const R: usize, const L: usize, T: 'a + Copy + std::fmt
}
&KeyCode(keycode) => {
self.last_press_tracker.coord = coord;
let _ = self.states.push(NormalKey { coord, keycode });
let _ = self.states.push(NormalKey {
coord,
keycode,
flags: NormalKeyFlags(0),
});
if !is_oneshot {
self.oneshot
.handle_press(OneShotHandlePressKey::Other(coord));
Expand All @@ -1313,7 +1334,23 @@ impl<'a, const C: usize, const R: usize, const L: usize, T: 'a + Copy + std::fmt
&MultipleKeyCodes(v) => {
self.last_press_tracker.coord = coord;
for &keycode in *v {
let _ = self.states.push(NormalKey { coord, keycode });
let _ = self.states.push(NormalKey {
coord,
keycode,
// In Kanata, this action is only ever used with output chords. Output
// chords within a one-shot are ignored because someone might do something
// like (one-shot C-S-lalt to get 3 modifiers. These are probably intended
// to remain held. However, other output chords are usually used to type
// symbols or accented characters, e.g. S-1 or RA-a. Clearing chord keys on
// the next action allows a subsequent typed key to not have modifiers
// alongside it. But if the symbol or accented character is held down, key
// repeat works just fine.
flags: NormalKeyFlags(if is_oneshot {
0
} else {
NORMAL_KEY_FLAG_CLEAR_ON_NEXT_ACTION
}),
});
}
if !is_oneshot {
self.oneshot
Expand Down Expand Up @@ -3357,4 +3394,17 @@ mod test {
assert_eq!(CustomEvent::NoEvent, layout.tick());
assert_keys(&[], layout.keycodes());
}

#[test]
fn test_clear_multiple_keycodes() {
static LAYERS: Layers<2, 1, 1> = [[[k(A), MultipleKeyCodes(&[LCtrl, Enter].as_slice())]]];
let mut layout = Layout::new(&LAYERS);
layout.event(Press(0, 1));
assert_eq!(CustomEvent::NoEvent, layout.tick());
assert_keys(&[LCtrl, Enter], layout.keycodes());
// Cancel chord keys on next keypress.
layout.event(Press(0, 0));
assert_eq!(CustomEvent::NoEvent, layout.tick());
assert_keys(&[A], layout.keycodes());
}
}
5 changes: 4 additions & 1 deletion parser/src/cfg/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2900,7 +2900,10 @@ fn parse_switch(ac_params: &[SExpr], s: &ParsedState) -> Result<&'static KanataA
let action = parse_action(action, s)?;

let Some(break_or_fallthrough) = break_or_fallthrough_expr.atom(s.vars()) else {
bail_expr!(break_or_fallthrough_expr, "{ERR_STR}\nthis must be one of: break, fallthrough");
bail_expr!(
break_or_fallthrough_expr,
"{ERR_STR}\nthis must be one of: break, fallthrough"
);
};
let break_or_fallthrough = match break_or_fallthrough {
"break" => BreakOrFallthrough::Break,
Expand Down
10 changes: 8 additions & 2 deletions src/kanata/windows/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,14 @@ impl Kanata {
pub fn check_release_non_physical_shift(&mut self) -> Result<()> {
fn state_filter(v: &State<'_, &&[&CustomAction]>) -> Option<State<'static, ()>> {
match v {
State::NormalKey { keycode, coord } => Some(State::NormalKey::<()> {
State::NormalKey {
keycode,
coord,
flags,
} => Some(State::NormalKey::<()> {
keycode: *keycode,
coord: *coord,
flags: *flags,
}),
State::FakeKey { keycode } => Some(State::FakeKey::<()> { keycode: *keycode }),
_ => None,
Expand All @@ -81,7 +86,7 @@ impl Kanata {
// this should not be a problem. State does not implement Hash so can't use a HashSet. A
// HashSet might perform worse anyway.
for prev_state in prev_states.iter() {
if let State::NormalKey { keycode, coord } = prev_state {
if let State::NormalKey { keycode, coord, .. } = prev_state {
if !matches!(keycode, KeyCode::LShift | KeyCode::RShift)
|| (matches!(keycode, KeyCode::LShift)
&& coord.1 == u16::from(OsCode::KEY_LEFTSHIFT))
Expand All @@ -104,6 +109,7 @@ impl Kanata {
State::NormalKey {
keycode: cur_kc,
coord: cur_coord,
..
} => cur_kc != keycode && *cur_coord != (0, u16::from(OsCode::from(keycode))),
_ => true,
});
Expand Down
12 changes: 12 additions & 0 deletions src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,15 @@ fn parse_all_keys() {
))
.unwrap();
}

#[test]
fn sizeof_state() {
assert_eq!(
std::mem::size_of::<
kanata_keyberon::layout::State<
&'static &'static [&'static kanata_parser::custom_action::CustomAction],
>,
>(),
2 * std::mem::size_of::<usize>()
);
}