Pi66

pind
Simple Rust hotkey daemon.
git clone https://git.pi66.xyz/pind

← back to log

add: support for new keys.

author: pi66
date: 2025-09-04 21:18
hash: c61ab33b6128c8ad2d159139520096523d3f156a

Diffstat:

M

src/main.rs
197 +++++++++++++++---------------
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
diff --git a/src/main.rs b/src/main.rs
index 5471787..bef3fed 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,36 +1,10 @@
-// pind - A simple keyboard shortcut runner in Rust
-// 
-// This program reads key events from all connected keyboards and executes
-// configured shell commands when a matching key combination is pressed.
-//
-// Features:
-// - Supports modifier keys (Ctrl, Shift, Alt, Meta) and common keys.
-// - Loads key bindings from a configuration file (/etc/pind/pindrc).
-// - Runs commands as a specified user using `runuser`.
-// - Detects multiple keyboards and avoids duplicates.
-// - Non-blocking key polling with a configurable delay (25ms by default).
-//
-// Usage:
-//   sudo ./pind [optional:user]   # user argument specifies which user to run commands as
-//
-// Config file format (/etc/pind/pindrc):
-//   # Lines starting with '#' are comments
-//   <key_combination>:<command>
-//   Example:
-//     C+E:echo "Ctrl+Enter pressed"
-//
-// Dependencies:
-// - evdev crate for reading keyboard input
-// - std for filesystem, environment, threading, and process handling
-
 use evdev::{AttributeSet, Device, KeyCode, enumerate, EventType, uinput::VirtualDevice};
 use std::{env::{var,args},fs::{read_to_string,canonicalize}, path::PathBuf, process::{exit,Command, Stdio}, sync::{Arc, OnceLock}, thread};

 const DELAY  :u64  =  25; // 25ms
-const CONFIG :&str =  "/etc/pind/pindrc"; // You can use ~/ for user path
-                                          // like ~/.config/pind/pindrc
-static BINDINGS: OnceLock<Arc<Vec<(AttributeSet<KeyCode>, String)>>> = OnceLock::new();
+const CONFIG :&str =  "~/.config/pind/pindrc";

