From 5bc8de24085446ee00420593b5c892a0cc046147 Mon Sep 17 00:00:00 2001 From: Federico Terzi Date: Mon, 9 Sep 2019 17:03:59 +0200 Subject: [PATCH] Add clipboard backend for MacOS. Fix #9 --- native/libmacbridge/bridge.h | 5 +++++ native/libmacbridge/bridge.mm | 32 ++++++++++++++++++++++++++++++++ src/bridge/macos.rs | 1 + src/keyboard/macos.rs | 4 +++- 4 files changed, 41 insertions(+), 1 deletion(-) diff --git a/native/libmacbridge/bridge.h b/native/libmacbridge/bridge.h index 6c9dcf9..83f6af7 100644 --- a/native/libmacbridge/bridge.h +++ b/native/libmacbridge/bridge.h @@ -44,6 +44,11 @@ void send_vkey(int32_t vk); */ void delete_string(int32_t count); +/* + * Trigger normal paste ( Pressing CMD+V ) + */ +void trigger_paste(); + // SYSTEM /* diff --git a/native/libmacbridge/bridge.mm b/native/libmacbridge/bridge.mm index b3325f3..156971b 100644 --- a/native/libmacbridge/bridge.mm +++ b/native/libmacbridge/bridge.mm @@ -99,6 +99,38 @@ void send_vkey(int32_t vk) { }); } +void trigger_paste() { + dispatch_async(dispatch_get_main_queue(), ^(void) { + CGEventRef keydown; + keydown = CGEventCreateKeyboardEvent(NULL, 0x37, true); // CMD + CGEventPost(kCGHIDEventTap, keydown); + CFRelease(keydown); + + usleep(2000); + + CGEventRef keydown2; + keydown2 = CGEventCreateKeyboardEvent(NULL, 0x09, true); // V key + CGEventPost(kCGHIDEventTap, keydown2); + CFRelease(keydown2); + + usleep(2000); + + CGEventRef keyup; + keyup = CGEventCreateKeyboardEvent(NULL, 0x09, false); + CGEventPost(kCGHIDEventTap, keyup); + CFRelease(keyup); + + usleep(2000); + + CGEventRef keyup2; + keyup2 = CGEventCreateKeyboardEvent(NULL, 0x37, false); // CMD + CGEventPost(kCGHIDEventTap, keyup2); + CFRelease(keyup2); + + usleep(2000); + }); +} + int32_t get_active_app_bundle(char * buffer, int32_t size) { NSRunningApplication *frontApp = [[NSWorkspace sharedWorkspace] frontmostApplication]; NSString *bundlePath = [frontApp bundleURL].path; diff --git a/src/bridge/macos.rs b/src/bridge/macos.rs index 40a0d03..9fc5374 100644 --- a/src/bridge/macos.rs +++ b/src/bridge/macos.rs @@ -20,4 +20,5 @@ extern { pub fn send_string(string: *const c_char); pub fn send_vkey(vk: i32); pub fn delete_string(count: i32); + pub fn trigger_paste(); } \ No newline at end of file diff --git a/src/keyboard/macos.rs b/src/keyboard/macos.rs index d6b4a3d..86afb44 100644 --- a/src/keyboard/macos.rs +++ b/src/keyboard/macos.rs @@ -44,7 +44,9 @@ impl super::KeyboardSender for MacKeyboardSender { } fn trigger_paste(&self) { - unimplemented!() + unsafe { + trigger_paste(); + } } fn delete_string(&self, count: i32) {