Skip to content

YC (S26) | Open Computer History | Continuously record your company computer work, map your workflows, help you find work worth automating, and power your agents' context

fix: Windows keyboard text never flushing to database (#2692)

Louis Beaumontd5db3b1e6a64
diff --git a/crates/screenpipe-a11y/src/platform/windows.rs b/crates/screenpipe-a11y/src/platform/windows.rs
index a725ae84a..0fa762732 100644
--- a/crates/screenpipe-a11y/src/platform/windows.rs
+++ b/crates/screenpipe-a11y/src/platform/windows.rs
@@ -358,15 +358,19 @@ fn run_native_hooks(
}
// Message loop (required for hooks to receive events)
+ // Install a timer so GetMessageW wakes periodically for text buffer flushing.
+ // Without this, typed text stays in the buffer until the next input event.
+ const TEXT_FLUSH_TIMER_ID: usize = 42;
+ SetTimer(HWND::default(), TEXT_FLUSH_TIMER_ID, 100, None);
+
let mut msg = MSG::default();
while !stop.load(Ordering::Relaxed) {
- // Use PeekMessage with a timeout to allow checking stop flag
if GetMessageW(&mut msg, HWND::default(), 0, 0).as_bool() {
let _ = TranslateMessage(&msg);
DispatchMessageW(&msg);
}
- // Check for text buffer flush
+ // Check for text buffer flush (runs on timer tick and after every message)
HOOK_STATE.with(|state| {
// Use try_borrow_mut to avoid panic — LL hook callbacks can
// fire synchronously during DispatchMessageW above
@@ -1402,4 +1406,68 @@ mod tests {
assert_eq!(0x04, 4); // Alt
assert_eq!(0x08, 8); // Win
}
+
+ fn make_test_state(tx: tokio::sync::mpsc::Sender<UiEvent>, text: &str) -> HookState {
+ HookState {
+ tx,
+ start: std::time::Instant::now(),
+ config: crate::config::UiCaptureConfig::default(),
+ last_mouse_pos: (0, 0),
+ text_buf: text.to_string(),
+ last_text_time: if text.is_empty() { None } else { Some(std::time::Instant::now()) },
+ current_app: Arc::new(parking_lot::Mutex::new(Some("test".into()))),
+ current_window: Arc::new(parking_lot::Mutex::new(Some("test window".into()))),
+ activity_feed: None,
+ click_queue: Arc::new(parking_lot::Mutex::new(Vec::new())),
+ focused_element: Arc::new(parking_lot::Mutex::new(None)),
+ }
+ }
+
+ #[test]
+ fn test_flush_text_buffer() {
+ let (tx, mut rx) = tokio::sync::mpsc::channel(64);
+ let mut state = make_test_state(tx, "hello world");
+
+ // Buffer has content — flush should send a Text event
+ flush_text_buffer(&mut state);
+ assert!(state.text_buf.is_empty());
+ assert!(state.last_text_time.is_none());
+
+ let event = rx.try_recv().unwrap();
+ match event.data {
+ EventData::Text { ref content, .. } => {
+ assert_eq!(content, "hello world");
+ }
+ _ => panic!("expected Text event, got {:?}", event.data),
+ }
+ }
+
+ #[test]
+ fn test_flush_empty_buffer_is_noop() {
+ let (tx, mut rx) = tokio::sync::mpsc::channel(64);
+ let mut state = make_test_state(tx, "");
+
+ flush_text_buffer(&mut state);
+ assert!(rx.try_recv().is_err()); // No event sent
+ }
+
+ #[test]
+ fn test_vk_to_char_punctuation() {
+ assert_eq!(vk_to_char(0xBA, 0), Some(';'));
+ assert_eq!(vk_to_char(0xBA, 1), Some(':'));
+ assert_eq!(vk_to_char(0xBE, 0), Some('.'));
+ assert_eq!(vk_to_char(0xBF, 0), Some('/'));
+ assert_eq!(vk_to_char(0xBF, 1), Some('?'));
+ assert_eq!(vk_to_char(0x0D, 0), Some('\n')); // Enter
+ assert_eq!(vk_to_char(0x08, 0), Some('\x08')); // Backspace
+ }
+
+ #[test]
+ fn test_vk_to_char_unknown_returns_none() {
+ // F1-F12 and other non-printable keys should return None
+ assert_eq!(vk_to_char(0x70, 0), None); // F1
+ assert_eq!(vk_to_char(0x7B, 0), None); // F12
+ assert_eq!(vk_to_char(0x2E, 0), None); // Delete
+ assert_eq!(vk_to_char(0x25, 0), None); // Left arrow
+ }
}