diff --git a/native/liblinuxbridge/bridge.cpp b/native/liblinuxbridge/bridge.cpp index fdaa432..88183e8 100644 --- a/native/liblinuxbridge/bridge.cpp +++ b/native/liblinuxbridge/bridge.cpp @@ -278,6 +278,12 @@ void delete_string(int32_t count) { } } +void left_arrow(int32_t count) { + for (int i = 0; i vec; - - for (int i = 0; i < count; i++) { - INPUT input = { 0 }; - - input.type = INPUT_KEYBOARD; - input.ki.wScan = 0; - input.ki.time = 0; - input.ki.dwExtraInfo = 0; - input.ki.wVk = VK_BACK; - input.ki.dwFlags = 0; // 0 for key press - vec.push_back(input); - - input.ki.dwFlags = KEYEVENTF_KEYUP; // KEYEVENTF_KEYUP for key release - vec.push_back(input); - } - - SendInput(vec.size(), vec.data(), sizeof(INPUT)); + send_multi_vkey(VK_BACK, count); } void send_vkey(int32_t vk) { @@ -492,6 +475,27 @@ void send_vkey(int32_t vk) { SendInput(vec.size(), vec.data(), sizeof(INPUT)); } +void send_multi_vkey(int32_t vk, int32_t count) { + std::vector vec; + + for (int i = 0; i < count; i++) { + INPUT input = { 0 }; + + input.type = INPUT_KEYBOARD; + input.ki.wScan = 0; + input.ki.time = 0; + input.ki.dwExtraInfo = 0; + input.ki.wVk = vk; + input.ki.dwFlags = 0; // 0 for key press + vec.push_back(input); + + input.ki.dwFlags = KEYEVENTF_KEYUP; // KEYEVENTF_KEYUP for key release + vec.push_back(input); + } + + SendInput(vec.size(), vec.data(), sizeof(INPUT)); +} + void trigger_paste() { std::vector vec; diff --git a/native/libwinbridge/bridge.h b/native/libwinbridge/bridge.h index fc0a80c..85d86ad 100644 --- a/native/libwinbridge/bridge.h +++ b/native/libwinbridge/bridge.h @@ -64,6 +64,11 @@ extern "C" void send_string(const wchar_t * string); */ extern "C" void send_vkey(int32_t vk); +/* + * Send the given Virtual Key press multiple times + */ +extern "C" void send_multi_vkey(int32_t vk, int32_t count); + /* * Send the backspace keypress, *count* times. */ diff --git a/src/bridge/linux.rs b/src/bridge/linux.rs index d85156b..f93efbb 100644 --- a/src/bridge/linux.rs +++ b/src/bridge/linux.rs @@ -39,6 +39,7 @@ extern { pub fn send_string(string: *const c_char); pub fn delete_string(count: i32); + pub fn left_arrow(count: i32); pub fn trigger_paste(); pub fn trigger_terminal_paste(); } \ No newline at end of file diff --git a/src/bridge/macos.rs b/src/bridge/macos.rs index 40f2ee8..0b09101 100644 --- a/src/bridge/macos.rs +++ b/src/bridge/macos.rs @@ -55,6 +55,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/bridge/windows.rs b/src/bridge/windows.rs index 349230c..db9833e 100644 --- a/src/bridge/windows.rs +++ b/src/bridge/windows.rs @@ -55,6 +55,7 @@ extern { pub fn eventloop(); pub fn send_string(string: *const u16); 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/engine.rs b/src/engine.rs index 4376c52..bc604c1 100644 --- a/src/engine.rs +++ b/src/engine.rs @@ -160,6 +160,25 @@ impl <'a, S: KeyboardManager, C: ClipboardManager, M: ConfigManager<'a>, U: UIMa // Convert Windows style newlines into unix styles target_string = target_string.replace("\r\n", "\n"); + // Calculate cursor rewind moves if a Cursor Hint is present + let index = target_string.find("$|$"); + let cursor_rewind = if let Some(index) = index { + // Convert the byte index to a char index + let char_str = &target_string[0..index]; + let char_index = char_str.chars().count(); + let total_size = target_string.chars().count(); + + // Remove the $|$ placeholder + target_string = target_string.replace("$|$", ""); + + // Calculate the amount of rewind moves needed (LEFT ARROW). + // Subtract also 3, equal to the number of chars of the placeholder "$|$" + let moves = (total_size - char_index - 3) as i32; + Some(moves) + }else{ + None + }; + match config.backend { BackendType::Inject => { // Send the expected string. On linux, newlines are managed automatically @@ -185,6 +204,11 @@ impl <'a, S: KeyboardManager, C: ClipboardManager, M: ConfigManager<'a>, U: UIMa self.keyboard_manager.trigger_paste(); }, } + + if let Some(moves) = cursor_rewind { + // Simulate left arrow key presses to bring the cursor into the desired position + self.keyboard_manager.move_cursor_left(moves); + } } fn on_enable_update(&self, status: bool) { diff --git a/src/keyboard/linux.rs b/src/keyboard/linux.rs index 4b9f15b..7b30028 100644 --- a/src/keyboard/linux.rs +++ b/src/keyboard/linux.rs @@ -53,4 +53,10 @@ impl super::KeyboardManager for LinuxKeyboardManager { fn delete_string(&self, count: i32) { unsafe {delete_string(count)} } + + fn move_cursor_left(&self, count: i32) { + unsafe { + left_arrow(count); + } + } } \ 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 diff --git a/src/keyboard/mod.rs b/src/keyboard/mod.rs index c48134c..5a9cc5b 100644 --- a/src/keyboard/mod.rs +++ b/src/keyboard/mod.rs @@ -31,6 +31,7 @@ pub trait KeyboardManager { fn send_enter(&self); fn trigger_paste(&self); fn delete_string(&self, count: i32); + fn move_cursor_left(&self, count: i32); } // WINDOWS IMPLEMENTATION diff --git a/src/keyboard/windows.rs b/src/keyboard/windows.rs index df3564d..8193006 100644 --- a/src/keyboard/windows.rs +++ b/src/keyboard/windows.rs @@ -55,4 +55,11 @@ impl super::KeyboardManager for WindowsKeyboardManager { delete_string(count) } } + + fn move_cursor_left(&self, count: i32) { + unsafe { + // Send the left arrow key multiple times + send_multi_vkey(0x25, count) + } + } } \ No newline at end of file diff --git a/src/matcher/mod.rs b/src/matcher/mod.rs index 772f732..4667318 100644 --- a/src/matcher/mod.rs +++ b/src/matcher/mod.rs @@ -55,6 +55,10 @@ impl<'a> From<&'a AutoMatch> for Match{ static ref VAR_REGEX: Regex = Regex::new("\\{\\{\\s*(\\w+)\\s*\\}\\}").unwrap(); } + // TODO: may need to replace windows newline (\r\n) with newline only (\n) + + let new_replace = other.replace.clone(); + // Check if the match contains variables let has_vars = VAR_REGEX.is_match(&other.replace); @@ -70,7 +74,7 @@ impl<'a> From<&'a AutoMatch> for Match{ Self { trigger: other.trigger.clone(), - replace: other.replace.clone(), + replace: new_replace, vars: other.vars.clone(), word: other.word.clone(), _has_vars: has_vars,