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
// Take a look at the license at the top of the repository in the LICENSE file.

use glib::translate::*;

/// Describes a key press or key release event.
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct EventKey(crate::Event);

event_wrapper!(EventKey, GdkEventKey);
event_subtype!(EventKey, ffi::GDK_KEY_PRESS | ffi::GDK_KEY_RELEASE);

impl EventKey {
    #[doc(alias = "get_time")]
    pub fn time(&self) -> u32 {
        self.as_ref().time
    }

    #[doc(alias = "get_state")]
    pub fn state(&self) -> crate::ModifierType {
        unsafe { from_glib(self.as_ref().state) }
    }

    #[doc(alias = "get_keyval")]
    pub fn keyval(&self) -> crate::keys::Key {
        unsafe { from_glib(self.as_ref().keyval) }
    }

    #[doc(alias = "get_length")]
    pub fn length(&self) -> u32 {
        let length = self.as_ref().length;
        assert!(length >= 0, "Unexpected negative value");
        length as u32
    }

    #[doc(alias = "get_hardware_keycode")]
    pub fn hardware_keycode(&self) -> u16 {
        self.as_ref().hardware_keycode
    }

    #[doc(alias = "get_group")]
    pub fn group(&self) -> u8 {
        self.as_ref().group
    }

    #[doc(alias = "get_is_modifier")]
    pub fn is_modifier(&self) -> bool {
        self.as_ref().is_modifier & 1 != 0
    }
}