gdk4/
key_event.rs

1// Take a look at the license at the top of the repository in the LICENSE file.
2
3use std::{fmt, mem};
4
5use glib::translate::*;
6
7use crate::{ffi, EventType, Key, KeyEvent, KeyMatch, ModifierType};
8
9define_event! {
10    KeyEvent,
11    ffi::GdkKeyEvent,
12    &[EventType::KeyPress, EventType::KeyRelease]
13}
14
15impl KeyEvent {
16    /// Extracts the keyval from a key event.
17    ///
18    /// # Returns
19    ///
20    /// the keyval of @self
21    #[doc(alias = "gdk_key_event_get_keyval")]
22    #[doc(alias = "get_keyval")]
23    pub fn keyval(&self) -> Key {
24        unsafe { from_glib(ffi::gdk_key_event_get_keyval(self.to_glib_none().0)) }
25    }
26
27    /// Gets a keyval and modifier combination that will match
28    /// the event.
29    ///
30    /// See [`matches()`][Self::matches()].
31    ///
32    /// # Returns
33    ///
34    /// [`true`] on success
35    ///
36    /// ## `keyval`
37    /// return location for a keyval
38    ///
39    /// ## `modifiers`
40    /// return location for modifiers
41    #[doc(alias = "gdk_key_event_get_match")]
42    #[doc(alias = "get_match")]
43    pub fn match_(&self) -> Option<(Key, ModifierType)> {
44        unsafe {
45            let mut keyval = mem::MaybeUninit::uninit();
46            let mut modifiers = mem::MaybeUninit::uninit();
47            let ret = from_glib(ffi::gdk_key_event_get_match(
48                self.to_glib_none().0,
49                keyval.as_mut_ptr(),
50                modifiers.as_mut_ptr(),
51            ));
52            if ret {
53                let keyval = keyval.assume_init();
54                let modifiers = modifiers.assume_init();
55                Some((from_glib(keyval), from_glib(modifiers)))
56            } else {
57                None
58            }
59        }
60    }
61
62    /// Matches a key event against a keyval and modifiers.
63    ///
64    /// This is typically used to trigger keyboard shortcuts such as Ctrl-C.
65    ///
66    /// Partial matches are possible where the combination matches
67    /// if the currently active group is ignored.
68    ///
69    /// Note that we ignore Caps Lock for matching.
70    /// ## `keyval`
71    /// the keyval to match
72    /// ## `modifiers`
73    /// the modifiers to match
74    ///
75    /// # Returns
76    ///
77    /// a [`KeyMatch`][crate::KeyMatch] value describing whether @self matches
78    #[doc(alias = "gdk_key_event_matches")]
79    pub fn matches(&self, keyval: Key, modifiers: ModifierType) -> KeyMatch {
80        unsafe {
81            from_glib(ffi::gdk_key_event_matches(
82                self.to_glib_none().0,
83                keyval.into_glib(),
84                modifiers.into_glib(),
85            ))
86        }
87    }
88}
89
90impl fmt::Debug for KeyEvent {
91    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
92        f.debug_struct("KeyEvent")
93            .field("consumed_modifiers", &self.consumed_modifiers())
94            .field("keycode", &self.keycode())
95            .field("keyval", &self.keyval())
96            .field("layout", &self.layout())
97            .field("level", &self.level())
98            .field("match", &self.match_())
99            .field("is_modifier", &self.is_modifier())
100            .finish()
101    }
102}