Skip to content
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
1 change: 1 addition & 0 deletions rmk-config/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -629,6 +629,7 @@ pub struct OneShotModifiersConfig {
pub(crate) struct CombosConfig {
pub combos: Vec<ComboConfig>,
pub timeout: Option<DurationMillis>,
pub prior_idle_time: Option<DurationMillis>,
}

/// Configurations for combo
Expand Down
2 changes: 2 additions & 0 deletions rmk-config/src/resolved/behavior.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ pub struct OneShot {
pub struct Combos {
pub combos: Vec<Combo>,
pub timeout_ms: Option<u64>,
pub prior_idle_time_ms: Option<u64>,
}

pub struct Combo {
Expand Down Expand Up @@ -116,6 +117,7 @@ impl crate::KeyboardTomlConfig {
})
.collect(),
timeout_ms: c.timeout.map(|t| t.0),
prior_idle_time_ms: c.prior_idle_time.map(|t| t.0),
});

let macros = toml_behavior.macros.map(|m| Macros {
Expand Down
8 changes: 8 additions & 0 deletions rmk-macro/src/codegen/behavior.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,13 @@ fn expand_combos(
None => quote! {},
};

let prior_idle_time = match &combos.prior_idle_time_ms {
Some(millis) => {
quote! { prior_idle_time: ::core::option::Option::Some(::embassy_time::Duration::from_millis(#millis)), }
}
None => quote! {},
};

quote! {
::rmk::config::CombosConfig {
combos: {
Expand All @@ -163,6 +170,7 @@ fn expand_combos(
})
},
#timeout
#prior_idle_time
..Default::default()
}
}
Expand Down
4 changes: 4 additions & 0 deletions rmk/src/config/behavior.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,17 @@ pub struct OneShotModifiersConfig {
pub struct CombosConfig {
pub combos: [Option<Combo>; COMBO_MAX_NUM],
pub timeout: Duration,
/// Cooldown after any key press before a combo can start recording.
/// `None` = no idle check (backward compatible). Equivalent to ZMK `require-prior-idle-ms`.
pub prior_idle_time: Option<Duration>,
}

impl Default for CombosConfig {
fn default() -> Self {
Self {
timeout: Duration::from_millis(50),
combos: core::array::from_fn(|_| None),
prior_idle_time: None,
}
}
}
Expand Down
41 changes: 26 additions & 15 deletions rmk/src/keyboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1027,21 +1027,32 @@ impl<'a> Keyboard<'a> {
return (None, true);
}
}

let max_size_of_updated_combo = self.keymap.with_combos_mut(|combos| {
combos
.iter_mut()
.filter_map(|c| c.as_mut())
.map(|c| {
if c.update(key_action, event, current_layer) {
info!("Updated combo: {:?}", c);
c.size()
} else {
0
}
})
.max()
});
// Combo idle cooldown: skip combo recording if within idle window
// Equivalent to ZMK's require-prior-idle-ms. Key still dispatches normally.
let skip_combo = event.pressed
&& self
.keymap
.combo_prior_idle_time()
.is_some_and(|idle_time| self.last_press_time.elapsed() < idle_time);

let max_size_of_updated_combo = if skip_combo {
None
} else {
self.keymap.with_combos_mut(|combos| {
combos
.iter_mut()
.filter_map(|c| c.as_mut())
.map(|c| {
if c.update(key_action, event, current_layer) {
info!("Updated combo: {:?}", c);
c.size()
} else {
0
}
})
.max()
})
};

if event.pressed
&& let Some(max_size) = max_size_of_updated_combo
Expand Down
4 changes: 4 additions & 0 deletions rmk/src/keymap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,10 @@ impl<'a> KeyMap<'a> {
self.inner.borrow().behavior.combo.timeout
}

pub(crate) fn combo_prior_idle_time(&self) -> Option<Duration> {
self.inner.borrow().behavior.combo.prior_idle_time
}

pub(crate) fn one_shot_timeout(&self) -> Duration {
self.inner.borrow().behavior.one_shot.timeout
}
Expand Down
4 changes: 4 additions & 0 deletions rmk/tests/keyboard_combo_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ pub fn get_combos_config() -> CombosConfig {
None,
],
timeout: Duration::from_millis(100),
prior_idle_time: None,
}
}

Expand Down Expand Up @@ -322,6 +323,7 @@ fn test_taphold_with_combo() {
))), None, None, None, None, None, None, None
],
timeout: Duration::from_millis(50),
prior_idle_time: None,
},
..Default::default()
};
Expand Down Expand Up @@ -364,6 +366,7 @@ fn test_re_press_combo_key_while_triggered_does_not_leak_to_hid() {
None,
],
timeout: Duration::from_millis(40),
prior_idle_time: None,
};
key_sequence_test! {
keyboard: create_test_keyboard_with_config(BehaviorConfig {
Expand Down Expand Up @@ -420,6 +423,7 @@ fn test_overlapping_triggered_combos_release_all_outputs() {
None,
],
timeout: Duration::from_millis(40),
prior_idle_time: None,
};
key_sequence_test! {
keyboard: create_test_keyboard_with_config(BehaviorConfig {
Expand Down
1 change: 1 addition & 0 deletions rmk/tests/keyboard_morse_hold_on_other_press_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ fn create_hold_on_other_key_press_keyboard_with_combo() -> Keyboard<'static> {
None,
],
timeout: Duration::from_millis(50),
prior_idle_time: None,
},
..BehaviorConfig::default()
})
Expand Down
1 change: 1 addition & 0 deletions rmk/tests/keyboard_morse_hrm_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ fn create_hrm_keyboard_with_combo() -> Keyboard<'static> {
None,
],
timeout: Duration::from_millis(50),
prior_idle_time: None,
},
..BehaviorConfig::default()
},
Expand Down
1 change: 1 addition & 0 deletions rmk/tests/keyboard_morse_permissive_hold_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ fn create_permissive_hold_keyboard_with_combo() -> Keyboard<'static> {
None,
],
timeout: Duration::from_millis(50),
prior_idle_time: None,
},
..BehaviorConfig::default()
})
Expand Down
2 changes: 2 additions & 0 deletions rmk/tests/keyboard_morse_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -733,6 +733,7 @@ fn test_morse_with_combo() {
))), None, None, None, None, None, None, None
],
timeout: Duration::from_millis(50),
prior_idle_time: None,
},
..BehaviorConfig::default()
}),
Expand Down Expand Up @@ -765,6 +766,7 @@ fn test_morse_with_combo_2() {
))), None, None, None, None, None, None, None
],
timeout: Duration::from_millis(50),
prior_idle_time: None,
},
..BehaviorConfig::default()
}),
Expand Down