Refactor keyboard mode by enum

This commit is contained in:
Asura 2022-07-19 15:43:13 +08:00
parent 0bacc1c250
commit 02b4d7f1d9
4 changed files with 36 additions and 21 deletions

View File

@ -97,6 +97,13 @@ message MouseEvent {
repeated ControlKey modifiers = 4;
}
enum KeyboardMode{
Legacy = 0;
Map = 1;
Translate = 2;
Auto = 3;
}
enum ControlKey {
Unknown = 0;
Alt = 1;
@ -190,7 +197,7 @@ message KeyEvent {
string seq = 6;
}
repeated ControlKey modifiers = 8;
uint32 mode = 9;
KeyboardMode mode = 9;
}
message CursorData {

View File

@ -420,11 +420,11 @@ impl Connection {
}
MessageInput::Key((mut msg, press)) => {
// todo: press and down have similar meanings.
if press && msg.mode == 0 {
if press && msg.mode.unwrap() == KeyboardMode::Legacy {
msg.down = true;
}
handle_key(&msg);
if press && msg.mode == 0 {
if press && msg.mode.unwrap() == KeyboardMode::Legacy {
msg.down = false;
handle_key(&msg);
}

View File

@ -450,7 +450,7 @@ pub fn lock_screen() {
key_event.down = true;
key_event.set_chr('l' as _);
key_event.modifiers.push(ControlKey::Meta.into());
key_event.mode = 0;
key_event.mode = KeyboardMode::Legacy;
handle_key(&key_event);
key_event.down = false;
handle_key(&key_event);
@ -463,7 +463,7 @@ pub fn lock_screen() {
key_event.set_chr('q' as _);
key_event.modifiers.push(ControlKey::Meta.into());
key_event.modifiers.push(ControlKey::Control.into());
key_event.mode = 0;
key_event.mode = KeyboardMode::Legacy;
handle_key(&key_event);
key_event.down = false;
handle_key(&key_event);
@ -787,11 +787,11 @@ fn handle_key_(evt: &KeyEvent) {
return;
}
match evt.mode {
0 => {
match evt.mode.unwrap() {
KeyboardMode::Legacy => {
legacy_keyboard_map(evt);
}
1 => {
KeyboardMode::Map => {
map_keyboard_map(evt);
}
_ => {

View File

@ -41,6 +41,7 @@ use hbb_common::{
time::{self, Duration, Instant, Interval},
},
Stream,
protobuf::ProtobufEnumOrUnknown,
};
use hbb_common::{config::TransferSerde, fs::TransferJobMeta};
use rdev::{Event, EventType::*, Key as RdevKey};
@ -921,14 +922,14 @@ impl Handler {
key_event.set_control_key(ControlKey::CtrlAltDel);
// todo
key_event.down = true;
self.send_key_event(key_event, 0);
self.send_key_event(key_event, KeyboardMode::Legacy);
} else {
let mut key_event = KeyEvent::new();
key_event.set_control_key(ControlKey::Delete);
self.legacy_modifiers(&mut key_event, true, true, false, false);
// todo
key_event.press = true;
self.send_key_event(key_event, 0);
self.send_key_event(key_event, KeyboardMode::Legacy);
}
}
@ -937,7 +938,7 @@ impl Handler {
key_event.set_control_key(ControlKey::LockScreen);
// todo
key_event.down = true;
self.send_key_event(key_event, 0);
self.send_key_event(key_event, KeyboardMode::Legacy);
}
fn transfer_file(&mut self) {
@ -956,9 +957,9 @@ impl Handler {
}
}
fn send_key_event(&mut self, mut evt: KeyEvent, keyboard_mode: u32) {
// mode: map(1), translate(2), legacy(3), auto(4)
evt.mode = keyboard_mode;
fn send_key_event(&mut self, mut evt: KeyEvent, keyboard_mode: KeyboardMode) {
// mode: legacy(0), map(1), translate(2), auto(3)
evt.mode = ProtobufEnumOrUnknown::new(keyboard_mode);
let mut msg_out = Message::new();
msg_out.set_key_event(evt);
log::info!("{:?}", msg_out);
@ -1012,7 +1013,7 @@ impl Handler {
key_event.modifiers.push(ControlKey::NumLock.into());
}
self.send_key_event(key_event, 1);
self.send_key_event(key_event, KeyboardMode::Map);
}
// fn translate_keyboard_mode(&mut self, down_or_up: bool, key: RdevKey) {
@ -1269,15 +1270,22 @@ impl Handler {
if down_or_up == true {
key_event.down = true;
}
dbg!(&key_event);
self.send_key_event(key_event, 0)
self.send_key_event(key_event, KeyboardMode::Legacy)
}
fn key_down_or_up(&mut self, down_or_up: bool, key: RdevKey, evt: Event) {
// Call different functions according to keyboard mode.
let mode = std::env::var("KEYBOARD_MOAD").unwrap_or(String::from("legacy"));
match mode.as_str() {
"map" => {
let mode = match std::env::var("KEYBOARD_MOAD")
.unwrap_or(String::from("legacy"))
.as_str()
{
"map" => KeyboardMode::Map,
"legacy" => KeyboardMode::Legacy,
_ => KeyboardMode::Legacy,
};
match mode {
KeyboardMode::Map => {
if down_or_up == true {
TO_RELEASE.lock().unwrap().insert(key);
} else {
@ -1285,7 +1293,7 @@ impl Handler {
}
self.map_keyboard_mode(down_or_up, key);
}
"legacy" => self.legacy_keyboard_mode(down_or_up, key, evt),
KeyboardMode::Legacy => self.legacy_keyboard_mode(down_or_up, key, evt),
_ => self.legacy_keyboard_mode(down_or_up, key, evt),
}
}