logitech-cve 1.5.4

A Rust library for interacting with Logitech virtual driver.
Documentation
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
use crate::device::Device;
use core::time::Duration;
use std::thread;

#[repr(u8)]
#[derive(Copy, Clone)]
/// Represents keyboard keys with their corresponding scan codes.
///
/// This enum maps keyboard keys to their usage codes, which are used
/// for sending keyboard input to device. Each variant corresponds to a specific
/// physical key on a standard keyboard layout.
pub enum Key {
    /// The 'A' key
    A = 0x4,
    /// The 'B' key
    B = 0x5,
    /// The 'C' key
    C = 0x6,
    /// The 'D' key
    D = 0x7,
    /// The 'E' key
    E = 0x8,
    /// The 'F' key
    F = 0x9,
    /// The 'G' key
    G = 0xA,
    /// The 'H' key
    H = 0xB,
    /// The 'I' key
    I = 0xC,
    /// The 'J' key
    J = 0xD,
    /// The 'K' key
    K = 0xE,
    /// The 'L' key
    L = 0xF,
    /// The 'M' key
    M = 0x10,
    /// The 'N' key
    N = 0x11,
    /// The 'O' key
    O = 0x12,
    /// The 'P' key
    P = 0x13,
    /// The 'Q' key
    Q = 0x14,
    /// The 'R' key
    R = 0x15,
    /// The 'S' key
    S = 0x16,
    /// The 'T' key
    T = 0x17,
    /// The 'U' key
    U = 0x18,
    /// The 'V' key
    V = 0x19,
    /// The 'W' key
    W = 0x1A,
    /// The 'X' key
    X = 0x1B,
    /// The 'Y' key
    Y = 0x1C,
    /// The 'Z' key
    Z = 0x1D,
    /// The '1' number key
    N1 = 0x1E,
    /// The '2' number key
    N2 = 0x1F,
    /// The '3' number key
    N3 = 0x20,
    /// The '4' number key
    N4 = 0x21,
    /// The '5' number key
    N5 = 0x22,
    /// The '6' number key
    N6 = 0x23,
    /// The '7' number key
    N7 = 0x24,
    /// The '8' number key
    N8 = 0x25,
    /// The '9' number key
    N9 = 0x26,
    /// The '0' number key
    N0 = 0x27,
    /// The Enter/Return key
    Enter = 0x28,
    /// The Escape key
    Esc = 0x29,
    /// The Backspace key
    BackSpace = 0x2A,
    /// The Tab key
    Tab = 0x2B,
    /// The Space bar
    Space = 0x2C,
    /// The minus/hyphen key (-)
    Minus = 0x2D,
    /// The equals key (=)
    Equal = 0x2E,
    /// The left square bracket key ([)
    SquareBracketLeft = 0x2F,
    /// The right square bracket key (])
    SquareBracketRight = 0x30,
    /// The backslash key (\)
    BackSlash = 0x31,
    /// Alternative backslash key
    BackSlash_ = 0x32,
    /// The semicolon/colon key (;)
    Column = 0x33,
    /// The single quote/apostrophe key (')
    Quote = 0x34,
    /// The backtick/grave accent key (\`)
    BackTick = 0x35,
    /// The comma key (,)
    Comma = 0x36,
    /// The period/dot key (.)
    Period = 0x37,
    /// The forward slash key (/)
    Slash = 0x38,
    /// The Caps Lock key
    Cap = 0x39,
    /// Function key F1
    F1 = 0x3A,
    /// Function key F2
    F2 = 0x3B,
    /// Function key F3
    F3 = 0x3C,
    /// Function key F4
    F4 = 0x3D,
    /// Function key F5
    F5 = 0x3E,
    /// Function key F6
    F6 = 0x3F,
    /// Function key F7
    F7 = 0x40,
    /// Function key F8
    F8 = 0x41,
    /// Function key F9
    F9 = 0x42,
    /// Function key F10
    F10 = 0x43,
    /// Function key F11
    F11 = 0x44,
    /// Function key F12
    F12 = 0x45,
    /// Print Screen/Snapshot key
    Snapshot = 0x46,
    /// Scroll Lock key
    ScrollLock = 0x47,
    /// Pause/Break key
    Pause = 0x48,
    /// Insert key
    Insert = 0x49,
    /// Home key
    Home = 0x4A,
    /// Page Up key
    PageUp = 0x4B,
    /// Delete key
    Del = 0x4C,
    /// End key
    End = 0x4D,
    /// Page Down key
    PageDown = 0x4E,
    /// Right arrow key
    Right = 0x4F,
    /// Left arrow key
    Left = 0x50,
    /// Down arrow key
    Down = 0x51,
    /// Up arrow key
    Up = 0x52,
    /// Num Lock key
    Numlock = 0x53,
    /// Numeric keypad division key (/)
    NumpadDiv = 0x54,
    /// Numeric keypad multiplication key (*)
    NumpadMul = 0x55,
    /// Numeric keypad minus key (-)
    NumpadMinus = 0x56,
    /// Numeric keypad plus key (+)
    NumpadPlus = 0x57,
    /// Numeric keypad Enter key
    NumpadEnter = 0x58,
    /// Numeric keypad 1 key
    Numpad1 = 0x59,
    /// Numeric keypad 2 key
    Numpad2 = 0x5A,
    /// Numeric keypad 3 key
    Numpad3 = 0x5B,
    /// Numeric keypad 4 key
    Numpad4 = 0x5C,
    /// Numeric keypad 5 key
    Numpad5 = 0x5D,
    /// Numeric keypad 6 key
    Numpad6 = 0x5E,
    /// Numeric keypad 7 key
    Numpad7 = 0x5F,
    /// Numeric keypad 8 key
    Numpad8 = 0x60,
    /// Numeric keypad 9 key
    Numpad9 = 0x61,
    /// Numeric keypad 0 key
    Numpad0 = 0x62,
    /// Numeric keypad decimal point key (.)
    NumpadDec = 0x63,
    /// Application/Menu key
    Apps = 0x65,
    /// Function key F13
    F13 = 0x68,
    /// Function key F14
    F14 = 0x69,
    /// Function key F15
    F15 = 0x6A,
    /// Function key F16
    F16 = 0x6B,
    /// Function key F17
    F17 = 0x6C,
    /// Function key F18
    F18 = 0x6D,
    /// Function key F19
    F19 = 0x6E,
    /// Function key F20
    F20 = 0x6F,
    /// Function key F21
    F21 = 0x70,
    /// Function key F22
    F22 = 0x71,
    /// Function key F23
    F23 = 0x72,
    /// Function key F24
    F24 = 0x73,
    /// Right Windows key
    Rwin = 0x8C,
    /// Alternative F24 key
    F24_ = 0x94,
    /// Left Control key
    Lctrl = 0xE0,
    /// Left Shift key
    Lshift = 0xE1,
    /// Left Alt key
    Lalt = 0xE2,
    /// Left Windows key
    Lwin = 0xE3,
    /// Right Control key
    Rctrl = 0xE4,
    /// Right Shift key
    Rshift = 0xE5,
    /// Right Alt key
    Ralt = 0xE6,
    /// Alternative Right Windows key
    Rwin_ = 0xE7,
    /// No key pressed (release state)
    Release = 0x0,
}

