Add option to delay backspace presses on Windows. See #246

This commit is contained in:
Federico Terzi 2020-05-05 20:17:45 +02:00
parent 70260aa0b7
commit e29e5b755e
5 changed files with 40 additions and 6 deletions

View File

@ -495,8 +495,12 @@ void send_string(const wchar_t * string) {
/* /*
* Send the backspace keypress, *count* times. * Send the backspace keypress, *count* times.
*/ */
void delete_string(int32_t count) { void delete_string(int32_t count, int32_t delay) {
send_multi_vkey(VK_BACK, count); if (delay != 0) {
send_multi_vkey_with_delay(VK_BACK, count, delay);
}else{
send_multi_vkey(VK_BACK, count);
}
} }
void send_vkey(int32_t vk) { void send_vkey(int32_t vk) {
@ -539,6 +543,27 @@ void send_multi_vkey(int32_t vk, int32_t count) {
SendInput(vec.size(), vec.data(), sizeof(INPUT)); SendInput(vec.size(), vec.data(), sizeof(INPUT));
} }
void send_multi_vkey_with_delay(int32_t vk, int32_t count, int32_t delay) {
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
SendInput(1, &input, sizeof(INPUT));
Sleep(delay);
input.ki.dwFlags = KEYEVENTF_KEYUP; // KEYEVENTF_KEYUP for key release
SendInput(1, &input, sizeof(INPUT));
Sleep(delay);
}
}
void trigger_paste() { void trigger_paste() {
std::vector<INPUT> vec; std::vector<INPUT> vec;

View File

@ -72,10 +72,15 @@ extern "C" void send_vkey(int32_t vk);
*/ */
extern "C" void send_multi_vkey(int32_t vk, int32_t count); extern "C" void send_multi_vkey(int32_t vk, int32_t count);
/*
* Send the given Virtual Key press multiple times adding a delay between each keypress
*/
extern "C" void send_multi_vkey_with_delay(int32_t vk, int32_t count, int32_t delay);
/* /*
* Send the backspace keypress, *count* times. * Send the backspace keypress, *count* times.
*/ */
extern "C" void delete_string(int32_t count); extern "C" void delete_string(int32_t count, int32_t delay);
/* /*
* Send the Paste keyboard shortcut (CTRL+V) * Send the Paste keyboard shortcut (CTRL+V)

View File

@ -57,7 +57,7 @@ extern {
pub fn send_string(string: *const u16); pub fn send_string(string: *const u16);
pub fn send_vkey(vk: i32); pub fn send_vkey(vk: i32);
pub fn send_multi_vkey(vk: i32, count: i32); pub fn send_multi_vkey(vk: i32, count: i32);
pub fn delete_string(count: i32); pub fn delete_string(count: i32, delay: i32);
pub fn trigger_paste(); pub fn trigger_paste();
pub fn trigger_copy(); pub fn trigger_copy();
} }

View File

@ -62,6 +62,7 @@ fn default_passive_key() -> KeyModifier { KeyModifier::OFF }
fn default_enable_passive() -> bool { false } fn default_enable_passive() -> bool { false }
fn default_enable_active() -> bool { true } fn default_enable_active() -> bool { true }
fn default_backspace_limit() -> i32 { 3 } fn default_backspace_limit() -> i32 { 3 }
fn default_backspace_delay() -> i32 { 0 }
fn default_restore_clipboard_delay() -> i32 { 300 } fn default_restore_clipboard_delay() -> i32 { 300 }
fn default_exclude_default_entries() -> bool {false} fn default_exclude_default_entries() -> bool {false}
fn default_secure_input_watcher_enabled() -> bool {true} fn default_secure_input_watcher_enabled() -> bool {true}
@ -168,6 +169,9 @@ pub struct Configs {
#[serde(default = "default_fast_inject")] #[serde(default = "default_fast_inject")]
pub fast_inject: bool, pub fast_inject: bool,
#[serde(default = "default_backspace_delay")]
pub backspace_delay: i32,
#[serde(default = "default_matches")] #[serde(default = "default_matches")]
pub matches: Vec<Match>, pub matches: Vec<Match>,

View File

@ -62,9 +62,9 @@ impl super::KeyboardManager for WindowsKeyboardManager {
} }
} }
fn delete_string(&self, _: &Configs, count: i32) { fn delete_string(&self, config: &Configs, count: i32) {
unsafe { unsafe {
delete_string(count) delete_string(count, config.backspace_delay)
} }
} }