neovim-gtk/src/input.rs

63 lines
1.5 KiB
Rust
Raw Normal View History

2016-04-04 10:14:57 +00:00
use gdk;
use gdk::EventKey;
use phf;
include!(concat!(env!("OUT_DIR"), "/key_map_table.rs"));
2017-03-31 21:12:00 +00:00
pub fn keyval_to_input_string(in_str: &str, state: gdk::ModifierType) -> String {
let mut val = in_str;
let mut input = String::new();
2017-03-31 21:12:00 +00:00
// CTRL-^ and CTRL-@ don't work in the normal way.
if state.contains(gdk::CONTROL_MASK) && !state.contains(gdk::SHIFT_MASK) &&
!state.contains(gdk::MOD1_MASK) {
if val == "6" {
val = "^";
} else if val == "2" {
val = "@";
}
}
if state.contains(gdk::SHIFT_MASK) {
2017-03-07 14:12:22 +00:00
if val != "\"" {
input.push_str("S-");
}
2016-04-04 10:14:57 +00:00
}
if state.contains(gdk::CONTROL_MASK) {
2016-04-04 10:14:57 +00:00
input.push_str("C-");
}
if state.contains(gdk::MOD1_MASK) {
2016-04-04 10:14:57 +00:00
input.push_str("A-");
}
2016-04-04 10:14:57 +00:00
input.push_str(val);
2017-03-12 15:11:43 +00:00
if input.chars().count() > 1 {
format!("<{}>", input)
} else {
input
}
2016-04-04 10:14:57 +00:00
}
pub fn convert_key(ev: &EventKey) -> Option<String> {
let keyval = ev.get_keyval();
let state = ev.get_state();
if let Some(ref keyval_name) = gdk::keyval_name(keyval) {
if let Some(cnvt) = KEYVAL_MAP.get(keyval_name as &str).cloned() {
return Some(keyval_to_input_string(cnvt, state));
}
}
2016-05-03 10:34:36 +00:00
2016-04-04 10:14:57 +00:00
if let Some(ch) = gdk::keyval_to_unicode(keyval) {
2016-05-03 10:34:36 +00:00
Some(if !state.is_empty() {
2016-04-04 10:14:57 +00:00
keyval_to_input_string(&ch.to_string(), state)
} else {
ch.to_string()
2016-05-03 10:34:36 +00:00
})
} else {
None
2016-04-04 10:14:57 +00:00
}
}