rktk_keymanager/keycode/mod.rs
1//! Key action and keycode definitions.
2//!
3//! A key is represented by a three-layer structure: `KeyAction` → `KeyCode` → `Key`.
4//! For example, a key action that sends A on Tap and Shift on Hold could be defined as follows
5//! ```
6//! # use rktk_keymanager::keycode::prelude::*;
7//! const ACTION: KeyAction = KeyAction::TapHold(KeyCode::Key(Key::A),
8//! KeyCode::Modifier(Modifier::LShft));
9//! ```
10//!
11//! This hierarchical structure allows any key to be used for any action.
12//! For example, QMK only allows Mod-Tap and Layer-Tap as TapHolds.
13//! However, it requires more bytes to represent one key.
14//!
15//! For convenience, keycodes are defined as constants with normal keyaction like this:
16//! ```
17//! # use rktk_keymanager::keycode::prelude::*;
18//! const A: KeyAction = KeyAction::Normal(KeyCode::Key(Key::A));
19//! ```
20//! You can use these constants to define keymap.
21
22use macro_rules_attribute::apply;
23
24use crate::macros::common_derive;
25
26pub mod key;
27pub mod layer;
28pub mod media;
29pub mod modifier;
30pub mod mouse;
31pub mod special;
32pub mod utils;
33
34/// Convenient prelude of keycodes for defining keymaps
35pub mod prelude {
36 pub use super::{key::*, layer::*, media::*, modifier::*, mouse::*, special::*, utils::*, *};
37}
38
39/// Represents key action.
40#[apply(common_derive)]
41#[derive(Copy)]
42pub enum KeyAction {
43 /// Inherit key definition from parent layer.
44 Inherit,
45 /// Normal key
46 Normal(KeyCode),
47 /// Normal key with secondary key. These keys are sent together.
48 Normal2(KeyCode, KeyCode),
49 /// Tap-Hold key (tap, hold)
50 ///
51 /// If key is pressed and released in [`KeyResolverConfig::tap_hold`](crate::interface::state::config::KeyResolverConfig::tap_hold), tap key is sent.
52 /// If key is pressed and held longer, hold key is sent. Also, `rktk-keymanager` emulates qmk's `HOLD_ON_OTHER_KEY_PRESS` feature.
53 /// If another key is pressed while holding this key, even before `tap_threshold`, hold key is sent.
54 TapHold(KeyCode, KeyCode),
55 /// One-shot key
56 ///
57 /// After pressing this key, the key will be sent together with the next key press.
58 OneShot(KeyCode),
59 /// Tap-dance (id)
60 ///
61 /// Execute tap-dance with specified id. TapDance can be configured in [`KeyResolverConfig`](crate::interface::state::config::KeyResolverConfig).
62 TapDance(u8),
63}
64
65impl KeyAction {
66 pub const fn const_default() -> Self {
67 Self::Inherit
68 }
69}
70
71impl Default for KeyAction {
72 fn default() -> Self {
73 Self::const_default()
74 }
75}
76
77/// Represents each key.
78#[apply(common_derive)]
79#[derive(Copy)]
80pub enum KeyCode {
81 None,
82 /// Normal key
83 Key(key::Key),
84 /// Mouse key (button)
85 Mouse(mouse::Mouse),
86 /// Modifier key
87 Modifier(modifier::Modifier),
88 /// Layer operation key
89 Layer(layer::LayerOp),
90 /// Special key
91 Special(special::Special),
92 /// Media key
93 Media(media::Media),
94 Custom1(u8),
95 Custom2(u8),
96 Custom3(u8),
97}
98
99/// Inherit key: `KeyAction::Inherit`
100pub const _____: KeyAction = KeyAction::Inherit;
101pub const ____: KeyAction = KeyAction::Inherit;
102pub const ___: KeyAction = KeyAction::Inherit;
103pub const __: KeyAction = KeyAction::Inherit;
104
105/// None key: `KeyAction::Normal(KeyCode::None)`
106pub const XXXXX: KeyAction = KeyAction::Normal(KeyCode::None);