Support multiple revert_default_keys
All checks were successful
moparisthebest/rusty-keys/pipeline/head This commit looks good

This commit is contained in:
Travis Burtrum 2021-04-03 17:15:38 -04:00
parent b1bb55766d
commit 95833e3a33
2 changed files with 21 additions and 6 deletions

View File

@ -3,9 +3,9 @@
# ie, in this case pressing both and 0 would go QWERTY, while both and 1 would go dvorak
switch_layout_keys = ['LEFTSHIFT','RIGHTSHIFT']
# pressing QWERTY reverts to the index specified in revert_keymap_index for only the duration of the pressing
# pressing any of these keys reverts to the index specified in revert_keymap_index for only the duration of the pressing
# used so QWERTY shortcuts like Ctrl+C still work
revert_default_key = 'LEFTCTRL'
revert_default_keys = ['LCTL','LGUI','LALT']
revert_keymap_index = 0
# this is the default index to use when the program first starts

View File

@ -63,7 +63,7 @@ pub struct KeyMaps<K, T, E, R = ()>
keymap_index_keys: HashMap<T, usize>,
switch_layout_keys: Vec<usize>,
key_state: [bool; KEY_MAX],
revert_default_key: T,
revert_default_keys: Vec<T>,
revert_keymap_index: usize,
// above do not change, below does
chosen_keymap_index: usize,
@ -173,13 +173,27 @@ impl<K, T, E, R> KeyMaps<K, T, E, R>
//println!("keymaps: {:?}", keymaps);
//println!("keymap_index_keys: {:?}", keymap_index_keys);
let mut revert_default_keys = Vec::new();
if config.revert_default_key.is_some() {
revert_default_keys.push(parse_key(key_map, &config.revert_default_key.unwrap()));
}
if config.revert_default_keys.is_some() {
for revert_default_key in config.revert_default_keys.unwrap() {
let revert_default_key = parse_key(key_map, &revert_default_key);
if !revert_default_keys.contains(&revert_default_key) {
revert_default_keys.push(revert_default_key);
}
}
}
// revert_default_keys may be empty, but that's ok
KeyMaps {
keymaps: keymaps,
keymap_index_keys: keymap_index_keys,
switch_layout_keys: config.switch_layout_keys.iter().map(|k| parse_key(key_map, k).into()).collect(),
key_state: [false; KEY_MAX],
// todo: detect key state? at least CAPSLOCK...
revert_default_key: parse_key(key_map, &config.revert_default_key),
revert_default_keys,
revert_keymap_index: config.revert_keymap_index,
chosen_keymap_index: config.default_keymap_index,
current_keymap_index: config.default_keymap_index,
@ -217,7 +231,7 @@ pub fn send_event(&mut self, mut event: &mut E, device: &K) -> Result<R> {
return device.block_key(); // we don't want to also send this keypress, so bail
}
}
if event.code() == self.revert_default_key {
if self.revert_default_keys.contains(&event.code()) {
match value {
// todo: ctrl+c will get c stuck because code c value 1 will be sent, but then we'll let go of ctrl, and code j value 0 is sent, so c is never released... fix that...
KeyState::DOWN => self.current_keymap_index = self.revert_keymap_index,
@ -445,7 +459,8 @@ use serde::Deserialize;
#[derive(Deserialize, Debug)]
pub struct KeymapConfig {
switch_layout_keys: Vec<String>,
revert_default_key: String,
revert_default_key: Option<String>,
revert_default_keys: Option<Vec<String>>,
revert_keymap_index: usize,
default_keymap_index: usize,
keymaps: Vec<String>