Skip to content

Commit

Permalink
feat: include numeric key code in KeyEvent Debug (#1089)
Browse files Browse the repository at this point in the history
Add the numeric key code value after the enum literal name in debug
prints of KeyEvent.

Printing the numeric key code together with the pressed key enum value
makes it much easier to create custom deflocalkeys.

Example --debug output:
```
21:42:09.5202 [DEBUG] (3) kanata_state_machine::kanata: process recv ev KeyEvent { code: KEY_LEFTCTRL (29), value: Press }
21:42:09.6403 [DEBUG] (3) kanata_state_machine::kanata: process recv ev KeyEvent { code: KEY_I (23), value: Press }
```
  • Loading branch information
thomasa88 committed Jun 6, 2024
1 parent 75b845c commit 849e5d7
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
5 changes: 5 additions & 0 deletions docs/config.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,11 @@ Please contribute to the document if you are able!
The number used for a custom key represents the converted value for an OsCode in
base 10. This differs between Windows-hooks, Windows-interception, and Linux.

Running kanata with the `--debug` flag lets you read the correct number,
shown in parenthesis of `code` in the `KeyEvent` log lines.

It also possible to use native tools, as described below.

In Linux, `evtest` will give the correct number for the physical key you press.

In Windows using the default hook mechanism, the non-interception version of the
Expand Down
14 changes: 13 additions & 1 deletion src/oskbd/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ impl From<KeyValue> for bool {

use kanata_parser::keys::OsCode;

#[derive(Debug, Clone, Copy)]
#[derive(Clone, Copy)]
pub struct KeyEvent {
pub code: OsCode,
pub value: KeyValue,
Expand Down Expand Up @@ -129,3 +129,15 @@ impl fmt::Display for KeyEvent {
write!(f, "{}{:?}", direction, key_name)
}
}

impl fmt::Debug for KeyEvent {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_struct("KeyEvent")
.field(
"code",
&format_args!("{:?} ({})", self.code, self.code.as_u16()),
)
.field("value", &self.value)
.finish()
}
}

0 comments on commit 849e5d7

Please sign in to comment.