modifiers: Support Control and Alt
Control and Alt are special in that they aren't expected to switch levels, and so don't need to change what characters are output. Use in layouts by adding `modifier: Control` or `modifier: Alt` in place of `text: "foo"`. The latching of the modifier will force the keyboard to emit raw key presses and prevent it from outputting text.
This commit is contained in:
@ -16,14 +16,19 @@
|
||||
* The text-input interface may be enabled and disabled at arbitrary times,
|
||||
* and those events SHOULD NOT cause any lost events.
|
||||
* */
|
||||
|
||||
|
||||
use std::collections::HashSet;
|
||||
use std::ffi::CString;
|
||||
|
||||
use ::action::Modifier;
|
||||
use ::imservice;
|
||||
use ::imservice::IMService;
|
||||
use ::keyboard::{ KeyCode, KeyStateId, PressType };
|
||||
use ::keyboard::{ KeyCode, KeyStateId, Modifiers, PressType };
|
||||
use ::util::vec_remove;
|
||||
use ::vkeyboard::VirtualKeyboard;
|
||||
|
||||
// traits
|
||||
use std::iter::FromIterator;
|
||||
|
||||
/// Gathers stuff defined in C or called by C
|
||||
pub mod c {
|
||||
use super::*;
|
||||
@ -60,6 +65,7 @@ pub mod c {
|
||||
Box::<Submission>::into_raw(Box::new(
|
||||
Submission {
|
||||
imservice,
|
||||
modifiers_active: Vec::new(),
|
||||
virtual_keyboard: VirtualKeyboard(vk),
|
||||
pressed: Vec::new(),
|
||||
}
|
||||
@ -106,6 +112,7 @@ enum SubmittedAction {
|
||||
pub struct Submission {
|
||||
imservice: Option<Box<IMService>>,
|
||||
virtual_keyboard: VirtualKeyboard,
|
||||
modifiers_active: Vec<(KeyStateId, Modifier)>,
|
||||
pressed: Vec<(KeyStateId, SubmittedAction)>,
|
||||
}
|
||||
|
||||
@ -125,8 +132,10 @@ impl Submission {
|
||||
keycodes: &Vec<KeyCode>,
|
||||
time: Timestamp,
|
||||
) {
|
||||
let was_committed_as_text = match &mut self.imservice {
|
||||
Some(imservice) => {
|
||||
let mods_are_on = !self.modifiers_active.is_empty();
|
||||
|
||||
let was_committed_as_text = match (&mut self.imservice, mods_are_on) {
|
||||
(Some(imservice), false) => {
|
||||
enum Outcome {
|
||||
Submitted(Result<(), imservice::SubmitError>),
|
||||
NotSubmitted,
|
||||
@ -157,7 +166,7 @@ impl Submission {
|
||||
Outcome::NotSubmitted => false,
|
||||
}
|
||||
},
|
||||
_ => false,
|
||||
(_, _) => false,
|
||||
};
|
||||
|
||||
let submit_action = match was_committed_as_text {
|
||||
@ -194,4 +203,44 @@ impl Submission {
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
pub fn handle_add_modifier(
|
||||
&mut self,
|
||||
key_id: KeyStateId,
|
||||
modifier: Modifier, _time: Timestamp,
|
||||
) {
|
||||
self.modifiers_active.push((key_id, modifier));
|
||||
self.update_modifiers();
|
||||
}
|
||||
|
||||
pub fn handle_drop_modifier(
|
||||
&mut self,
|
||||
key_id: KeyStateId,
|
||||
_time: Timestamp,
|
||||
) {
|
||||
vec_remove(&mut self.modifiers_active, |(id, _)| *id == key_id);
|
||||
self.update_modifiers();
|
||||
}
|
||||
|
||||
fn update_modifiers(&mut self) {
|
||||
let raw_modifiers = self.modifiers_active.iter()
|
||||
.map(|(_id, m)| match m {
|
||||
Modifier::Control => Modifiers::CONTROL,
|
||||
Modifier::Alt => Modifiers::MOD1,
|
||||
})
|
||||
.fold(Modifiers::empty(), |m, n| m | n);
|
||||
self.virtual_keyboard.set_modifiers_state(raw_modifiers);
|
||||
}
|
||||
|
||||
pub fn is_modifier_active(&self, modifier: Modifier) -> bool {
|
||||
self.modifiers_active.iter()
|
||||
.position(|(_id, m)| *m == modifier)
|
||||
.is_some()
|
||||
}
|
||||
|
||||
pub fn get_active_modifiers(&self) -> HashSet<Modifier> {
|
||||
HashSet::from_iter(
|
||||
self.modifiers_active.iter().map(|(_id, m)| m.clone())
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user