impl From<Key> for u8 {
    #[inline]
    fn from(button: Key) -> Self {
        button as Self
    }
}

impl TryFrom<char> for Key {
    type Error = String;
    
    #[inline]
    fn try_from(c: char) -> Result<Self, Self::Error> {
        match c.to_ascii_uppercase() {
            'A' => Ok(Self::A),
            'B' => Ok(Self::B),
            'C' => Ok(Self::C),
            'D' => Ok(Self::D),
            'E' => Ok(Self::E),
            'F' => Ok(Self::F),
            'G' => Ok(Self::G),
            'H' => Ok(Self::H),
            'I' => Ok(Self::I),
            'J' => Ok(Self::J),
            'K' => Ok(Self::K),
            'L' => Ok(Self::L),
            'M' => Ok(Self::M),
            'N' => Ok(Self::N),
            'O' => Ok(Self::O),
            'P' => Ok(Self::P),
            'Q' => Ok(Self::Q),
            'R' => Ok(Self::R),
            'S' => Ok(Self::S),
            'T' => Ok(Self::T),
            'U' => Ok(Self::U),
            'V' => Ok(Self::V),
            'W' => Ok(Self::W),
            'X' => Ok(Self::X),
            'Y' => Ok(Self::Y),
            'Z' => Ok(Self::Z),
            '\n' => Ok(Self::Enter),
            '\t' => Ok(Self::Tab),
            ' ' => Ok(Self::Space),
            '1' | '!' => Ok(Self::N1),                 // ! for Shift + 1
            '2' | '@' => Ok(Self::N2),                 // @ for Shift + 2
            '3' | '#' => Ok(Self::N3),                 // # for Shift + 3
            '4' | '$' => Ok(Self::N4),                 // $ for Shift + 4
            '5' | '%' => Ok(Self::N5),                 // % for Shift + 5
            '6' | '^' => Ok(Self::N6),                 // ^ for Shift + 6
            '7' | '&' => Ok(Self::N7),                 // & for Shift + 7
            '8' | '*' => Ok(Self::N8),                 // * for Shift + 8
            '9' | '(' => Ok(Self::N9),                 // ( for Shift + 9
            '0' | ')' => Ok(Self::N0),                 // ) for Shift + 0
            '-' | '_' => Ok(Self::Minus),              // _ for Shift + -
            '=' => Ok(Self::Equal),                    // + for Shift + =
            '[' | '{' => Ok(Self::SquareBracketLeft),  // { for Shift + [
            ']' | '}' => Ok(Self::SquareBracketRight), // } for Shift + ]
            ';' | ':' => Ok(Self::Column),             // : for Shift + ;
            '\'' | '"' => Ok(Self::Quote),             // " for Shift + '
            '\\' | '|' => Ok(Self::BackSlash),         // | for Shift + \
            ',' | '<' => Ok(Self::Comma),              // < for Shift + ,
            '.' | '>' => Ok(Self::Period),             // > for Shift + .
            '/' | '?' => Ok(Self::Slash),              // ? for Shift + /
            '`' | '~' => Ok(Self::BackTick),           // ~ for Shift + `
            _ => Err(format!("Unsupported character: {c}")),
        }
    }
}

