mirror of
https://github.com/FliegendeWurst/cursive.git
synced 2024-11-23 17:35:00 +00:00
ncurses: compute whole modifier table at runtime
This commit is contained in:
parent
b2097bd1c8
commit
0c39f52016
@ -365,26 +365,6 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn parse_modifier(code: i32, key: Key) -> Event {
|
|
||||||
match code {
|
|
||||||
0 => Event::Alt(key),
|
|
||||||
1 => Event::AltShift(key),
|
|
||||||
2 => Event::Ctrl(key),
|
|
||||||
3 => Event::CtrlShift(key),
|
|
||||||
4 => Event::CtrlAlt(key),
|
|
||||||
_ => {
|
|
||||||
warn!("Parsing invalid modifier: {} for key {:?}", code, key);
|
|
||||||
Event::Unknown(split_i32(code))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn add_modifiers(start: i32, key: Key, map: &mut HashMap<i32, Event>) {
|
|
||||||
for i in 0..5 {
|
|
||||||
map.insert(start + i, parse_modifier(i, key));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn add_fn<F>(start: i32, with_key: F, map: &mut HashMap<i32, Event>)
|
fn add_fn<F>(start: i32, with_key: F, map: &mut HashMap<i32, Event>)
|
||||||
where
|
where
|
||||||
F: Fn(Key) -> Event,
|
F: Fn(Key) -> Event,
|
||||||
@ -455,25 +435,45 @@ fn initialize_keymap() -> HashMap<i32, Event> {
|
|||||||
add_fn(313, Event::Alt, &mut map);
|
add_fn(313, Event::Alt, &mut map);
|
||||||
|
|
||||||
// Those codes actually vary between ncurses versions...
|
// Those codes actually vary between ncurses versions...
|
||||||
// Use ncurses::keyname to find the key representing kDC3 (alt-DEL)
|
|
||||||
let del_offset = (512..1024)
|
|
||||||
.find(|&code| {
|
|
||||||
ncurses::keyname(code)
|
|
||||||
.map(|name| &name == "kDC3")
|
|
||||||
.unwrap_or(false)
|
|
||||||
})
|
|
||||||
.unwrap_or(522);
|
|
||||||
|
|
||||||
add_modifiers(del_offset, Key::Del, &mut map);
|
let key_names = hashmap!{
|
||||||
add_modifiers(del_offset + 6, Key::Down, &mut map);
|
"DC" => Key::Del,
|
||||||
add_modifiers(del_offset + 11, Key::End, &mut map);
|
"DN" => Key::Down,
|
||||||
add_modifiers(del_offset + 17, Key::Home, &mut map);
|
"END" => Key::End,
|
||||||
add_modifiers(del_offset + 23, Key::Ins, &mut map);
|
"HOM" => Key::Home,
|
||||||
add_modifiers(del_offset + 28, Key::Left, &mut map);
|
"IC" => Key::Ins,
|
||||||
add_modifiers(del_offset + 33, Key::PageDown, &mut map);
|
"LFT" => Key::Left,
|
||||||
add_modifiers(del_offset + 38, Key::PageUp, &mut map);
|
"NXT" => Key::PageDown,
|
||||||
add_modifiers(del_offset + 43, Key::Right, &mut map);
|
"PRV" => Key::PageUp,
|
||||||
add_modifiers(del_offset + 49, Key::Up, &mut map);
|
"RIT" => Key::Right,
|
||||||
|
"UP" => Key::Up,
|
||||||
|
};
|
||||||
|
|
||||||
|
for code in 512..1024 {
|
||||||
|
let name = match ncurses::keyname(code) {
|
||||||
|
Some(name) => name,
|
||||||
|
None => continue,
|
||||||
|
};
|
||||||
|
|
||||||
|
if !name.starts_with("k") {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
let (key_name, modifier) = name[1..].split_at(name.len() - 2);
|
||||||
|
let key = match key_names.get(key_name) {
|
||||||
|
Some(&key) => key,
|
||||||
|
None => continue,
|
||||||
|
};
|
||||||
|
let event = match modifier {
|
||||||
|
"3" => Event::Alt(key),
|
||||||
|
"4" => Event::AltShift(key),
|
||||||
|
"5" => Event::Ctrl(key),
|
||||||
|
"6" => Event::CtrlShift(key),
|
||||||
|
"7" => Event::CtrlAlt(key),
|
||||||
|
_ => continue,
|
||||||
|
};
|
||||||
|
map.insert(code, event);
|
||||||
|
}
|
||||||
|
|
||||||
map
|
map
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user