inputbot/linux/
inputs.rs

1use crate::public::{
2    KeybdKey::{self, *},
3    MouseButton::{self, *},
4};
5
6impl From<KeybdKey> for u64 {
7    // https://www.cl.cam.ac.uk/~mgk25/ucs/keysymdef.h
8    // https://github.com/AltF02/x11-rs/blob/master/src/keysym.rs
9    fn from(key: KeybdKey) -> u64 {
10        match key {
11            BackspaceKey => 0xFF08,
12            TabKey => 0xFF09,
13            EnterKey => 0xFF8D,
14            EscapeKey => 0xFF1B,
15            SpaceKey => 0x020,
16            PageUpKey => 0xFF55,
17            PageDownKey => 0xFF56,
18            EndKey => 0xFF56,
19            HomeKey => 0xFF50,
20            LeftKey => 0xFF51,
21            UpKey => 0xFF52,
22            RightKey => 0xFF53,
23            DownKey => 0xFF54,
24            InsertKey => 0xFF63,
25            DeleteKey => 0xFF9F,
26            Numrow0Key => 0x030,
27            Numrow1Key => 0x031,
28            Numrow2Key => 0x032,
29            Numrow3Key => 0x033,
30            Numrow4Key => 0x034,
31            Numrow5Key => 0x035,
32            Numrow6Key => 0x036,
33            Numrow7Key => 0x037,
34            Numrow8Key => 0x038,
35            Numrow9Key => 0x039,
36            AKey => 0x041,
37            BKey => 0x042,
38            CKey => 0x043,
39            DKey => 0x044,
40            EKey => 0x045,
41            FKey => 0x046,
42            GKey => 0x047,
43            HKey => 0x048,
44            IKey => 0x049,
45            JKey => 0x04A,
46            KKey => 0x04B,
47            LKey => 0x04C,
48            MKey => 0x04D,
49            NKey => 0x04E,
50            OKey => 0x04F,
51            PKey => 0x050,
52            QKey => 0x051,
53            RKey => 0x052,
54            SKey => 0x053,
55            TKey => 0x054,
56            UKey => 0x055,
57            VKey => 0x056,
58            WKey => 0x057,
59            XKey => 0x058,
60            YKey => 0x059,
61            ZKey => 0x05A,
62            LSuper => 0xFFEB,
63            RSuper => 0xFFEC,
64            Numpad0Key => 0xFFB0,
65            Numpad1Key => 0xFFB1,
66            Numpad2Key => 0xFFB2,
67            Numpad3Key => 0xFFB3,
68            Numpad4Key => 0xFFB4,
69            Numpad5Key => 0xFFB5,
70            Numpad6Key => 0xFFB6,
71            Numpad7Key => 0xFFB7,
72            Numpad8Key => 0xFFB8,
73            Numpad9Key => 0xFFB9,
74            F1Key => 0xFFBE,
75            F2Key => 0xFFBF,
76            F3Key => 0xFFC0,
77            F4Key => 0xFFC1,
78            F5Key => 0xFFC2,
79            F6Key => 0xFFC3,
80            F7Key => 0xFFC4,
81            F8Key => 0xFFC5,
82            F9Key => 0xFFC6,
83            F10Key => 0xFFC7,
84            F11Key => 0xFFC8,
85            F12Key => 0xFFC9,
86            F13Key => 0xFFCA,
87            F14Key => 0xFFCB,
88            F15Key => 0xFFCC,
89            F16Key => 0xFFCD,
90            F17Key => 0xFFCE,
91            F18Key => 0xFFCF,
92            F19Key => 0xFFD0,
93            F20Key => 0xFFD1,
94            F21Key => 0xFFD2,
95            F22Key => 0xFFD3,
96            F23Key => 0xFFD4,
97            F24Key => 0xFFD5,
98            NumLockKey => 0xFF7F,
99            ScrollLockKey => 0xFF14,
100            CapsLockKey => 0xFFE5,
101            LShiftKey => 0xFFE1,
102            RShiftKey => 0xFFE2,
103            LControlKey => 0xFFE3,
104            RControlKey => 0xFFE4,
105            LAltKey => 0xFFE9,
106            RAltKey => 0xFFEA,
107            BrowserBackKey => 0x1008FF26,
108            BrowserForwardKey => 0x1008FF27,
109            BrowserRefreshKey => 0x1008FF29,
110            VolumeMuteKey => 0x1008FF12,
111            VolumeDownKey => 0x1008FF11,
112            VolumeUpKey => 0x1008FF13,
113            MediaNextTrackKey => 0x1008FF17,
114            MediaPrevTrackKey => 0x1008FF16,
115            MediaStopKey => 0x1008FF15,
116            MediaPlayPauseKey => 0x1008FF14,
117            BackquoteKey => 0x29,
118            SlashKey => 0x35,
119            BackslashKey => 0x2B,
120            CommaKey => 0x33,
121            PeriodKey => 0x34,
122            MinusKey => 0x0C,
123            QuoteKey => 0x28,
124            SemicolonKey => 0x27,
125            LBracketKey => 0x1A,
126            RBracketKey => 0x1B,
127            EqualKey => 0x0D,
128            OtherKey(keycode) => keycode,
129        }
130    }
131}
132
133impl From<u64> for KeybdKey {
134    // https://www.cl.cam.ac.uk/~mgk25/ucs/keysymdef.h
135    // https://github.com/AltF02/x11-rs/blob/master/src/keysym.rs
136    fn from(code: u64) -> KeybdKey {
137        match code {
138            0xFF08 => BackspaceKey,
139            0xFF09 => TabKey,
140            0xFF8D => EnterKey,
141            0xFF1B => EscapeKey,
142            0x020 => SpaceKey,
143            0xFF55 => PageUpKey,
144            0xFF56 => PageDownKey,
145            0xFF56 => EndKey,
146            0xFF50 => HomeKey,
147            0xFF51 => LeftKey,
148            0xFF52 => UpKey,
149            0xFF53 => RightKey,
150            0xFF54 => DownKey,
151            0xFF63 => InsertKey,
152            0xFF9F => DeleteKey,
153            0x030 => Numrow0Key,
154            0x031 => Numrow1Key,
155            0x032 => Numrow2Key,
156            0x033 => Numrow3Key,
157            0x034 => Numrow4Key,
158            0x035 => Numrow5Key,
159            0x036 => Numrow6Key,
160            0x037 => Numrow7Key,
161            0x038 => Numrow8Key,
162            0x039 => Numrow9Key,
163            0x041 => AKey,
164            0x042 => BKey,
165            0x043 => CKey,
166            0x044 => DKey,
167            0x045 => EKey,
168            0x046 => FKey,
169            0x047 => GKey,
170            0x048 => HKey,
171            0x049 => IKey,
172            0x04A => JKey,
173            0x04B => KKey,
174            0x04C => LKey,
175            0x04D => MKey,
176            0x04E => NKey,
177            0x04F => OKey,
178            0x050 => PKey,
179            0x051 => QKey,
180            0x052 => RKey,
181            0x053 => SKey,
182            0x054 => TKey,
183            0x055 => UKey,
184            0x056 => VKey,
185            0x057 => WKey,
186            0x058 => XKey,
187            0x059 => YKey,
188            0x05A => ZKey,
189            0xFFEB => LSuper,
190            0xFFEC => RSuper,
191            0xFFB0 => Numpad0Key,
192            0xFFB1 => Numpad1Key,
193            0xFFB2 => Numpad2Key,
194            0xFFB3 => Numpad3Key,
195            0xFFB4 => Numpad4Key,
196            0xFFB5 => Numpad5Key,
197            0xFFB6 => Numpad6Key,
198            0xFFB7 => Numpad7Key,
199            0xFFB8 => Numpad8Key,
200            0xFFB9 => Numpad9Key,
201            0xFFBE => F1Key,
202            0xFFBF => F2Key,
203            0xFFC0 => F3Key,
204            0xFFC1 => F4Key,
205            0xFFC2 => F5Key,
206            0xFFC3 => F6Key,
207            0xFFC4 => F7Key,
208            0xFFC5 => F8Key,
209            0xFFC6 => F9Key,
210            0xFFC7 => F10Key,
211            0xFFC8 => F11Key,
212            0xFFC9 => F12Key,
213            0xFFCA => F13Key,
214            0xFFCB => F14Key,
215            0xFFCC => F15Key,
216            0xFFCD => F16Key,
217            0xFFCE => F17Key,
218            0xFFCF => F18Key,
219            0xFFD0 => F19Key,
220            0xFFD1 => F20Key,
221            0xFFD2 => F21Key,
222            0xFFD3 => F22Key,
223            0xFFD4 => F23Key,
224            0xFFD5 => F24Key,
225            0xFF7F => NumLockKey,
226            0xFF14 => ScrollLockKey,
227            0xFFE5 => CapsLockKey,
228            0xFFE1 => LShiftKey,
229            0xFFE2 => RShiftKey,
230            0xFFE3 => LControlKey,
231            0xFFE4 => RControlKey,
232            0xFFE9 => LAltKey,
233            0xFFEA => RAltKey,
234            0x1008FF26 => BrowserBackKey,
235            0x1008FF27 => BrowserForwardKey,
236            0x1008FF29 => BrowserRefreshKey,
237            0x1008FF12 => VolumeMuteKey,
238            0x1008FF11 => VolumeDownKey,
239            0x1008FF13 => VolumeUpKey,
240            0x1008FF17 => MediaNextTrackKey,
241            0x1008FF16 => MediaPrevTrackKey,
242            0x1008FF15 => MediaStopKey,
243            0x1008FF14 => MediaPlayPauseKey,
244            0x29 => BackquoteKey,
245            0x35 => SlashKey,
246            0x2B => BackslashKey,
247            0x33 => CommaKey,
248            0x34 => PeriodKey,
249            0x0C => MinusKey,
250            0x28 => QuoteKey,
251            0x27 => SemicolonKey,
252            0x1A => LBracketKey,
253            0x1B => RBracketKey,
254            0x0D => EqualKey,
255            _ => OtherKey(code),
256        }
257    }
258}
259
260impl From<u32> for MouseButton {
261    fn from(keycode: u32) -> MouseButton {
262        match keycode {
263            1 => LeftButton,
264            2 => MiddleButton,
265            3 => RightButton,
266            4 => X1Button,
267            5 => X2Button,
268            _ => OtherButton(keycode),
269        }
270    }
271}
272
273impl From<MouseButton> for u32 {
274    fn from(button: MouseButton) -> u32 {
275        match button {
276            LeftButton => 1,
277            MiddleButton => 2,
278            RightButton => 3,
279            X1Button => 4,
280            X2Button => 5,
281            OtherButton(keycode) => keycode,
282        }
283    }
284}
285
286// https://www.win.tue.nl/~aeb/linux/kbd/scancodes-1.html
287pub fn scan_code_to_key(scan_code: u32) -> Option<KeybdKey> {
288    match scan_code {
289        0x0E => Some(BackspaceKey),
290        0x0F => Some(TabKey),
291        0x1C => Some(EnterKey),
292        0x01 => Some(EscapeKey),
293        0x39 => Some(SpaceKey),
294        0x47 => Some(HomeKey),
295        0x4B => Some(LeftKey),
296        0x48 => Some(UpKey),
297        0x4D => Some(RightKey),
298        0x50 => Some(DownKey),
299        0x52 => Some(InsertKey),
300        0x53 => Some(DeleteKey),
301        0x0B => Some(Numrow0Key),
302        0x02 => Some(Numrow1Key),
303        0x03 => Some(Numrow2Key),
304        0x04 => Some(Numrow3Key),
305        0x05 => Some(Numrow4Key),
306        0x06 => Some(Numrow5Key),
307        0x07 => Some(Numrow6Key),
308        0x08 => Some(Numrow7Key),
309        0x09 => Some(Numrow8Key),
310        0x0A => Some(Numrow9Key),
311        0x1E => Some(AKey),
312        0x30 => Some(BKey),
313        0x2E => Some(CKey),
314        0x20 => Some(DKey),
315        0x12 => Some(EKey),
316        0x21 => Some(FKey),
317        0x22 => Some(GKey),
318        0x23 => Some(HKey),
319        0x17 => Some(IKey),
320        0x24 => Some(JKey),
321        0x25 => Some(KKey),
322        0x26 => Some(LKey),
323        0x32 => Some(MKey),
324        0x31 => Some(NKey),
325        0x18 => Some(OKey),
326        0x19 => Some(PKey),
327        0x10 => Some(QKey),
328        0x13 => Some(RKey),
329        0x1F => Some(SKey),
330        0x14 => Some(TKey),
331        0x16 => Some(UKey),
332        0x2F => Some(VKey),
333        0x11 => Some(WKey),
334        0x2D => Some(XKey),
335        0x15 => Some(YKey),
336        0x2C => Some(ZKey),
337        0x52 => Some(Numpad0Key),
338        0x4F => Some(Numpad1Key),
339        0x50 => Some(Numpad2Key),
340        0x51 => Some(Numpad3Key),
341        0x4B => Some(Numpad4Key),
342        0x4C => Some(Numpad5Key),
343        0x4D => Some(Numpad6Key),
344        0x47 => Some(Numpad7Key),
345        0x48 => Some(Numpad8Key),
346        0x49 => Some(Numpad9Key),
347        0x3B => Some(F1Key),
348        0x3C => Some(F2Key),
349        0x3D => Some(F3Key),
350        0x3E => Some(F4Key),
351        0x3F => Some(F5Key),
352        0x40 => Some(F6Key),
353        0x41 => Some(F7Key),
354        0x42 => Some(F8Key),
355        0x43 => Some(F9Key),
356        0x44 => Some(F10Key),
357        0x45 => Some(NumLockKey),
358        0x46 => Some(ScrollLockKey),
359        0x3A => Some(CapsLockKey),
360        0x2A => Some(LShiftKey),
361        0x36 => Some(RShiftKey),
362        0x1D => Some(LControlKey),
363        0x29 => Some(BackquoteKey),
364        0x35 => Some(SlashKey),
365        0x2B => Some(BackslashKey),
366        0x33 => Some(CommaKey),
367        0x34 => Some(PeriodKey),
368        0x0C => Some(MinusKey),
369        0x28 => Some(QuoteKey),
370        0x27 => Some(SemicolonKey),
371        0x1A => Some(LBracketKey),
372        0x1B => Some(RBracketKey),
373        0x0D => Some(EqualKey),
374        _ => None,
375    }
376}
377
378// https://www.win.tue.nl/~aeb/linux/kbd/scancodes-1.html
379pub fn key_to_scan_code(key: KeybdKey) -> i32 {
380    match key {
381        BackspaceKey => 0x0E,
382        TabKey => 0x0F,
383        EnterKey => 0x1C,
384        EscapeKey => 0x01,
385        SpaceKey => 0x39,
386        HomeKey => 0x47,
387        LeftKey => 0x4b,
388        UpKey => 0x48,
389        RightKey => 0x4D,
390        DownKey => 0x50,
391        InsertKey => 0x52,
392        DeleteKey => 0x53,
393        Numrow0Key => 0x0B,
394        Numrow1Key => 0x02,
395        Numrow2Key => 0x03,
396        Numrow3Key => 0x04,
397        Numrow4Key => 0x05,
398        Numrow5Key => 0x06,
399        Numrow6Key => 0x07,
400        Numrow7Key => 0x08,
401        Numrow8Key => 0x09,
402        Numrow9Key => 0x0A,
403        AKey => 0x1E,
404        BKey => 0x30,
405        CKey => 0x2E,
406        DKey => 0x20,
407        EKey => 0x12,
408        FKey => 0x21,
409        GKey => 0x22,
410        HKey => 0x23,
411        IKey => 0x17,
412        JKey => 0x24,
413        KKey => 0x25,
414        LKey => 0x26,
415        MKey => 0x32,
416        NKey => 0x31,
417        OKey => 0x18,
418        PKey => 0x19,
419        QKey => 0x10,
420        RKey => 0x13,
421        SKey => 0x1F,
422        TKey => 0x14,
423        UKey => 0x16,
424        VKey => 0x2F,
425        WKey => 0x11,
426        XKey => 0x2D,
427        YKey => 0x15,
428        ZKey => 0x2C,
429        Numpad0Key => 0x52,
430        Numpad1Key => 0x4F,
431        Numpad2Key => 0x50,
432        Numpad3Key => 0x51,
433        Numpad4Key => 0x4B,
434        Numpad5Key => 0x4C,
435        Numpad6Key => 0x4D,
436        Numpad7Key => 0x47,
437        Numpad8Key => 0x48,
438        Numpad9Key => 0x49,
439        F1Key => 0x3B,
440        F2Key => 0x3C,
441        F3Key => 0x3D,
442        F4Key => 0x3E,
443        F5Key => 0x3F,
444        F6Key => 0x40,
445        F7Key => 0x41,
446        F8Key => 0x42,
447        F9Key => 0x43,
448        F10Key => 0x44,
449        NumLockKey => 0x45,
450        ScrollLockKey => 0x46,
451        CapsLockKey => 0x3A,
452        LShiftKey => 0x2A,
453        RShiftKey => 0x36,
454        LControlKey => 0x1D,
455        BackquoteKey => 0x29,
456        SlashKey => 0x35,
457        BackslashKey => 0x2B,
458        CommaKey => 0x33,
459        PeriodKey => 0x34,
460        MinusKey => 0x0C,
461        QuoteKey => 0x28,
462        SemicolonKey => 0x27,
463        LBracketKey => 0x1A,
464        RBracketKey => 0x1B,
465        EqualKey => 0x0D,
466        OtherKey(code) => code as i32,
467        _ => 0x0,
468    }
469}
470
471impl From<MouseButton> for uinput::event::controller::Mouse {
472    fn from(button: MouseButton) -> Self {
473        use uinput::event::controller::Mouse;
474        match button {
475            MouseButton::LeftButton => Mouse::Left,
476            MouseButton::RightButton => Mouse::Right,
477            MouseButton::MiddleButton => Mouse::Middle,
478            MouseButton::X1Button => unimplemented!(),
479            MouseButton::X2Button => unimplemented!(),
480            MouseButton::OtherButton(_) => unimplemented!(),
481        }
482    }
483}