Skip to content
This repository was archived by the owner on Jan 13, 2026. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
116 changes: 116 additions & 0 deletions native/text-writer/src/keyboard_layout.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
//! Keyboard layout utilities for macOS
//!
//! Uses TIS (Text Input Source) APIs to dynamically determine keycodes
//! for the current keyboard layout, enabling layout-independent shortcuts.

use core_foundation::base::{CFRelease, TCFType};
use core_foundation::data::CFData;
use std::collections::HashMap;
use std::sync::OnceLock;

/// Cached keycode map - built once on first access
static KEYCODE_MAP: OnceLock<HashMap<char, u16>> = OnceLock::new();

// FFI declarations for Carbon/CoreServices APIs
#[link(name = "Carbon", kind = "framework")]
extern "C" {
fn TISCopyCurrentKeyboardLayoutInputSource() -> *mut std::ffi::c_void;
fn TISGetInputSourceProperty(
input_source: *const std::ffi::c_void,
property_key: *const std::ffi::c_void,
) -> *const std::ffi::c_void;
fn LMGetKbdType() -> u32;
static kTISPropertyUnicodeKeyLayoutData: *const std::ffi::c_void;
}

#[link(name = "CoreServices", kind = "framework")]
extern "C" {
fn UCKeyTranslate(
key_layout_ptr: *const u8,
virtual_key_code: u16,
key_action: u16,
modifier_key_state: u32,
keyboard_type: u32,
key_translate_options: u32,
dead_key_state: *mut u32,
max_string_length: usize,
actual_string_length: *mut usize,
unicode_string: *mut u16,
) -> i32;
}

const KUC_KEY_ACTION_DISPLAY: u16 = 3;
const KUC_KEY_TRANSLATE_NO_DEAD_KEYS_BIT: u32 = 0;

/// Default QWERTY keycode for 'V' key
const QWERTY_V_KEYCODE: u16 = 9;

/// Build a lookup table mapping lowercase characters to their keycodes
/// for the current keyboard layout.
fn build_char_to_keycode_map() -> HashMap<char, u16> {
let mut map = HashMap::new();

unsafe {
let input_source = TISCopyCurrentKeyboardLayoutInputSource();
if input_source.is_null() {
return map;
}

let layout_data_ref =
TISGetInputSourceProperty(input_source, kTISPropertyUnicodeKeyLayoutData);

if layout_data_ref.is_null() {
CFRelease(input_source);
return map;
}

// Wrap the CFData without retaining (it's owned by input_source)
let layout_data: CFData = TCFType::wrap_under_get_rule(layout_data_ref as *const _);
let layout_ptr = layout_data.bytes().as_ptr();
let kbd_type = LMGetKbdType();

// Iterate through keycodes 0-127 to build reverse lookup
for keycode in 0u16..128 {
let mut dead_key_state: u32 = 0;
let mut char_buf: [u16; 4] = [0; 4];
let mut actual_len: usize = 0;

let result = UCKeyTranslate(
layout_ptr,
keycode,
KUC_KEY_ACTION_DISPLAY,
0, // no modifiers
kbd_type,
KUC_KEY_TRANSLATE_NO_DEAD_KEYS_BIT,
&mut dead_key_state,
char_buf.len(),
&mut actual_len,
char_buf.as_mut_ptr(),
);

if result == 0 && actual_len == 1 {
if let Some(ch) = char::from_u32(u32::from(char_buf[0])) {
// Store lowercase version, prefer lower keycodes
map.entry(ch.to_ascii_lowercase()).or_insert(keycode);
}
}
}

CFRelease(input_source);
}

map
}

/// Get the keycode for a character in the current keyboard layout.
/// Returns None if the character cannot be found.
pub fn keycode_for_char(ch: char) -> Option<u16> {
let map = KEYCODE_MAP.get_or_init(build_char_to_keycode_map);
map.get(&ch.to_ascii_lowercase()).copied()
}

/// Get the keycode for 'v' in the current keyboard layout.
/// Falls back to QWERTY keycode (9) if lookup fails.
pub fn get_paste_keycode() -> u16 {
keycode_for_char('v').unwrap_or(QWERTY_V_KEYCODE)
}
10 changes: 7 additions & 3 deletions native/text-writer/src/macos_writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ use core_graphics::event_source::{CGEventSource, CGEventSourceStateID};
use std::thread;
use std::time::Duration;

use crate::keyboard_layout;

/// Type text on macOS using clipboard paste approach
/// This avoids character-by-character typing which can cause issues in some
/// apps
Expand Down Expand Up @@ -51,11 +53,13 @@ pub fn type_text_macos(text: &str, _char_delay: u64) -> Result<(), String> {
let source = CGEventSource::new(CGEventSourceStateID::CombinedSessionState)
.map_err(|_| "Failed to create event source")?;

// Get layout-aware keycode for 'v' (works with Dvorak, Colemak, etc.)
let v_keycode = keyboard_layout::get_paste_keycode();

// Simulate Cmd+V (paste)
// Key code 9 is 'V' key
let key_v_down = CGEvent::new_keyboard_event(source.clone(), 9, true)
let key_v_down = CGEvent::new_keyboard_event(source.clone(), v_keycode, true)
.map_err(|_| "Failed to create key down event")?;
let key_v_up = CGEvent::new_keyboard_event(source.clone(), 9, false)
let key_v_up = CGEvent::new_keyboard_event(source.clone(), v_keycode, false)
.map_err(|_| "Failed to create key up event")?;

// Set the Command modifier flag
Expand Down
2 changes: 2 additions & 0 deletions native/text-writer/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ use std::time::Duration;
#[cfg(target_os = "linux")]
use enigo::{Enigo, Key, Keyboard, Settings};

#[cfg(target_os = "macos")]
mod keyboard_layout;
#[cfg(target_os = "macos")]
mod macos_writer;
#[cfg(target_os = "macos")]
Expand Down