Add cursor position implementation for MacOS

This commit is contained in:
Federico Terzi 2019-10-23 19:13:21 +02:00
parent 9bbdb8d95f
commit 4f9be699ac
4 changed files with 36 additions and 19 deletions

View File

@ -60,6 +60,11 @@ void send_string(const char * string);
*/ */
void send_vkey(int32_t vk); 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. * Send the backspace keypress, *count* times.
*/ */

View File

@ -101,23 +101,7 @@ void send_string(const char * string) {
} }
void delete_string(int32_t count) { void delete_string(int32_t count) {
dispatch_async(dispatch_get_main_queue(), ^(void) { send_multi_vkey(0x33, count);
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);
}
});
} }
void send_vkey(int32_t vk) { void send_vkey(int32_t vk) {
@ -127,14 +111,34 @@ void send_vkey(int32_t vk) {
CGEventPost(kCGHIDEventTap, keydown); CGEventPost(kCGHIDEventTap, keydown);
CFRelease(keydown); CFRelease(keydown);
usleep(2000); usleep(500);
CGEventRef keyup; CGEventRef keyup;
keyup = CGEventCreateKeyboardEvent(NULL, vk, false); keyup = CGEventCreateKeyboardEvent(NULL, vk, false);
CGEventPost(kCGHIDEventTap, keyup); CGEventPost(kCGHIDEventTap, keyup);
CFRelease(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);
}
}); });
} }

View File

@ -54,6 +54,7 @@ extern {
pub fn send_string(string: *const c_char); pub fn send_string(string: *const c_char);
pub fn send_vkey(vk: i32); pub fn send_vkey(vk: i32);
pub fn send_multi_vkey(vk: i32, count: i32);
pub fn delete_string(count: i32); pub fn delete_string(count: i32);
pub fn trigger_paste(); pub fn trigger_paste();
} }

View File

@ -48,4 +48,11 @@ impl super::KeyboardManager for MacKeyboardManager {
fn delete_string(&self, count: i32) { fn delete_string(&self, count: i32) {
unsafe {delete_string(count)} unsafe {delete_string(count)}
} }
fn move_cursor_left(&self, count: i32) {
unsafe {
// Simulate the Left arrow count times
send_multi_vkey(0x7B, count);
}
}
} }