mirror of
https://github.com/bitwarden/browser
synced 2025-12-11 22:03:36 +00:00
[PM-22790] Adds SendInput() Functionality to the Autotype Crate (#15751)
* [PM-22790] Adds SendInput() functionality to the autotype crate * [PM-22790] Fixes unused variable linting errors * [PM-22790] Updated autotype lib.rs comment
This commit is contained in:
@@ -10,3 +10,13 @@ mod windowing;
|
||||
pub fn get_foreground_window_title() -> std::result::Result<String, ()> {
|
||||
windowing::get_foreground_window_title()
|
||||
}
|
||||
|
||||
/// Attempts to type the input text wherever the user's cursor is.
|
||||
///
|
||||
/// `input` must be an array of utf-16 encoded characters to insert.
|
||||
///
|
||||
/// TODO: The error handling will be improved in a future PR: PM-23615
|
||||
#[allow(clippy::result_unit_err)]
|
||||
pub fn type_input(input: Vec<u16>) -> std::result::Result<(), ()> {
|
||||
windowing::type_input(input)
|
||||
}
|
||||
|
||||
@@ -1,3 +1,7 @@
|
||||
pub fn get_foreground_window_title() -> std::result::Result<String, ()> {
|
||||
todo!("Bitwarden does not yet support Linux autotype");
|
||||
}
|
||||
|
||||
pub fn type_input(_input: Vec<u16>) -> std::result::Result<(), ()> {
|
||||
todo!("Bitwarden does not yet support Linux autotype");
|
||||
}
|
||||
|
||||
@@ -1,3 +1,7 @@
|
||||
pub fn get_foreground_window_title() -> std::result::Result<String, ()> {
|
||||
todo!("Bitwarden does not yet support Mac OS autotype");
|
||||
}
|
||||
|
||||
pub fn type_input(_input: Vec<u16>) -> std::result::Result<(), ()> {
|
||||
todo!("Bitwarden does not yet support Mac OS autotype");
|
||||
}
|
||||
|
||||
@@ -1,7 +1,11 @@
|
||||
use std::ffi::OsString;
|
||||
use std::os::windows::ffi::OsStringExt;
|
||||
|
||||
use windows::Win32::Foundation::HWND;
|
||||
use windows::Win32::Foundation::{GetLastError, HWND};
|
||||
use windows::Win32::UI::Input::KeyboardAndMouse::{
|
||||
BlockInput, SendInput, INPUT, INPUT_0, INPUT_KEYBOARD, KEYBDINPUT, KEYEVENTF_KEYUP,
|
||||
KEYEVENTF_UNICODE,
|
||||
};
|
||||
use windows::Win32::UI::WindowsAndMessaging::{
|
||||
GetForegroundWindow, GetWindowTextLengthW, GetWindowTextW,
|
||||
};
|
||||
@@ -18,6 +22,29 @@ pub fn get_foreground_window_title() -> std::result::Result<String, ()> {
|
||||
Ok(window_title)
|
||||
}
|
||||
|
||||
/// Attempts to type the input text wherever the user's cursor is.
|
||||
///
|
||||
/// `input` must be an array of utf-16 encoded characters to insert.
|
||||
///
|
||||
/// https://learn.microsoft.com/en-in/windows/win32/api/winuser/nf-winuser-sendinput
|
||||
pub fn type_input(input: Vec<u16>) -> Result<(), ()> {
|
||||
let mut keyboard_inputs: Vec<INPUT> = Vec::new();
|
||||
|
||||
for i in input {
|
||||
let next_down_input = build_input(InputKeyPress::Down, i);
|
||||
let next_up_input = build_input(InputKeyPress::Up, i);
|
||||
|
||||
keyboard_inputs.push(next_down_input);
|
||||
keyboard_inputs.push(next_up_input);
|
||||
}
|
||||
|
||||
let _ = block_input(true);
|
||||
let result = send_input(keyboard_inputs);
|
||||
let _ = block_input(false);
|
||||
|
||||
result
|
||||
}
|
||||
|
||||
/// Gets the foreground window handle.
|
||||
///
|
||||
/// https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-getforegroundwindow
|
||||
@@ -33,8 +60,6 @@ fn get_foreground_window() -> Result<HWND, ()> {
|
||||
|
||||
/// Gets the length of the window title bar text.
|
||||
///
|
||||
/// TODO: Future improvement is to use GetLastError for better error handling
|
||||
///
|
||||
/// https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-getwindowtextlengthw
|
||||
fn get_window_title_length(window_handle: HWND) -> Result<usize, ()> {
|
||||
if window_handle.is_invalid() {
|
||||
@@ -49,8 +74,6 @@ fn get_window_title_length(window_handle: HWND) -> Result<usize, ()> {
|
||||
|
||||
/// Gets the window title bar title.
|
||||
///
|
||||
/// TODO: Future improvement is to use GetLastError for better error handling
|
||||
///
|
||||
/// https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-getwindowtextw
|
||||
fn get_window_title(window_handle: HWND) -> Result<Option<String>, ()> {
|
||||
if window_handle.is_invalid() {
|
||||
@@ -73,3 +96,71 @@ fn get_window_title(window_handle: HWND) -> Result<Option<String>, ()> {
|
||||
|
||||
Ok(Some(window_title.to_string_lossy().into_owned()))
|
||||
}
|
||||
|
||||
/// Used in build_input() to specify if an input key is being pressed (down) or released (up).
|
||||
enum InputKeyPress {
|
||||
Down,
|
||||
Up,
|
||||
}
|
||||
|
||||
/// A function for easily building keyboard INPUT structs used in SendInput().
|
||||
///
|
||||
/// Before modifying this function, make sure you read the SendInput() documentation:
|
||||
/// https://learn.microsoft.com/en-in/windows/win32/api/winuser/nf-winuser-sendinput
|
||||
fn build_input(key_press: InputKeyPress, character: u16) -> INPUT {
|
||||
match key_press {
|
||||
InputKeyPress::Down => INPUT {
|
||||
r#type: INPUT_KEYBOARD,
|
||||
Anonymous: INPUT_0 {
|
||||
ki: KEYBDINPUT {
|
||||
wVk: Default::default(),
|
||||
wScan: character,
|
||||
dwFlags: KEYEVENTF_UNICODE,
|
||||
time: 0,
|
||||
dwExtraInfo: 0,
|
||||
},
|
||||
},
|
||||
},
|
||||
InputKeyPress::Up => INPUT {
|
||||
r#type: INPUT_KEYBOARD,
|
||||
Anonymous: INPUT_0 {
|
||||
ki: KEYBDINPUT {
|
||||
wVk: Default::default(),
|
||||
wScan: character,
|
||||
dwFlags: KEYEVENTF_KEYUP | KEYEVENTF_UNICODE,
|
||||
time: 0,
|
||||
dwExtraInfo: 0,
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
/// Block keyboard and mouse input events. This prevents the hotkey
|
||||
/// key presses from interfering with the input sent via SendInput().
|
||||
///
|
||||
/// https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-blockinput
|
||||
fn block_input(block: bool) -> Result<(), ()> {
|
||||
match unsafe { BlockInput(block) } {
|
||||
Ok(()) => Ok(()),
|
||||
Err(_) => Err(()),
|
||||
}
|
||||
}
|
||||
|
||||
/// Attempts to type the provided input wherever the user's cursor is.
|
||||
///
|
||||
/// https://learn.microsoft.com/en-in/windows/win32/api/winuser/nf-winuser-sendinput
|
||||
fn send_input(inputs: Vec<INPUT>) -> Result<(), ()> {
|
||||
let insert_count = unsafe { SendInput(&inputs, std::mem::size_of::<INPUT>() as i32) };
|
||||
|
||||
let e = unsafe { GetLastError().to_hresult().message() };
|
||||
println!("type_input() called, GetLastError() is: {:?}", e);
|
||||
|
||||
if insert_count == 0 {
|
||||
return Err(()); // input was blocked by another thread
|
||||
} else if insert_count != inputs.len() as u32 {
|
||||
return Err(()); // input insertion not completed
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
1
apps/desktop/desktop_native/napi/index.d.ts
vendored
1
apps/desktop/desktop_native/napi/index.d.ts
vendored
@@ -210,4 +210,5 @@ export declare namespace logging {
|
||||
}
|
||||
export declare namespace autotype {
|
||||
export function getForegroundWindowTitle(): string
|
||||
export function typeInput(input: Array<number>): void
|
||||
}
|
||||
|
||||
@@ -872,8 +872,15 @@ pub mod autotype {
|
||||
pub fn get_foreground_window_title() -> napi::Result<String, napi::Status> {
|
||||
autotype::get_foreground_window_title().map_err(|_| {
|
||||
napi::Error::from_reason(
|
||||
"Autotype Error: faild to get foreground window title".to_string(),
|
||||
"Autotype Error: failed to get foreground window title".to_string(),
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
#[napi]
|
||||
pub fn type_input(input: Vec<u16>) -> napi::Result<(), napi::Status> {
|
||||
autotype::type_input(input).map_err(|_| {
|
||||
napi::Error::from_reason("Autotype Error: failed to type input".to_string())
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user