load keyboard layout for mac

This commit is contained in:
rustdesk
2022-01-14 01:15:03 +08:00
parent 7b1806b0f3
commit 780b57f597
3 changed files with 301 additions and 32 deletions

View File

@@ -40,7 +40,7 @@ struct Input {
time: i64,
}
const KEY_CHAR_START: i32 = 9999;
const KEY_CHAR_START: u64 = 9999;
#[derive(Clone, Default)]
pub struct MouseCursorSub {
@@ -163,7 +163,7 @@ fn run_cursor(sp: MouseCursorService, state: &mut StateCursor) -> ResultType<()>
lazy_static::lazy_static! {
static ref ENIGO: Arc<Mutex<Enigo>> = Arc::new(Mutex::new(Enigo::new()));
static ref KEYS_DOWN: Arc<Mutex<HashMap<i32, Instant>>> = Default::default();
static ref KEYS_DOWN: Arc<Mutex<HashMap<u64, Instant>>> = Default::default();
static ref LATEST_INPUT: Arc<Mutex<Input>> = Default::default();
}
static EXITING: AtomicBool = AtomicBool::new(false);
@@ -246,6 +246,11 @@ pub fn fix_key_down_timeout_at_exit() {
log::info!("fix_key_down_timeout_at_exit");
}
#[inline]
fn get_layout(key: u32) -> Key {
Key::Layout(std::char::from_u32(key).unwrap_or('\0'))
}
fn fix_key_down_timeout(force: bool) {
if KEYS_DOWN.lock().unwrap().is_empty() {
return;
@@ -256,13 +261,13 @@ fn fix_key_down_timeout(force: bool) {
if force || value.elapsed().as_millis() >= 3_000 {
KEYS_DOWN.lock().unwrap().remove(&key);
let key = if key < KEY_CHAR_START {
if let Some(key) = KEY_MAP.get(&key) {
if let Some(key) = KEY_MAP.get(&(key as _)) {
Some(*key)
} else {
None
}
} else {
Some(Key::Layout(((key - KEY_CHAR_START) as u8) as _))
Some(get_layout((key - KEY_CHAR_START) as _))
};
if let Some(key) = key {
let func = move || {
@@ -604,10 +609,13 @@ fn handle_key_(evt: &KeyEvent) {
}
if evt.down {
allow_err!(en.key_down(key.clone()));
KEYS_DOWN.lock().unwrap().insert(ck.value(), Instant::now());
KEYS_DOWN
.lock()
.unwrap()
.insert(ck.value() as _, Instant::now());
} else {
en.key_up(key.clone());
KEYS_DOWN.lock().unwrap().remove(&ck.value());
KEYS_DOWN.lock().unwrap().remove(&(ck.value() as _));
}
} else if ck.value() == ControlKey::CtrlAltDel.value() {
// have to spawn new thread because send_sas is tokio_main, the caller can not be tokio_main.
@@ -621,17 +629,17 @@ fn handle_key_(evt: &KeyEvent) {
}
Some(key_event::Union::chr(chr)) => {
if evt.down {
allow_err!(en.key_down(Key::Layout(chr as u8 as _)));
allow_err!(en.key_down(get_layout(chr)));
KEYS_DOWN
.lock()
.unwrap()
.insert(chr as i32 + KEY_CHAR_START, Instant::now());
.insert(chr as u64 + KEY_CHAR_START, Instant::now());
} else {
en.key_up(Key::Layout(chr as u8 as _));
en.key_up(get_layout(chr));
KEYS_DOWN
.lock()
.unwrap()
.remove(&(chr as i32 + KEY_CHAR_START));
.remove(&(chr as u64 + KEY_CHAR_START));
}
}
Some(key_event::Union::unicode(chr)) => {