1
0
mirror of https://github.com/bitwarden/browser synced 2026-01-07 11:03:30 +00:00

[PM-22758] Configurable Keyboard Shortcut for Autotype (#16613)

* [PM-22785] Initial push with configuration and ipc changes for the configurable autotype keyboard shortcut

* [PM-22785] Add messy code with working configurable hotkey

* [PM-22785] Add more messy rust code

* [PM-22785] Add temp changes with configurable hotkey ui

* Add shortcut display to settings

* [PM-22785] Logic updates. Ran npm run prettier and lint:fix.

* [PM-22785] Add back disableAutotype with refactors.

* [PM-22785] Clean up Rust code

* [PM-22785] Clean up Rust code v2

* [PM-22785] Add unicode bounds in Rust code

* [PM-22785] Update rust code comments

* [PM-22785] Add unicode_value byte length check post-encoding

* [PM-22785] Extract encoding to a separate function

* Various fixes for the autotype setting label

* Misc component fixes

* Disallow nunmbers and allow Win key

* Themify edit shortcut

* Change display of Super to Win

* Create autotype format method

* Autotpe modal cleanup

* [PM-22785] Some cleanup

* Add unit tests and adjust error handling

* [PM-22785] Fix build issues on Mac and Linux

* [PM-22785] Linting fix

* Remove unused message

* [PM-22785] Linting fix

* [PM-22785] More linting fix

* [PM-22785] Address initial PR comments

* [PM-22785] Comment change

* [PM-22785] If statement change

* [PM-22785] Update with fixes from PR comments

* [PM-22785] Update with fixes from PR comments version ?

* add unit tests for get_alphabetic_hot_key()

* Fix tests

* Add missing mock to tests

* [PM-22785] Update with small fixes via PR comments

---------

Co-authored-by: Robyn MacCallum <robyntmaccallum@gmail.com>
Co-authored-by: neuronull <9162534+neuronull@users.noreply.github.com>
This commit is contained in:
Colton Hurst
2025-09-29 10:20:15 -04:00
committed by GitHub
parent 018b4d5eb4
commit fc53eae4c5
18 changed files with 802 additions and 50 deletions

View File

@@ -0,0 +1,98 @@
import { defaultWindowsAutotypeKeyboardShortcut } from "../services/desktop-autotype.service";
/*
This class provides the following:
- A way to get and set an AutotypeKeyboardShortcut value within the main process
- A way to set an AutotypeKeyboardShortcut with validation
- A way to "get" the value in string array format or a single string format for electron
- Default shortcut support
This is currently only supported for Windows operating systems.
*/
export class AutotypeKeyboardShortcut {
private autotypeKeyboardShortcut: string[];
constructor() {
this.autotypeKeyboardShortcut = defaultWindowsAutotypeKeyboardShortcut;
}
/*
Returns a boolean value indicating if the autotypeKeyboardShortcut
was valid and set or not.
*/
set(newAutotypeKeyboardShortcut: string[]) {
if (!this.#keyboardShortcutIsValid(newAutotypeKeyboardShortcut)) {
return false;
}
this.autotypeKeyboardShortcut = newAutotypeKeyboardShortcut;
return true;
}
/*
Returns the autotype keyboard shortcut as a string array.
*/
getArrayFormat() {
return this.autotypeKeyboardShortcut;
}
/*
Returns the autotype keyboard shortcut as a single string, as
Electron expects. Please note this does not reorder the keys.
See Electron keyboard shorcut docs for more info:
https://www.electronjs.org/docs/latest/tutorial/keyboard-shortcuts
*/
getElectronFormat() {
return this.autotypeKeyboardShortcut.join("+");
}
/*
This private function validates the strArray input to make sure the array contains
valid, currently accepted shortcut keys for Windows.
Valid windows shortcut keys: Control, Alt, Super, Shift, letters A - Z
Valid macOS shortcut keys: Control, Alt, Command, Shift, letters A - Z (not yet supported)
See Electron keyboard shorcut docs for more info:
https://www.electronjs.org/docs/latest/tutorial/keyboard-shortcuts
*/
#keyboardShortcutIsValid(strArray: string[]) {
const VALID_SHORTCUT_CONTROL_KEYS: string[] = ["Control", "Alt", "Super", "Shift"];
const UNICODE_LOWER_BOUND = 65; // unicode 'A'
const UNICODE_UPPER_BOUND = 90; // unicode 'Z'
const MIN_LENGTH: number = 2;
const MAX_LENGTH: number = 3;
// Ensure strArray is a string array of valid length
if (
strArray === undefined ||
strArray === null ||
strArray.length < MIN_LENGTH ||
strArray.length > MAX_LENGTH
) {
return false;
}
// Ensure strArray is all modifier keys, and that the last key is a letter
for (let i = 0; i < strArray.length; i++) {
if (i < strArray.length - 1) {
if (!VALID_SHORTCUT_CONTROL_KEYS.includes(strArray[i])) {
return false;
}
} else {
const unicodeValue: number = strArray[i].charCodeAt(0);
if (
Number.isNaN(unicodeValue) ||
unicodeValue < UNICODE_LOWER_BOUND ||
unicodeValue > UNICODE_UPPER_BOUND
) {
return false;
}
}
}
return true;
}
}