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
//! Contains the struct for key event [`KeyPress`] and [`KeyCode`]

use std::fmt::{Display, Formatter};

#[cfg(feature = "serde-serialize")]
use serde::{Deserialize, Serialize};

/// Key press event from the read device.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
#[cfg_attr(feature = "serde-serialize", derive(Serialize, Deserialize))]
pub struct KeyPress {
    /// Which key recived an event
    key_code: KeyCode,
    /// Is the key pressed or released
    is_pressed: bool,
}

impl KeyPress {
    /// read key press from buffer hid read buffer.
    pub const fn new_from_buffer(buffer: [u8; 5]) -> Self {
        Self {
            key_code: KeyCode::new(buffer[2], buffer[3]),
            is_pressed: {
                match buffer[2] {
                    10 => buffer[4] == 0, // for some reason the caps lock signal is inverted
                    _ => buffer[4] != 0,
                }
            },
        }
    }

    /// Create a new event from a [`KeyCode`] and a [`bool`]
    pub const fn new(key_code: KeyCode, is_pressed: bool) -> Self {
        Self {
            key_code,
            is_pressed,
        }
    }

    /// Get the key code.
    pub const fn key_code(&self) -> &KeyCode {
        &self.key_code
    }

    /// Is pressed event.
    pub const fn is_pressed(self) -> bool {
        self.is_pressed
    }

    /// Get the key code.
    pub fn key_code_mut(&mut self) -> &mut KeyCode {
        &mut self.key_code
    }

    /// Is pressed event.
    pub fn is_pressed_mut(&mut self) -> &mut bool {
        &mut self.is_pressed
    }
}

impl From<KeyPress> for KeyCode {
    fn from(d: KeyPress) -> Self {
        d.key_code
    }
}

impl Display for KeyPress {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        match self.is_pressed() {
            true => write!(f, "key code {} being pressed", self.key_code()),
            false => write!(f, "key code {} being release", self.key_code()),
        }
    }
}

/// structur of data to incode the key when a key press is read form hid device.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
#[cfg_attr(feature = "serde-serialize", derive(Serialize, Deserialize))]
pub struct KeyCode {
    /// first u8 representing the key code
    first_u8: u8,
    /// Second u8 representing the key code
    seconde_u8: u8,
}

impl KeyCode {
    /// Create a new key code
    pub const fn new(first_u8: u8, seconde_u8: u8) -> Self {
        Self {
            first_u8,
            seconde_u8,
        }
    }

    /// First u8 of the code.
    pub const fn first_u8(self) -> u8 {
        self.first_u8
    }

    /// Seconde u8 of the code.
    pub const fn seconde_u8(self) -> u8 {
        self.seconde_u8
    }

    /// First u8 of the code.
    pub fn first_u8_mut(&mut self) -> &mut u8 {
        &mut self.first_u8
    }

    /// Seconde u8 of the code.
    pub fn seconde_u8_mut(&mut self) -> &mut u8 {
        &mut self.seconde_u8
    }
}

impl From<[u8; 2]> for KeyCode {
    fn from(array: [u8; 2]) -> Self {
        KeyCode::new(array[0], array[1])
    }
}

impl From<(u8, u8)> for KeyCode {
    fn from(tuple: (u8, u8)) -> Self {
        let (a, b) = tuple;
        KeyCode::new(a, b)
    }
}

impl From<KeyCode> for [u8; 2] {
    fn from(d: KeyCode) -> Self {
        [d.first_u8(), d.seconde_u8()]
    }
}

impl Display for KeyCode {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(f, "({}, {})", self.first_u8(), self.seconde_u8())
    }
}