Skip to content
This repository has been archived by the owner on Jun 8, 2021. It is now read-only.

Commit

Permalink
Nicer bindings for Key(val) related functions
Browse files Browse the repository at this point in the history
Fix #345
  • Loading branch information
danigm committed Apr 30, 2020
1 parent 0287908 commit 2c2cd54
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/enums.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#[allow(non_upper_case_globals)]
pub mod key {
use gdk_sys;
use glib::GString;
use keys;

pub type Key = u32;

Expand Down Expand Up @@ -2280,4 +2282,19 @@ pub mod key {
pub const Prev_VMode: Key = gdk_sys::GDK_KEY_Prev_VMode as u32;
pub const LogWindowTree: Key = gdk_sys::GDK_KEY_LogWindowTree as u32;
pub const LogGrabInfo: Key = gdk_sys::GDK_KEY_LogGrabInfo as u32;

pub trait Keys {
fn to_unicode(self) -> Option<char>;
fn name(self) -> Option<GString>;
}

impl Keys for Key {
fn to_unicode(self) -> Option<char> {
keys::keyval_to_unicode(self as u32)
}

fn name(self) -> Option<GString> {
keys::keyval_name(self as u32)
}
}
}
24 changes: 24 additions & 0 deletions tests/check_event.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
extern crate gdk;
extern crate gdk_sys;

use gdk::enums::key::Keys;

#[test]
fn check_event() {
gdk::init();
let base_ev = gdk::Event::new(gdk::EventType::KeyPress);
let mut ev: gdk::EventKey = base_ev.downcast().unwrap();
ev.as_mut().keyval = gdk::enums::key::A;

let keyval = gdk::keyval_to_unicode(ev.get_keyval());
let keyval2 = ev.get_keyval().to_unicode();

assert_eq!(keyval, Some('A'));
assert_eq!(keyval, keyval2);

let name = gdk::keyval_name(ev.get_keyval());
let name2 = ev.get_keyval().name();

assert_eq!(name, Some("A".into()));
assert_eq!(name, name2);
}

0 comments on commit 2c2cd54

Please sign in to comment.