Compare commits

..

1 Commits

Author SHA1 Message Date
Ferdinand Schober
f0f73ea7ba log pressed keys 2024-09-23 20:48:45 +02:00
3 changed files with 16 additions and 29 deletions

View File

@@ -160,6 +160,7 @@ impl InputCapture {
_ => self.pressed_keys.remove(&scancode),
};
}
log::info!("pressed keys: {:?}", self.pressed_keys);
}
}

View File

@@ -217,13 +217,15 @@ impl InputEmulation {
return false;
};
if state == 0 {
let ret = if state == 0 {
// currently pressed => can release
pressed_keys.remove(&key)
} else {
// currently not pressed => can press
pressed_keys.insert(key)
}
};
log::info!("client {handle}: pressed keys: {:?}", pressed_keys);
ret
}
}

View File

@@ -11,9 +11,8 @@ use core_graphics::event_source::{CGEventSource, CGEventSourceStateID};
use input_event::{Event, KeyboardEvent, PointerEvent};
use keycode::{KeyMap, KeyMapping};
use std::ops::{Index, IndexMut};
use std::sync::Arc;
use std::time::Duration;
use tokio::{sync::Notify, task::JoinHandle};
use tokio::task::AbortHandle;
use super::error::MacOSEmulationCreationError;
@@ -22,9 +21,8 @@ const DEFAULT_REPEAT_INTERVAL: Duration = Duration::from_millis(32);
pub(crate) struct MacOSEmulation {
event_source: CGEventSource,
repeat_task: Option<JoinHandle<()>>,
repeat_task: Option<AbortHandle>,
button_state: ButtonState,
notify_repeat_task: Arc<Notify>,
}
struct ButtonState {
@@ -70,7 +68,6 @@ impl MacOSEmulation {
event_source,
button_state,
repeat_task: None,
notify_repeat_task: Arc::new(Notify::new()),
})
}
@@ -82,33 +79,20 @@ impl MacOSEmulation {
async fn spawn_repeat_task(&mut self, key: u16) {
// there can only be one repeating key and it's
// always the last to be pressed
self.cancel_repeat_task().await;
self.kill_repeat_task();
let event_source = self.event_source.clone();
let notify = self.notify_repeat_task.clone();
let repeat_task = tokio::task::spawn_local(async move {
let stop = tokio::select! {
_ = tokio::time::sleep(DEFAULT_REPEAT_DELAY) => false,
_ = notify.notified() => true,
};
if !stop {
loop {
key_event(event_source.clone(), key, 1);
tokio::select! {
_ = tokio::time::sleep(DEFAULT_REPEAT_INTERVAL) => {},
_ = notify.notified() => break,
}
}
tokio::time::sleep(DEFAULT_REPEAT_DELAY).await;
loop {
key_event(event_source.clone(), key, 1);
tokio::time::sleep(DEFAULT_REPEAT_INTERVAL).await;
}
// release key when cancelled
key_event(event_source.clone(), key, 0);
});
self.repeat_task = Some(repeat_task);
self.repeat_task = Some(repeat_task.abort_handle());
}
async fn cancel_repeat_task(&mut self) {
fn kill_repeat_task(&mut self) {
if let Some(task) = self.repeat_task.take() {
self.notify_repeat_task.notify_waiters();
let _ = task.await;
task.abort();
}
}
}
@@ -362,7 +346,7 @@ impl Emulation for MacOSEmulation {
match state {
// pressed
1 => self.spawn_repeat_task(code).await,
_ => self.cancel_repeat_task().await,
_ => self.kill_repeat_task(),
}
key_event(self.event_source.clone(), code, state)
}