Skip to content

Commit

Permalink
Merge pull request #112 from jackbrad1ey/with_modifier_support
Browse files Browse the repository at this point in the history
  • Loading branch information
HaoboGu authored Oct 27, 2024
2 parents 786e183 + 5399e71 commit 9fb1903
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 2 deletions.
3 changes: 2 additions & 1 deletion docs/src/keyboard_configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,11 +116,12 @@ The key string should follow several rules:

For example, if you set a keycode `"Backspace"`, it will be turned to `KeyCode::Backspace`. So you have to ensure that the keycode string is valid, or RMK wouldn't compile!

For simple keycodes with modifiers active, you can use `WM(key, modifier)` to create a keypress with modifier action. Modifiers can be chained together like `LShift | RGui` to have multiple modifiers active.
2. For no-key, use `"__"`

3. RMK supports many advanced layer operations:
1. Use `"MO(n)"` to create a layer activate action, `n` is the layer number
2. Use `"LM(n, modifier)"` to create layer activate with modifier action. The modifier can be like `LShift | RGui`
2. Use `"LM(n, modifier)"` to create layer activate with modifier action. The modifier can be chained in the same way as `WM`.
3. Use `"LT(n, key)"` to create a layer activate action or tap key(tap/hold). The `key` here is the RMK [`KeyCode`](https://docs.rs/rmk/latest/rmk/keycode/enum.KeyCode.html)
4. Use `"OSL(n)"` to create a one-shot layer action, `n` is the layer number
5. Use `"TT(n)"` to create a layer activate or tap toggle action, `n` is the layer number
Expand Down
2 changes: 1 addition & 1 deletion rmk-macro/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added

- Add default config for chips

- Implemented `keyboard.toml` parsing for the new `WM(key, modifier)` "With Modifier" macro
### Changed

- BREAKING: refactor the whole macro crate, update `keyboard.toml` fields
Expand Down
62 changes: 62 additions & 0 deletions rmk-macro/src/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,68 @@ fn parse_key(key: String) -> TokenStream2 {
};
}
match &key[0..3] {
"WM(" => {
if let Some(internal) = key.trim_start_matches("WM(").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: WM(layer, modifier) invalid, please check the documentation: https://haobogu.github.io/rmk/keyboard_configuration.html");
};
}

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;
}
_ => (),
}
});

if (gui || alt || shift || ctrl) == false {
return quote! {
compile_error!("keyboard.toml: modifier in WM(layer, modifier) is not valid! Please check the documentation: https://haobogu.github.io/rmk/keyboard_configuration.html");
};
}
quote! {
::rmk::wm!(#ident, ::rmk::keycode::ModifierCombination::new_from(#right, #gui, #alt, #shift, #ctrl))
}
} else {
return quote! {
compile_error!("keyboard.toml: WM(layer, modifier) invalid, please check the documentation: https://haobogu.github.io/rmk/keyboard_configuration.html");
};
}
}
"MO(" => {
let layer = get_layer(key, "MO(", ")");
quote! {
Expand Down
1 change: 1 addition & 0 deletions rmk/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- Add restart to ESP32
- Optimize nRF BLE power consumption, now the idle current is decreased to about 20uA
- Added new `wm` "With Modifier" macro to support basic keycodes with modifiers active

### Changed

Expand Down
8 changes: 8 additions & 0 deletions rmk/src/layout_macro.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@ macro_rules! k {
};
}

/// Create a normal key with modifier action
#[macro_export]
macro_rules! wm {
($x: ident, $m: expr) => {
$crate::action::KeyAction::WithModifier($crate::action::Action::Key($crate::keycode::KeyCode::$x), $m)
};
}

/// Create a normal action: `KeyAction`
#[macro_export]
macro_rules! a {
Expand Down

0 comments on commit 9fb1903

Please sign in to comment.