swap key renewal

This commit is contained in:
sjpark 2023-02-11 10:41:06 +09:00
parent f438dd9fd0
commit b29236da06
4 changed files with 102 additions and 66 deletions

View File

@ -167,3 +167,8 @@ panic = 'abort'
strip = true
#opt-level = 'z' # only have smaller size after strip
rpath = true
[patch."https://github.com/fufesou/rdev"]
#rdev = { path = "../rdev" }
rdev = { git = "https://github.com/sj6219/rdev", branch = "sigma" }

View File

@ -1702,6 +1702,7 @@ pub fn send_mouse(
if check_scroll_on_mac(mask, x, y) {
mouse_event.modifiers.push(ControlKey::Scroll.into());
}
interface.swap_modifier_mouse(&mut mouse_event);
msg_out.set_mouse_event(mouse_event);
interface.send(Data::Message(msg_out));
}
@ -1928,6 +1929,7 @@ pub trait Interface: Send + Clone + 'static + Sized {
fn is_force_relay(&self) -> bool {
self.get_login_config_handler().read().unwrap().force_relay
}
fn swap_modifier_mouse(&self, msg : &mut hbb_common::protos::message::MouseEvent) {}
}
/// Data used by the client interface.

View File

@ -202,70 +202,14 @@ pub fn update_grab_get_key_name() {
#[cfg(target_os = "windows")]
static mut IS_0X021D_DOWN: bool = false;
fn swap_modifier_key(mut event: Event) -> Event {
let mut allow_swap_key = false;
#[cfg(not(any(feature = "flutter", feature = "cli")))]
if let Some(session) = CUR_SESSION.lock().unwrap().as_ref() {
allow_swap_key = session.get_toggle_option("allow_swap_key".to_string());
}
#[cfg(feature = "flutter")]
if let Some(session) = SESSIONS
.read()
.unwrap()
.get(&*CUR_SESSION_ID.read().unwrap())
{
allow_swap_key = session.get_toggle_option("allow_swap_key".to_string());
}
if allow_swap_key {
match event.event_type {
EventType::KeyPress( key) => {
let key = match key {
rdev::Key::ControlLeft => rdev::Key::MetaLeft,
rdev::Key::MetaLeft => rdev::Key::ControlLeft,
rdev::Key::ControlRight => rdev::Key::MetaLeft,
rdev::Key::MetaRight => rdev::Key::ControlLeft,
_ => key,
};
event.event_type = EventType::KeyPress(key);
#[cfg(target_os = "windows")]
let scan_code = rdev::win_scancode_from_key(key).unwrap_or_default();
#[cfg(target_os = "macos")]
let scan_code = rdev::macos_keycode_from_key(key).unwrap_or_default();
event.scan_code = scan_code;
event.code = event.scan_code as _;
}
EventType::KeyRelease(key) => {
let key = match key {
rdev::Key::ControlLeft => rdev::Key::MetaLeft,
rdev::Key::MetaLeft => rdev::Key::ControlLeft,
rdev::Key::ControlRight => rdev::Key::MetaLeft,
rdev::Key::MetaRight => rdev::Key::ControlLeft,
_ => key,
};
event.event_type = EventType::KeyRelease(key);
#[cfg(target_os = "windows")]
let scan_code = rdev::win_scancode_from_key(key).unwrap_or_default();
#[cfg(target_os = "macos")]
let scan_code = rdev::macos_keycode_from_key(key).unwrap_or_default();
event.scan_code = scan_code;
event.code = event.scan_code as _;
}
_ => {}
};
}
event
}
pub fn start_grab_loop() {
#[cfg(any(target_os = "windows", target_os = "macos"))]
std::thread::spawn(move || {
let try_handle_keyboard = move |event: Event, key: Key, is_press: bool| -> Option<Event> {
let try_handle_keyboard = move |event: Event, key: Key, is_press: bool| -> Option<Event> {
// fix #2211CAPS LOCK don't work
if key == Key::CapsLock || key == Key::NumLock {
return Some(event);
}
let event = swap_modifier_key(event);
let mut _keyboard_mode = KeyboardMode::Map;
let _scan_code = event.scan_code;

View File

@ -335,10 +335,87 @@ impl<T: InvokeUiSession> Session<T> {
return "".to_owned();
}
pub fn swab_modifier_key(&self, msg: &mut KeyEvent) {
let allow_swap_key = self.get_toggle_option("allow_swap_key".to_string());
if allow_swap_key {
if let Some(key_event::Union::ControlKey(ck)) = msg.union {
let ck = ck.enum_value_or_default();
let ck = match ck {
ControlKey::Control => ControlKey::Meta,
ControlKey::Meta => ControlKey::Control,
ControlKey::RControl => ControlKey::Meta,
ControlKey::RWin => ControlKey::Control,
_ => ck,
};
msg.set_control_key(ck);
}
msg.modifiers = msg.modifiers.iter().map(|ck| {
let ck = ck.enum_value_or_default();
let ck = match ck {
ControlKey::Control => ControlKey::Meta,
ControlKey::Meta => ControlKey::Control,
ControlKey::RControl => ControlKey::Meta,
ControlKey::RWin => ControlKey::Control,
_ => ck,
};
hbb_common::protobuf::EnumOrUnknown::new(ck)
}).collect();
let code = msg.chr();
if code != 0 {
let mut peer = self.peer_platform().to_lowercase();
peer.retain(|c| !c.is_whitespace());
let key = match peer.as_str() {
"windows" => {
let key = rdev::win_key_from_code(code);
let key = match key {
rdev::Key::ControlLeft => rdev::Key::MetaLeft,
rdev::Key::MetaLeft => rdev::Key::ControlLeft,
rdev::Key::ControlRight => rdev::Key::MetaLeft,
rdev::Key::MetaRight => rdev::Key::ControlLeft,
_ => key,
};
rdev::win_keycode_from_key(key).unwrap_or_default()
}
"macos" => {
let key = rdev::macos_key_from_code(code);
let key = match key {
rdev::Key::ControlLeft => rdev::Key::MetaLeft,
rdev::Key::MetaLeft => rdev::Key::ControlLeft,
rdev::Key::ControlRight => rdev::Key::MetaLeft,
rdev::Key::MetaRight => rdev::Key::ControlLeft,
_ => key,
};
rdev::macos_keycode_from_key(key).unwrap_or_default()
}
_ => {
let key = rdev::linux_key_from_code(code);
let key = match key {
rdev::Key::ControlLeft => rdev::Key::MetaLeft,
rdev::Key::MetaLeft => rdev::Key::ControlLeft,
rdev::Key::ControlRight => rdev::Key::MetaLeft,
rdev::Key::MetaRight => rdev::Key::ControlLeft,
_ => key,
};
rdev::linux_keycode_from_key(key).unwrap_or_default()
}
};
msg.set_chr(key);
}
}
}
pub fn send_key_event(&self, evt: &KeyEvent) {
// mode: legacy(0), map(1), translate(2), auto(3)
let mut msg = evt.clone();
self.swab_modifier_key(&mut msg);
let mut msg_out = Message::new();
msg_out.set_key_event(evt.clone());
msg_out.set_key_event(msg);
self.send(Data::Message(msg_out));
}
@ -505,14 +582,6 @@ impl<T: InvokeUiSession> Session<T> {
shift: bool,
command: bool,
) {
let (ctrl, command) =
if self.get_toggle_option("allow_swap_key".to_string()) {
(command, ctrl)
}
else {
(ctrl, command)
};
#[allow(unused_mut)]
let mut command = command;
#[cfg(windows)]
@ -851,6 +920,22 @@ impl<T: InvokeUiSession> Interface for Session<T> {
handle_test_delay(t, peer).await;
}
}
fn swap_modifier_mouse(&self, msg : &mut hbb_common::protos::message::MouseEvent) {
let allow_swap_key = self.get_toggle_option("allow_swap_key".to_string());
if allow_swap_key {
msg.modifiers = msg.modifiers.iter().map(|ck| {
let ck = ck.enum_value_or_default();
let ck = match ck {
ControlKey::Control => ControlKey::Meta,
ControlKey::Meta => ControlKey::Control,
ControlKey::RControl => ControlKey::Meta,
ControlKey::RWin => ControlKey::Control,
_ => ck,
};
hbb_common::protobuf::EnumOrUnknown::new(ck)
}).collect();
};
}
}
impl<T: InvokeUiSession> Session<T> {