+static BINDINGS: OnceLock<Arc<Vec<(AttributeSet<KeyCode>, String)>>> = OnceLock::new();

 fn get_bindings() -> Arc<Vec<(AttributeSet<KeyCode>, String)>>
 {
@@ -46,7 +20,6 @@ fn error(title: &str, message: &str) -> !
 fn run(command: &str, user: &str)
 {
     let shell = var("SHELL").unwrap_or_else(|_| "sh".into());
-    
     Command::new("runuser")
         .args(["-u", user, "--", &shell, "-c", command])
         .stdin(Stdio::null())
@@ -60,67 +33,101 @@ fn key_to_keycode(input: &str) -> AttributeSet<KeyCode>
 {
     let mut attribute_set = AttributeSet::new();

-    for ch in input.chars() {
-        match ch {
-            'M' => attribute_set.insert(KeyCode::KEY_LEFTMETA),
-            'C' => attribute_set.insert(KeyCode::KEY_LEFTCTRL),
-            'S' => attribute_set.insert(KeyCode::KEY_LEFTSHIFT),
-            'A' => attribute_set.insert(KeyCode::KEY_LEFTALT),
-            'K' => attribute_set.insert(KeyCode::KEY_CAPSLOCK),
-            '/' => attribute_set.insert(KeyCode::KEY_SLASH),
-            '\\'=> attribute_set.insert(KeyCode::KEY_BACKSLASH),
-            'T' => attribute_set.insert(KeyCode::KEY_TAB),
-            'E' => attribute_set.insert(KeyCode::KEY_ENTER),
-            '0' => attribute_set.insert(KeyCode::KEY_0),
-            '1' => attribute_set.insert(KeyCode::KEY_1),
-            '2' => attribute_set.insert(KeyCode::KEY_2),
-            '3' => attribute_set.insert(KeyCode::KEY_3),
-            '4' => attribute_set.insert(KeyCode::KEY_4),
-            '5' => attribute_set.insert(KeyCode::KEY_5),
-            '6' => attribute_set.insert(KeyCode::KEY_6),
-            '7' => attribute_set.insert(KeyCode::KEY_7),
-            '8' => attribute_set.insert(KeyCode::KEY_8),
-            '9' => attribute_set.insert(KeyCode::KEY_9),
-            'a' => attribute_set.insert(KeyCode::KEY_A),
-            'b' => attribute_set.insert(KeyCode::KEY_B),
-            'c' => attribute_set.insert(KeyCode::KEY_C),
-            'd' => attribute_set.insert(KeyCode::KEY_D),
-            'e' => attribute_set.insert(KeyCode::KEY_E),
-            'f' => attribute_set.insert(KeyCode::KEY_F),
-            'g' => attribute_set.insert(KeyCode::KEY_G),
-            'h' => attribute_set.insert(KeyCode::KEY_H),
-            'i' => attribute_set.insert(KeyCode::KEY_I),
-            'j' => attribute_set.insert(KeyCode::KEY_J),
-            'k' => attribute_set.insert(KeyCode::KEY_K),
-            'l' => attribute_set.insert(KeyCode::KEY_L),
-            'm' => attribute_set.insert(KeyCode::KEY_M),
-            'n' => attribute_set.insert(KeyCode::KEY_N),
-            'o' => attribute_set.insert(KeyCode::KEY_O),
-            'p' => attribute_set.insert(KeyCode::KEY_P),
-            'q' => attribute_set.insert(KeyCode::KEY_Q),
-            'r' => attribute_set.insert(KeyCode::KEY_R),
-            's' => attribute_set.insert(KeyCode::KEY_S),
-            't' => attribute_set.insert(KeyCode::KEY_T),
-            'u' => attribute_set.insert(KeyCode::KEY_U),
-            'v' => attribute_set.insert(KeyCode::KEY_V),
-            'w' => attribute_set.insert(KeyCode::KEY_W),
-            'x' => attribute_set.insert(KeyCode::KEY_X),
-            'y' => attribute_set.insert(KeyCode::KEY_Y),
-            'z' => attribute_set.insert(KeyCode::KEY_Z),
-            '.' => attribute_set.insert(KeyCode::KEY_DOT),
-            ',' => attribute_set.insert(KeyCode::KEY_COMMA),
-            ';' => attribute_set.insert(KeyCode::KEY_SEMICOLON),
-            '\''=> attribute_set.insert(KeyCode::KEY_APOSTROPHE),
-            '[' => attribute_set.insert(KeyCode::KEY_LEFTBRACE),
-            ']' => attribute_set.insert(KeyCode::KEY_RIGHTBRACE),
-            '-' => attribute_set.insert(KeyCode::KEY_MINUS),
-            '=' => attribute_set.insert(KeyCode::KEY_EQUAL),
-            '`' => attribute_set.insert(KeyCode::KEY_GRAVE),
-            'U' => attribute_set.insert(KeyCode::KEY_UP),
-            'D' => attribute_set.insert(KeyCode::KEY_DOWN),
-            'L' => attribute_set.insert(KeyCode::KEY_LEFT),
-            'R' => attribute_set.insert(KeyCode::KEY_RIGHT),
-            _ => continue,
+    for key in input.replace(' ',"").split('+')  {
+        match key.to_lowercase().as_str(){
+            "meta_left"  | "meta"  => attribute_set.insert(KeyCode::KEY_LEFTMETA),
+            "ctrl_left"  | "ctrl"  => attribute_set.insert(KeyCode::KEY_LEFTCTRL),
+            "shift_left" | "shift" => attribute_set.insert(KeyCode::KEY_LEFTSHIFT),
+            "alt_left"   | "alt"   => attribute_set.insert(KeyCode::KEY_LEFTALT),
+            "back_slash" | "\\"    => attribute_set.insert(KeyCode::KEY_BACKSLASH),
+            "slash"      | "/"     => attribute_set.insert(KeyCode::KEY_SLASH),
+            "enter"     | "return" => attribute_set.insert(KeyCode::KEY_ENTER),
+            "ctrl_right"  => attribute_set.insert(KeyCode::KEY_RIGHTCTRL),
+            "shift_right" => attribute_set.insert(KeyCode::KEY_RIGHTSHIFT),
+            "meta_right"  => attribute_set.insert(KeyCode::KEY_RIGHTMETA),
+            "alt_right"   => attribute_set.insert(KeyCode::KEY_RIGHTALT),
+            "capslock"    => attribute_set.insert(KeyCode::KEY_CAPSLOCK),
+            "tab" => attribute_set.insert(KeyCode::KEY_TAB),
+            "0" => attribute_set.insert(KeyCode::KEY_0),
+            "1" => attribute_set.insert(KeyCode::KEY_1),
+            "2" => attribute_set.insert(KeyCode::KEY_2),
+            "3" => attribute_set.insert(KeyCode::KEY_3),
+            "4" => attribute_set.insert(KeyCode::KEY_4),
+            "5" => attribute_set.insert(KeyCode::KEY_5),
+            "6" => attribute_set.insert(KeyCode::KEY_6),
+            "7" => attribute_set.insert(KeyCode::KEY_7),
+            "8" => attribute_set.insert(KeyCode::KEY_8),
+            "9" => attribute_set.insert(KeyCode::KEY_9),
+            "a" => attribute_set.insert(KeyCode::KEY_A),
+            "b" => attribute_set.insert(KeyCode::KEY_B),
+            "c" => attribute_set.insert(KeyCode::KEY_C),
+            "d" => attribute_set.insert(KeyCode::KEY_D),
+            "e" => attribute_set.insert(KeyCode::KEY_E),
+            "f" => attribute_set.insert(KeyCode::KEY_F),
+            "g" => attribute_set.insert(KeyCode::KEY_G),
+            "h" => attribute_set.insert(KeyCode::KEY_H),
+            "i" => attribute_set.insert(KeyCode::KEY_I),
+            "j" => attribute_set.insert(KeyCode::KEY_J),
+            "k" => attribute_set.insert(KeyCode::KEY_K),
+            "l" => attribute_set.insert(KeyCode::KEY_L),
+            "m" => attribute_set.insert(KeyCode::KEY_M),
+            "n" => attribute_set.insert(KeyCode::KEY_N),
+            "o" => attribute_set.insert(KeyCode::KEY_O),
+            "p" => attribute_set.insert(KeyCode::KEY_P),
+            "q" => attribute_set.insert(KeyCode::KEY_Q),
+            "r" => attribute_set.insert(KeyCode::KEY_R),
+            "s" => attribute_set.insert(KeyCode::KEY_S),
+            "t" => attribute_set.insert(KeyCode::KEY_T),
+            "u" => attribute_set.insert(KeyCode::KEY_U),
+            "v" => attribute_set.insert(KeyCode::KEY_V),
+            "w" => attribute_set.insert(KeyCode::KEY_W),
+            "x" => attribute_set.insert(KeyCode::KEY_X),
+            "y" => attribute_set.insert(KeyCode::KEY_Y),
+            "z" => attribute_set.insert(KeyCode::KEY_Z),
+            "up"    => attribute_set.insert(KeyCode::KEY_UP),
+            "down"  => attribute_set.insert(KeyCode::KEY_DOWN),
+            "right" => attribute_set.insert(KeyCode::KEY_RIGHT),
+            "left"  => attribute_set.insert(KeyCode::KEY_LEFT),
+            "dot" | "." => attribute_set.insert(KeyCode::KEY_DOT),
+            "comma" | "," => attribute_set.insert(KeyCode::KEY_COMMA),
+            "semicolon"  | ";"  => attribute_set.insert(KeyCode::KEY_SEMICOLON),
+            "apostrophe" | "\"" => attribute_set.insert(KeyCode::KEY_APOSTROPHE),
+            "leftbrace"  | "["  => attribute_set.insert(KeyCode::KEY_LEFTBRACE),
+            "rightbrace" | "]"  => attribute_set.insert(KeyCode::KEY_RIGHTBRACE),
+            "numlock"     => attribute_set.insert(KeyCode::KEY_NUMLOCK),
+            "scroll_lock"  => attribute_set.insert(KeyCode::KEY_SCROLLLOCK),
+            "minus" | "-" => attribute_set.insert(KeyCode::KEY_MINUS),
+            "equal" | "=" => attribute_set.insert(KeyCode::KEY_EQUAL),
+            "plus" => attribute_set.insert(KeyCode::KEY_KPPLUS),
+            "grave" | "`" => attribute_set.insert(KeyCode::KEY_GRAVE),
+            "space" => attribute_set.insert(KeyCode::KEY_SPACE),
+            "esc" => attribute_set.insert(KeyCode::KEY_ESC),
+            "f1"  => attribute_set.insert(KeyCode::KEY_F1),
+            "f2"  => attribute_set.insert(KeyCode::KEY_F2),
+            "f3"  => attribute_set.insert(KeyCode::KEY_F3),
+            "f4"  => attribute_set.insert(KeyCode::KEY_F4),
+            "f5"  => attribute_set.insert(KeyCode::KEY_F5),
+            "f6"  => attribute_set.insert(KeyCode::KEY_F6),
+            "f7"  => attribute_set.insert(KeyCode::KEY_F7),
+            "f8"  => attribute_set.insert(KeyCode::KEY_F8),
+            "f9"  => attribute_set.insert(KeyCode::KEY_F9),
+            "f10" => attribute_set.insert(KeyCode::KEY_F10),
+            "f11" => attribute_set.insert(KeyCode::KEY_F11),
+            "f12" => attribute_set.insert(KeyCode::KEY_F12),
+            "kp0"     | "keypad_0" => attribute_set.insert(KeyCode::KEY_KP0),
+            "kp1"     | "keypad_1" => attribute_set.insert(KeyCode::KEY_KP1),
+            "kp2"     | "keypad_2" => attribute_set.insert(KeyCode::KEY_KP2),
+            "kp3"     | "keypad_3" => attribute_set.insert(KeyCode::KEY_KP3),
+            "kp4"     | "keypad_4" => attribute_set.insert(KeyCode::KEY_KP4),
+            "kp5"     | "keypad_5" => attribute_set.insert(KeyCode::KEY_KP5),
+            "kp6"     | "keypad_6" => attribute_set.insert(KeyCode::KEY_KP6),
+            "kp7"     | "keypad_7" => attribute_set.insert(KeyCode::KEY_KP7),
+            "kp8"     | "keypad_8" => attribute_set.insert(KeyCode::KEY_KP8),
+            "kp9"     | "keypad_9" => attribute_set.insert(KeyCode::KEY_KP9),
+            "kpdot"   | "keypad_dot" => attribute_set.insert(KeyCode::KEY_KPDOT),
+            "kpplus"  | "keypad_plus" => attribute_set.insert(KeyCode::KEY_KPPLUS),
+            "kpminus" | "keypad_minus" => attribute_set.insert(KeyCode::KEY_KPMINUS),
+            _ => {error("Key", &format!("key {} is not exists",key))},
         }
     }
     attribute_set
@@ -144,7 +151,7 @@ fn load_config(config: &str) -> Vec<(AttributeSet<KeyCode>, String)>
     content.lines()
         .filter(|line| !line.starts_with('#') && !line.trim().is_empty())
         .filter_map(|line| {
-            let mut parts = line.splitn(2, ':');
+            let mut parts = line.splitn(2, "=>");
             let key = parts.next()?.trim();
             let cmd = parts.next()?.trim();
             Some((key_to_keycode(key), cmd.to_string()))
@@ -152,7 +159,6 @@ fn load_config(config: &str) -> Vec<(AttributeSet<KeyCode>, String)>
         .collect()
 }

-// Modify the read_keys function to properly block shortcut keys
 fn read_keys(kc: &[(AttributeSet<KeyCode>, String)], kbs: PathBuf, delay: u64, user: String)
 {
     let mut state: Vec<(u64, u64)> = vec![(0, 0); kc.len()];
@@ -244,12 +250,9 @@ fn read_keys(kc: &[(AttributeSet<KeyCode>, String)], kbs: PathBuf, delay: u64, u
                 }

                 if is_shortcut_key && active_shortcut {
-                    // This is a shortcut key and we're in an active shortcut combo
-                    // Process the shortcut but DON'T forward the key event
                     for (i, (combo, cmd)) in kc.iter().enumerate() {
                         // Check if all keys in combo are pressed
                         let pressed = combo.iter().all(|k| current_state.contains(k));
-                        
                         let (since, last) = &mut state[i];
                         if pressed {
                             if *since == 0 {
@@ -264,16 +267,12 @@ fn read_keys(kc: &[(AttributeSet<KeyCode>, String)], kbs: PathBuf, delay: u64, u
                         }
                     }
                 } else {
-                    // Forward non-shortcut keys or shortcut keys when not in active combo
                     events_to_forward.push(event);
                 }
             } else {
-                // Forward non-key events (like SYN_REPORT, etc.)
                 events_to_forward.push(event);
             }
         }
-        
-        // Forward non-shortcut events through virtual device
         if !events_to_forward.is_empty() {
             if let Err(e) = virtual_device.emit(&events_to_forward) {
                 eprintln!("Failed to emit events: {}", e);