Open source Loom alternative. Beautiful, shareable screen recordings.
Rust appcapcossloommacnextjsnextjs15open-sourceossreactrecordrustscreen-capturescreen-recorderscreenshotsolidjstauritauri-apptypescriptvite
feat: record keyboard presses alongside cursor in studio recording
Cursor Agent73b5d07eaa07
diff --git a/crates/recording/src/cursor.rs b/crates/recording/src/cursor.rsindex 796093d03..18b4fd305 100644--- a/crates/recording/src/cursor.rs+++ b/crates/recording/src/cursor.rs@@ -1,6 +1,8 @@use cap_cursor_capture::CursorCropBounds;use cap_cursor_info::CursorShape;-use cap_project::{CursorClickEvent, CursorEvents, CursorMoveEvent, XY};+use cap_project::{+ CursorClickEvent, CursorEvents, CursorMoveEvent, KeyPressEvent, KeyboardEvents, XY,+};use cap_timestamp::Timestamps;use futures::{FutureExt, future::Shared};use std::{@@ -23,11 +25,11 @@ pub type Cursors = HashMap<u64, Cursor>;#[derive(Clone)]pub struct CursorActorResponse {- // pub cursor_images: HashMap<String, Vec<u8>>,pub cursors: Cursors,pub next_cursor_id: u32,pub moves: Vec<CursorMoveEvent>,pub clicks: Vec<CursorClickEvent>,+ pub keyboard_presses: Vec<KeyPressEvent>,}pub struct CursorActor {@@ -59,6 +61,128 @@ fn flush_cursor_data(output_path: &Path, moves: &[CursorMoveEvent], clicks: &[Cu}}+fn flush_keyboard_data(output_path: &Path, presses: &[KeyPressEvent]) {+ let events = KeyboardEvents {+ presses: presses.to_vec(),+ };+ if let Ok(json) = serde_json::to_string_pretty(&events)+ && let Err(e) = std::fs::write(output_path, json)+ {+ tracing::error!(+ "Failed to write keyboard data to {}: {}",+ output_path.display(),+ e+ );+ }+}++fn keycode_to_string(key: &device_query::Keycode) -> (String, String) {+ use device_query::Keycode;+ let (display, code) = match key {+ Keycode::Key0 => ("0", "Key0"),+ Keycode::Key1 => ("1", "Key1"),+ Keycode::Key2 => ("2", "Key2"),+ Keycode::Key3 => ("3", "Key3"),+ Keycode::Key4 => ("4", "Key4"),+ Keycode::Key5 => ("5", "Key5"),+ Keycode::Key6 => ("6", "Key6"),+ Keycode::Key7 => ("7", "Key7"),+ Keycode::Key8 => ("8", "Key8"),+ Keycode::Key9 => ("9", "Key9"),+ Keycode::A => ("a", "A"),+ Keycode::B => ("b", "B"),+ Keycode::C => ("c", "C"),+ Keycode::D => ("d", "D"),+ Keycode::E => ("e", "E"),+ Keycode::F => ("f", "F"),+ Keycode::G => ("g", "G"),+ Keycode::H => ("h", "H"),+ Keycode::I => ("i", "I"),+ Keycode::J => ("j", "J"),+ Keycode::K => ("k", "K"),+ Keycode::L => ("l", "L"),+ Keycode::M => ("m", "M"),+ Keycode::N => ("n", "N"),+ Keycode::O => ("o", "O"),+ Keycode::P => ("p", "P"),+ Keycode::Q => ("q", "Q"),+ Keycode::R => ("r", "R"),+ Keycode::S => ("s", "S"),+ Keycode::T => ("t", "T"),+ Keycode::U => ("u", "U"),+ Keycode::V => ("v", "V"),+ Keycode::W => ("w", "W"),+ Keycode::X => ("x", "X"),+ Keycode::Y => ("y", "Y"),+ Keycode::Z => ("z", "Z"),+ Keycode::F1 => ("F1", "F1"),+ Keycode::F2 => ("F2", "F2"),+ Keycode::F3 => ("F3", "F3"),+ Keycode::F4 => ("F4", "F4"),+ Keycode::F5 => ("F5", "F5"),+ Keycode::F6 => ("F6", "F6"),+ Keycode::F7 => ("F7", "F7"),+ Keycode::F8 => ("F8", "F8"),+ Keycode::F9 => ("F9", "F9"),+ Keycode::F10 => ("F10", "F10"),+ Keycode::F11 => ("F11", "F11"),+ Keycode::F12 => ("F12", "F12"),+ Keycode::Escape => ("Escape", "Escape"),+ Keycode::Space => (" ", "Space"),+ Keycode::LControl => ("LControl", "LControl"),+ Keycode::RControl => ("RControl", "RControl"),+ Keycode::LShift => ("LShift", "LShift"),+ Keycode::RShift => ("RShift", "RShift"),+ Keycode::LAlt => ("LAlt", "LAlt"),+ Keycode::RAlt => ("RAlt", "RAlt"),+ Keycode::Meta => ("Meta", "Meta"),+ Keycode::Enter => ("Enter", "Enter"),+ Keycode::Up => ("Up", "Up"),+ Keycode::Down => ("Down", "Down"),+ Keycode::Left => ("Left", "Left"),+ Keycode::Right => ("Right", "Right"),+ Keycode::Backspace => ("Backspace", "Backspace"),+ Keycode::CapsLock => ("CapsLock", "CapsLock"),+ Keycode::Tab => ("Tab", "Tab"),+ Keycode::Home => ("Home", "Home"),+ Keycode::End => ("End", "End"),+ Keycode::PageUp => ("PageUp", "PageUp"),+ Keycode::PageDown => ("PageDown", "PageDown"),+ Keycode::Insert => ("Insert", "Insert"),+ Keycode::Delete => ("Delete", "Delete"),+ Keycode::Numpad0 => ("0", "Numpad0"),+ Keycode::Numpad1 => ("1", "Numpad1"),+ Keycode::Numpad2 => ("2", "Numpad2"),+ Keycode::Numpad3 => ("3", "Numpad3"),+ Keycode::Numpad4 => ("4", "Numpad4"),+ Keycode::Numpad5 => ("5", "Numpad5"),+ Keycode::Numpad6 => ("6", "Numpad6"),+ Keycode::Numpad7 => ("7", "Numpad7"),+ Keycode::Numpad8 => ("8", "Numpad8"),+ Keycode::Numpad9 => ("9", "Numpad9"),+ Keycode::NumpadSubtract => ("-", "NumpadSubtract"),+ Keycode::NumpadAdd => ("+", "NumpadAdd"),+ Keycode::NumpadDivide => ("/", "NumpadDivide"),+ Keycode::NumpadMultiply => ("*", "NumpadMultiply"),+ Keycode::Grave => ("`", "Grave"),+ Keycode::Minus => ("-", "Minus"),+ Keycode::Equal => ("=", "Equal"),+ Keycode::LeftBracket => ("[", "LeftBracket"),+ Keycode::RightBracket => ("]", "RightBracket"),+ Keycode::BackSlash => ("\\", "BackSlash"),+ Keycode::Semicolon => (";", "Semicolon"),+ Keycode::Apostrophe => ("'", "Apostrophe"),+ Keycode::Comma => (",", "Comma"),+ Keycode::Dot => (".", "Dot"),+ Keycode::Slash => ("/", "Slash"),+ _ => {+ let s = format!("{key:?}");+ return (s.clone(), s);+ }+ };+ (display.to_string(), code.to_string())+}+#[tracing::instrument(name = "cursor", skip_all)]pub fn spawn_cursor_recorder(crop_bounds: CursorCropBounds,@@ -68,6 +192,7 @@ pub fn spawn_cursor_recorder(next_cursor_id: u32,start_time: Timestamps,output_path: Option<PathBuf>,+ keyboard_output_path: Option<PathBuf>,) -> CursorActor {use cap_utils::spawn_actor;use device_query::{DeviceQuery, DeviceState};@@ -83,6 +208,7 @@ pub fn spawn_cursor_recorder(spawn_actor(async move {let device_state = DeviceState::new();let mut last_mouse_state = device_state.get_mouse();+ let mut last_keys: Vec<device_query::Keycode> = device_state.get_keys();let mut last_position = cap_cursor_capture::RawCursorPosition::get();@@ -93,6 +219,7 @@ pub fn spawn_cursor_recorder(next_cursor_id,moves: vec![],clicks: vec![],+ keyboard_presses: vec![],};let mut last_flush = Instant::now();@@ -203,10 +330,41 @@ pub fn spawn_cursor_recorder(last_mouse_state = mouse_state;+ let current_keys = device_state.get_keys();++ for key in ¤t_keys {+ if !last_keys.contains(key) {+ let (display, code) = keycode_to_string(key);+ response.keyboard_presses.push(KeyPressEvent {+ key: display,+ key_code: code,+ time_ms: elapsed,+ down: true,+ });+ }+ }++ for key in &last_keys {+ if !current_keys.contains(key) {+ let (display, code) = keycode_to_string(key);+ response.keyboard_presses.push(KeyPressEvent {+ key: display,+ key_code: code,+ time_ms: elapsed,+ down: false,+ });+ }+ }++ last_keys = current_keys;+if let Some(ref path) = output_path&& last_flush.elapsed() >= flush_interval{flush_cursor_data(path, &response.moves, &response.clicks);+ if let Some(ref kb_path) = keyboard_output_path {+ flush_keyboard_data(kb_path, &response.keyboard_presses);+ }last_flush = Instant::now();}}@@ -217,6 +375,10 @@ pub fn spawn_cursor_recorder(flush_cursor_data(path, &response.moves, &response.clicks);}+ if let Some(ref kb_path) = keyboard_output_path {+ flush_keyboard_data(kb_path, &response.keyboard_presses);+ }+let _ = tx.send(response);});diff --git a/crates/recording/src/studio_recording.rs b/crates/recording/src/studio_recording.rsindex 4d575f678..de526ac24 100644--- a/crates/recording/src/studio_recording.rs+++ b/crates/recording/src/studio_recording.rs@@ -100,6 +100,15 @@ impl Actor {})?,)?;+ if !res.keyboard_presses.is_empty() {+ std::fs::write(+ &cursor.keyboard_output_path,+ serde_json::to_string_pretty(&cap_project::KeyboardEvents {+ presses: res.keyboard_presses,+ })?,+ )?;+ }+(res.cursors, res.next_cursor_id)} else {(Default::default(), 0)@@ -419,6 +428,7 @@ impl Pipeline {struct CursorPipeline {output_path: PathBuf,+ keyboard_output_path: PathBuf,actor: CursorActor,}@@ -790,6 +800,12 @@ async fn stop_recording(.cursor.as_ref().map(|cursor| make_relative(&cursor.output_path)),+ keyboard: s+ .pipeline+ .cursor+ .as_ref()+ .filter(|cursor| cursor.keyboard_output_path.exists())+ .map(|cursor| make_relative(&cursor.keyboard_output_path)),}}).collect::<Vec<_>>()@@ -1226,11 +1242,17 @@ async fn create_segment_pipeline(.ok_or(CreateSegmentPipelineError::NoBounds)?;let cursor_output_path = dir.join("cursor.json");+ let keyboard_output_path = dir.join("keyboard.json");let incremental_output = if fragmented {Some(cursor_output_path.clone())} else {None};+ let keyboard_incremental_output = if fragmented {+ Some(keyboard_output_path.clone())+ } else {+ None+ };let cursor_display = cursor_display.ok_or(CreateSegmentPipelineError::NoDisplay)?;@@ -1242,10 +1264,12 @@ async fn create_segment_pipeline(next_cursors_id,start_time,incremental_output,+ keyboard_incremental_output,);Ok::<_, CreateSegmentPipelineError>(CursorPipeline {output_path: cursor_output_path,+ keyboard_output_path,actor: cursor,})})