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
use super::*;

/**
Represents an input event which occurred on the keyboard.
*/
#[derive(Clone, Copy, Debug, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct KeyEvent {
    /// The character associated with the pressed key.
    pub character: char,
    /// The KeyCode of the key which the event occurred on.
    pub key_code: KeyCode,
    /// A ControlKeyState object describing the state of control keys.
    pub modifiers: ControlKeyState,
    /// Is the key pressed?
    pub pressed: bool,
    /// The amount of times the event was repeated in the input buffer.
    pub repeat_count: u16,
    /// The scan code of the key.
    pub scan_code: u16,
}

impl KeyEvent {
    /**
    Returns an empty KeyEvent.
    */
    pub fn new() -> KeyEvent {
        KeyEvent {
            character: '\0',
            modifiers: ControlKeyState::default(),
            key_code: KeyCode::None,
            pressed: false,
            repeat_count: 0,
            scan_code: 0,
        }
    }
}

impl Into<InputEvent> for KeyEvent {
    fn into(self) -> InputEvent {
        if self.pressed {
            InputEvent::KeyDown(self)
        } else {
            InputEvent::KeyUp(self)
        }
    }
}