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

Modifier tap hold in keyboard toml #222

Merged
merged 7 commits into from
Dec 29, 2024
Merged
Show file tree
Hide file tree
Changes from 5 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
5 changes: 4 additions & 1 deletion docs/src/keyboard_configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -151,9 +151,12 @@ The key string should follow several rules:
7. Use `"TT(n)"` to create a layer activate or tap toggle action, `n` is the layer number
8. Use `"TG(n)"` to create a layer toggle action, `n` is the layer number
9. Use `"TO(n)"` to create a layer toggle only action (activate layer `n` and deactivate all other layers), `n` is the layer number

The definitions of those operations are same with QMK, you can found [here](https://docs.qmk.fm/#/feature_layers). If you want other actions, please [fire an issue](https://github.com/HaoboGu/rmk/issues/new).

4. For modifier-tap-hold, use `MTH(key, modifier)` where the modifier can be a chain like explained on point 1. For example for a Home row modifier config you can use `MTH(F,LShift)`
hariseldon78 marked this conversation as resolved.
Show resolved Hide resolved

5. For generic key tap-hold, use `TH(key-tap, key-hold)`.

### `[behavior]`

Expand Down
190 changes: 94 additions & 96 deletions rmk-macro/src/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,43 @@ fn expand_row(row: Vec<String>) -> TokenStream2 {
quote! { [#(#keys), *] }
}

/// Get modifier combination, in types of mod1 | mod2 | ...
hariseldon78 marked this conversation as resolved.
Show resolved Hide resolved
fn parse_modifiers(modifiers_str: &str) -> (bool, bool, bool, bool, bool){
let mut right = false;
let mut gui = false;
let mut alt = false;
let mut shift = false;
let mut ctrl = false;
let tokens = modifiers_str.split_terminator("|");
tokens.for_each(|w| {
let w = w.trim();
match w {
"LShift" => shift = true,
"LCtrl" => ctrl = true,
"LAlt" => alt = true,
"LGui" => gui = true,
"RShift" => {
right = true;
shift = true;
}
"RCtrl" => {
right = true;
ctrl = true;
}
"RAlt" => {
right = true;
alt = true;
}
"Rgui" => {
right = true;
gui = true;
}
_ => (),
}
});
return (right, gui, alt, shift, ctrl);
}

/// Parse the key string at a single position
fn parse_key(key: String) -> TokenStream2 {
if key.len() < 5 {
Expand All @@ -63,38 +100,7 @@ fn parse_key(key: String) -> TokenStream2 {

let ident = format_ident!("{}", keys[0].to_string());

// Get modifier combination, in types of mod1 | mod2 | ...
let mut right = false;
let mut gui = false;
let mut alt = false;
let mut shift = false;
let mut ctrl = false;
keys[1].split_terminator("|").for_each(|w| {
let w = w.trim();
match w {
"LShift" => shift = true,
"LCtrl" => ctrl = true,
"LAlt" => alt = true,
"Lgui" => gui = true,
"RShift" => {
right = true;
shift = true;
}
"RCtrl" => {
right = true;
ctrl = true;
}
"RAlt" => {
right = true;
alt = true;
}
"Rgui" => {
right = true;
gui = true;
}
_ => (),
}
});
let (right, gui, alt, shift, ctrl) = parse_modifiers(keys[1]);

if (gui || alt || shift || ctrl) == false {
return quote! {
Expand Down Expand Up @@ -124,38 +130,7 @@ fn parse_key(key: String) -> TokenStream2 {
}
"OSM" => {
if let Some(internal) = key.trim_start_matches("OSM(").strip_suffix(")") {
// Get modifier combination, in types of mod1 | mod2 | ...
let mut right = false;
let mut gui = false;
let mut alt = false;
let mut shift = false;
let mut ctrl = false;
internal.split_terminator("|").for_each(|w| {
let w = w.trim();
match w {
"LShift" => shift = true,
"LCtrl" => ctrl = true,
"LAlt" => alt = true,
"Lgui" => gui = true,
"RShift" => {
right = true;
shift = true;
}
"RCtrl" => {
right = true;
ctrl = true;
}
"RAlt" => {
right = true;
alt = true;
}
"Rgui" => {
right = true;
gui = true;
}
_ => (),
}
});
let (right, gui, alt, shift, ctrl) = parse_modifiers(internal);

if !(gui || alt || shift || ctrl) {
return quote! {
Expand Down Expand Up @@ -185,38 +160,7 @@ fn parse_key(key: String) -> TokenStream2 {
}
let layer = keys[0].parse::<u8>().unwrap();

// Get modifier combination, in types of mod1 | mod2 | ...
let mut right = false;
let mut gui = false;
let mut alt = false;
let mut shift = false;
let mut ctrl = false;
keys[1].split_terminator("|").for_each(|w| {
let w = w.trim();
match w {
"LShift" => shift = true,
"LCtrl" => ctrl = true,
"LAlt" => alt = true,
"Lgui" => gui = true,
"RShift" => {
right = true;
shift = true;
}
"RCtrl" => {
right = true;
ctrl = true;
}
"RAlt" => {
right = true;
alt = true;
}
"Rgui" => {
right = true;
gui = true;
}
_ => (),
}
});
let (right, gui, alt, shift, ctrl) = parse_modifiers(keys[1]);

if (gui || alt || shift || ctrl) == false {
return quote! {
Expand Down Expand Up @@ -275,6 +219,60 @@ fn parse_key(key: String) -> TokenStream2 {
::rmk::df!(#layer)
}
}
"MTH" => {
if let Some(internal) = key.trim_start_matches("MTH(").strip_suffix(")") {
let keys: Vec<&str> = internal
.split_terminator(",")
.map(|w| w.trim())
.filter(|w| w.len() > 0)
.collect();
if keys.len() != 2 {
return quote! {
compile_error!("keyboard.toml: MTH(key, modifier) invalid, please check the documentation: https://haobogu.github.io/rmk/keyboard_configuration.html");
};
}
let ident = format_ident!("{}", keys[0].to_string());
let (right, gui, alt, shift, ctrl) = parse_modifiers(keys[1]);

if (gui || alt || shift || ctrl) == false {
return quote! {
compile_error!("keyboard.toml: modifier in MTH(key, modifier) is not valid! Please check the documentation: https://haobogu.github.io/rmk/keyboard_configuration.html");
};
}
quote! {
::rmk::mth!(#ident, ::rmk::keycode::ModifierCombination::new_from(#right, #gui, #alt, #shift, #ctrl))
}
} else {
return quote! {
compile_error!("keyboard.toml: MTH(key, modifier) invalid, please check the documentation: https://haobogu.github.io/rmk/keyboard_configuration.html");
};
}
}
"TH(" => {
if let Some(internal) = key.trim_start_matches("TH(").strip_suffix(")") {
let keys: Vec<&str> = internal
.split_terminator(",")
.map(|w| w.trim())
.filter(|w| w.len() > 0)
.collect();
if keys.len() != 2 {
return quote! {
compile_error!("keyboard.toml: TH(key_tap, key_hold) invalid, please check the documentation: https://haobogu.github.io/rmk/keyboard_configuration.html");
};
}
let ident1 = format_ident!("{}", keys[0].to_string());
let ident2 = format_ident!("{}", keys[1].to_string());

quote! {
::rmk::th!(#ident1, #ident2)
}
} else {
return quote! {
compile_error!("keyboard.toml: TH(key_tap, key_hold) invalid, please check the documentation: https://haobogu.github.io/rmk/keyboard_configuration.html");
};
}

}
_ => {
let ident = format_ident!("{}", key);
quote! {::rmk::k!(#ident) }
Expand Down
22 changes: 22 additions & 0 deletions rmk/src/layout_macro.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,28 @@ macro_rules! lt {
};
}

/// Create a modifier-tap-hold action
#[macro_export]
macro_rules! mth {
($k: ident, $m: expr) => {
$crate::action::KeyAction::ModifierTapHold(
$crate::action::Action::Key($crate::keycode::KeyCode::$k),
$m,
)
};
}

/// Create a tap-hold action
#[macro_export]
macro_rules! th {
($t: ident, $h: ident) => {
$crate::action::KeyAction::TapHold(
$crate::action::Action::Key($crate::keycode::KeyCode::$t),
$crate::action::Action::Key($crate::keycode::KeyCode::$h),
)
};
}

/// Create an oneshot layer key in keymap
#[macro_export]
macro_rules! osl {
Expand Down
Loading