/// A struct for controlling a virtual keyboard.
///
/// It holds a reference to a `Device` which is used to send the keyboard commands.
pub struct Keyboard<'a> {
    /// A reference to the device used to send keyboard commands.
    device: &'a Device,
}

impl<'a> Keyboard<'a> {
    /// Creates a new [`Keyboard`].
    #[must_use]
    #[inline]
    pub const fn new(device: &'a Device) -> Self {
        Self { device }
    }

    /// Presses a single keyboard button.
    ///
    /// The button is held down until a `release()` or `multi_press()` with `Key::NONE` is called.
    ///
    /// # Arguments
    ///
    /// * `button` - The `Key` to press.
    #[inline]
    pub fn press(&self, button: Key) {
        self.device.call_keyboard(
            button,
            Key::Release,
            Key::Release,
            Key::Release,
            Key::Release,
            Key::Release,
        );
    }

    /// Releases all currently pressed keyboard buttons.
    ///
    /// This effectively sends a "no keys pressed" command to the device.
    #[inline]
    pub fn release(&self) {
        self.device.call_keyboard(
            Key::Release,
            Key::Release,
            Key::Release,
            Key::Release,
            Key::Release,
            Key::Release,
        );
    }

    /// Presses and releases a single keyboard button.
    ///
    /// The button is pressed down, held for the specified duration, then released.
    ///
    /// # Arguments
    ///
    /// * `button` - The `Key` to press and release.
    /// * `millis` - The duration in milliseconds to hold the button down before releasing it.
    #[inline]
    pub fn press_and_release(&self, button: Key, millis: u64) {
        self.device.call_keyboard(
            button,
            Key::Release,
            Key::Release,
            Key::Release,
            Key::Release,
            Key::Release,
        );
        thread::sleep(Duration::from_millis(millis));
        self.device.call_keyboard(
            Key::Release,
            Key::Release,
            Key::Release,
            Key::Release,
            Key::Release,
            Key::Release,
        );
    }

    /// Presses up to six keyboard buttons simultaneously.
    ///
    /// This can be used for pressing modifier keys and other keys at the same time.
    ///
    /// # Arguments
    ///
    /// * `button1` - The first `Key` to press.
    /// * `button2` - The second `Key` to press.
    /// * `button3` - The third `Key` to press.
    /// * `button4` - The fourth `Key` to press.
    /// * `button5` - The fifth `Key` to press.
    /// * `button6` - The sixth `Key` to press.
    #[inline]
    pub fn multi_press(&self, button1: Key, button2: Key, button3: Key, button4: Key, button5: Key, button6: Key) {
        self.device
            .call_keyboard(button1, button2, button3, button4, button5, button6);
    }

    /// Types a string by simulating individual key presses for each character.
    ///
    /// # Arguments
    ///
    /// * `string` - The string to be typed.
    /// * `millis` - The duration in milliseconds to hold the button down before releasing it.
    ///
    /// # Errors
    ///
    /// This function will return an error if a character in the input string
    /// cannot be converted into a valid `Key` enum variant.
    #[inline]
    pub fn type_string(&self, string: &str, millis: u64) -> Result<(), String> {
        for c in string.chars() {
            let key = Key::try_from(c)?;
            match c {
                'a'..='z'
                | '0'..='9'
                | '\n'
                | '\t'
                | ' '
                | '-'
                | '='
                | '['
                | ']'
                | '\\'
                | ';'
                | '\''
                | '`'
                | ','
                | '.'
                | '/' => self.press_and_release(key, millis),

                'A'..='Z'
                | '!'
                | '@'
                | '#'
                | '$'
                | '%'
                | '^'
                | '&'
                | '*'
                | '('
                | ')'
                | '_'
                | '+'
                | '{'
                | '}'
                | ':'
                | '"'
                | '|'
                | '<'
                | '>'
                | '?'
                | '~' => {
                    self.multi_press(Key::Lshift, key, Key::Release, Key::Release, Key::Release, Key::Release);
                    thread::sleep(Duration::from_millis(millis));
                    self.release();
                }
                _ => {}
            }
        }

        Ok(())
    }
}