Wait until modifier keys are released after form on Windows. Fix #440

This commit is contained in:
Federico Terzi 2020-09-09 19:55:05 +02:00
parent fbfafe3564
commit 99f048fcd7
6 changed files with 49 additions and 4 deletions

View File

@ -663,6 +663,25 @@ void trigger_copy() {
SendInput(vec.size(), vec.data(), sizeof(INPUT));
}
int32_t are_modifiers_pressed() {
short ctrl_pressed = GetAsyncKeyState(VK_CONTROL);
short enter_pressed = GetAsyncKeyState(VK_RETURN);
short alt_pressed = GetAsyncKeyState(VK_MENU);
short shift_pressed = GetAsyncKeyState(VK_SHIFT);
short meta_pressed = GetAsyncKeyState(VK_LWIN);
short rmeta_pressed = GetAsyncKeyState(VK_RWIN);
if (((ctrl_pressed & 0x8000) +
(enter_pressed & 0x8000) +
(alt_pressed & 0x8000) +
(shift_pressed & 0x8000) +
(meta_pressed & 0x8000) +
(rmeta_pressed & 0x8000)) != 0) {
return 1;
}
return 0;
}
// SYSTEM

View File

@ -97,6 +97,11 @@ extern "C" void trigger_shift_paste();
*/
extern "C" void trigger_copy();
/*
* Check whether keyboard modifiers (CTRL, CMD, SHIFT, ecc) are pressed
*/
extern "C" int32_t are_modifiers_pressed();
// Detect current application commands
/*

View File

@ -69,6 +69,7 @@ extern "C" {
pub fn trigger_paste();
pub fn trigger_shift_paste();
pub fn trigger_copy();
pub fn are_modifiers_pressed() -> i32;
// PROCESSES

View File

@ -74,7 +74,7 @@ impl super::Extension for FormExtension {
.manager
.invoke(&["form", "-i", "-"], &serialized_config);
// On macOS, after the form closes we have to wait until the user releases the modifier keys
// On macOS and Windows, after the form closes we have to wait until the user releases the modifier keys
on_form_close();
if let Some(output) = output {
@ -95,9 +95,17 @@ impl super::Extension for FormExtension {
}
}
#[cfg(not(target_os = "macos"))]
#[cfg(target_os = "linux")]
fn on_form_close() {
// NOOP on Windows and Linux
// NOOP on Linux
}
#[cfg(target_os = "windows")]
fn on_form_close() {
let released = crate::keyboard::windows::wait_for_modifiers_release();
if !released {
warn!("Wait for modifiers release timed out! Please after closing the form, release your modifiers keys (CTRL, CMD, ALT, SHIFT)");
}
}
#[cfg(target_os = "macos")]

View File

@ -21,7 +21,7 @@ use crate::config::Configs;
use serde::{Deserialize, Serialize};
#[cfg(target_os = "windows")]
mod windows;
pub mod windows;
#[cfg(target_os = "linux")]
mod linux;

View File

@ -78,3 +78,15 @@ impl super::KeyboardManager for WindowsKeyboardManager {
}
}
}
pub fn wait_for_modifiers_release() -> bool {
let start = std::time::SystemTime::now();
while start.elapsed().unwrap_or_default().as_millis() < 3000 {
let pressed = unsafe { crate::bridge::windows::are_modifiers_pressed() };
if pressed == 0 {
return true;
}
std::thread::sleep(std::time::Duration::from_millis(100));
}
false
}