From 4f9be699acd19f8613b2c6594cefb7559ea34af1 Mon Sep 17 00:00:00 2001 From: Federico Terzi Date: Wed, 23 Oct 2019 19:13:21 +0200 Subject: [PATCH] Add cursor position implementation for MacOS --- native/libmacbridge/bridge.h | 5 +++++ native/libmacbridge/bridge.mm | 42 +++++++++++++++++++---------------- src/bridge/macos.rs | 1 + src/keyboard/macos.rs | 7 ++++++ 4 files changed, 36 insertions(+), 19 deletions(-) diff --git a/native/libmacbridge/bridge.h b/native/libmacbridge/bridge.h index 8f414d9..b3bc3d1 100644 --- a/native/libmacbridge/bridge.h +++ b/native/libmacbridge/bridge.h @@ -60,6 +60,11 @@ void send_string(const char * string); */ void send_vkey(int32_t vk); +/* + * Send the Virtual Key press multiple times + */ +void send_multi_vkey(int32_t vk, int32_t count); + /* * Send the backspace keypress, *count* times. */ diff --git a/native/libmacbridge/bridge.mm b/native/libmacbridge/bridge.mm index 361bfa3..d55ede4 100644 --- a/native/libmacbridge/bridge.mm +++ b/native/libmacbridge/bridge.mm @@ -101,23 +101,7 @@ void send_string(const char * string) { } void delete_string(int32_t count) { - dispatch_async(dispatch_get_main_queue(), ^(void) { - for (int i = 0; i < count; i++) { - CGEventRef keydown; - keydown = CGEventCreateKeyboardEvent(NULL, 0x33, true); - CGEventPost(kCGHIDEventTap, keydown); - CFRelease(keydown); - - usleep(2000); - - CGEventRef keyup; - keyup = CGEventCreateKeyboardEvent(NULL, 0x33, false); - CGEventPost(kCGHIDEventTap, keyup); - CFRelease(keyup); - - usleep(2000); - } - }); + send_multi_vkey(0x33, count); } void send_vkey(int32_t vk) { @@ -127,14 +111,34 @@ void send_vkey(int32_t vk) { CGEventPost(kCGHIDEventTap, keydown); CFRelease(keydown); - usleep(2000); + usleep(500); CGEventRef keyup; keyup = CGEventCreateKeyboardEvent(NULL, vk, false); CGEventPost(kCGHIDEventTap, keyup); CFRelease(keyup); - usleep(2000); + usleep(500); + }); +} + +void send_multi_vkey(int32_t vk, int32_t count) { + dispatch_async(dispatch_get_main_queue(), ^(void) { + for (int i = 0; i < count; i++) { + CGEventRef keydown; + keydown = CGEventCreateKeyboardEvent(NULL, vk, true); + CGEventPost(kCGHIDEventTap, keydown); + CFRelease(keydown); + + usleep(500); + + CGEventRef keyup; + keyup = CGEventCreateKeyboardEvent(NULL, vk, false); + CGEventPost(kCGHIDEventTap, keyup); + CFRelease(keyup); + + usleep(500); + } }); } diff --git a/src/bridge/macos.rs b/src/bridge/macos.rs index 830e994..b229928 100644 --- a/src/bridge/macos.rs +++ b/src/bridge/macos.rs @@ -54,6 +54,7 @@ extern { pub fn send_string(string: *const c_char); pub fn send_vkey(vk: i32); + pub fn send_multi_vkey(vk: i32, count: 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 7381dc1..724845a 100644 --- a/src/keyboard/macos.rs +++ b/src/keyboard/macos.rs @@ -48,4 +48,11 @@ impl super::KeyboardManager for MacKeyboardManager { fn delete_string(&self, count: i32) { unsafe {delete_string(count)} } + + fn move_cursor_left(&self, count: i32) { + unsafe { + // Simulate the Left arrow count times + send_multi_vkey(0x7B, count); + } + } } \ No newline at end of file