1
0
mirror of https://github.com/bitwarden/browser synced 2026-02-07 20:24:01 +00:00

PM-21845: Successful register_hotkey() call

This commit is contained in:
Colton Hurst
2025-05-29 11:17:55 -04:00
parent 01ed7ea340
commit 67e7641dea
11 changed files with 95 additions and 41 deletions

View File

@@ -366,8 +366,9 @@ checksum = "ace50bade8e6234aa140d9a2f552bbee1db4d353f69b8217bc503490fc1a9f26"
name = "autotype"
version = "0.0.0"
dependencies = [
"windows 0.61.1 (git+https://github.com/microsoft/windows-rs.git?rev=d09b4681de02560cf05bd3e57d7ea56b73f3b2f8)",
"windows 0.61.1 (git+https://github.com/microsoft/windows-rs.git?rev=bbe2bf3a5a020893e785ca79b0a09fc8770ba7e7)",
"windows-core 0.61.2",
"windows-result 0.3.4",
]
[[package]]
@@ -918,6 +919,7 @@ name = "desktop_napi"
version = "0.0.0"
dependencies = [
"anyhow",
"autotype",
"base64",
"desktop_core",
"hex",
@@ -3546,13 +3548,13 @@ dependencies = [
[[package]]
name = "windows"
version = "0.61.1"
source = "git+https://github.com/microsoft/windows-rs.git?rev=d09b4681de02560cf05bd3e57d7ea56b73f3b2f8#d09b4681de02560cf05bd3e57d7ea56b73f3b2f8"
source = "git+https://github.com/microsoft/windows-rs.git?rev=bbe2bf3a5a020893e785ca79b0a09fc8770ba7e7#bbe2bf3a5a020893e785ca79b0a09fc8770ba7e7"
dependencies = [
"windows-collections 0.2.0 (git+https://github.com/microsoft/windows-rs.git?rev=d09b4681de02560cf05bd3e57d7ea56b73f3b2f8)",
"windows-collections 0.2.0 (git+https://github.com/microsoft/windows-rs.git?rev=bbe2bf3a5a020893e785ca79b0a09fc8770ba7e7)",
"windows-core 0.61.2",
"windows-future 0.2.1",
"windows-link 0.1.1 (git+https://github.com/microsoft/windows-rs.git?rev=d09b4681de02560cf05bd3e57d7ea56b73f3b2f8)",
"windows-numerics 0.2.0 (git+https://github.com/microsoft/windows-rs.git?rev=d09b4681de02560cf05bd3e57d7ea56b73f3b2f8)",
"windows-link 0.1.1 (git+https://github.com/microsoft/windows-rs.git?rev=bbe2bf3a5a020893e785ca79b0a09fc8770ba7e7)",
"windows-numerics 0.2.0 (git+https://github.com/microsoft/windows-rs.git?rev=bbe2bf3a5a020893e785ca79b0a09fc8770ba7e7)",
]
[[package]]
@@ -3567,7 +3569,7 @@ dependencies = [
[[package]]
name = "windows-collections"
version = "0.2.0"
source = "git+https://github.com/microsoft/windows-rs.git?rev=d09b4681de02560cf05bd3e57d7ea56b73f3b2f8#d09b4681de02560cf05bd3e57d7ea56b73f3b2f8"
source = "git+https://github.com/microsoft/windows-rs.git?rev=bbe2bf3a5a020893e785ca79b0a09fc8770ba7e7#bbe2bf3a5a020893e785ca79b0a09fc8770ba7e7"
dependencies = [
"windows-core 0.61.2",
]
@@ -3600,11 +3602,11 @@ dependencies = [
[[package]]
name = "windows-core"
version = "0.61.2"
source = "git+https://github.com/microsoft/windows-rs.git?rev=d09b4681de02560cf05bd3e57d7ea56b73f3b2f8#d09b4681de02560cf05bd3e57d7ea56b73f3b2f8"
source = "git+https://github.com/microsoft/windows-rs.git?rev=bbe2bf3a5a020893e785ca79b0a09fc8770ba7e7#bbe2bf3a5a020893e785ca79b0a09fc8770ba7e7"
dependencies = [
"windows-implement 0.60.0 (git+https://github.com/microsoft/windows-rs.git?rev=d09b4681de02560cf05bd3e57d7ea56b73f3b2f8)",
"windows-interface 0.59.1 (git+https://github.com/microsoft/windows-rs.git?rev=d09b4681de02560cf05bd3e57d7ea56b73f3b2f8)",
"windows-link 0.1.1 (git+https://github.com/microsoft/windows-rs.git?rev=d09b4681de02560cf05bd3e57d7ea56b73f3b2f8)",
"windows-implement 0.60.0 (git+https://github.com/microsoft/windows-rs.git?rev=bbe2bf3a5a020893e785ca79b0a09fc8770ba7e7)",
"windows-interface 0.59.1 (git+https://github.com/microsoft/windows-rs.git?rev=bbe2bf3a5a020893e785ca79b0a09fc8770ba7e7)",
"windows-link 0.1.1 (git+https://github.com/microsoft/windows-rs.git?rev=bbe2bf3a5a020893e785ca79b0a09fc8770ba7e7)",
"windows-result 0.3.4",
"windows-strings 0.4.2",
]
@@ -3622,10 +3624,10 @@ dependencies = [
[[package]]
name = "windows-future"
version = "0.2.1"
source = "git+https://github.com/microsoft/windows-rs.git?rev=d09b4681de02560cf05bd3e57d7ea56b73f3b2f8#d09b4681de02560cf05bd3e57d7ea56b73f3b2f8"
source = "git+https://github.com/microsoft/windows-rs.git?rev=bbe2bf3a5a020893e785ca79b0a09fc8770ba7e7#bbe2bf3a5a020893e785ca79b0a09fc8770ba7e7"
dependencies = [
"windows-core 0.61.2",
"windows-link 0.1.1 (git+https://github.com/microsoft/windows-rs.git?rev=d09b4681de02560cf05bd3e57d7ea56b73f3b2f8)",
"windows-link 0.1.1 (git+https://github.com/microsoft/windows-rs.git?rev=bbe2bf3a5a020893e785ca79b0a09fc8770ba7e7)",
"windows-threading",
]
@@ -3654,7 +3656,7 @@ dependencies = [
[[package]]
name = "windows-implement"
version = "0.60.0"
source = "git+https://github.com/microsoft/windows-rs.git?rev=d09b4681de02560cf05bd3e57d7ea56b73f3b2f8#d09b4681de02560cf05bd3e57d7ea56b73f3b2f8"
source = "git+https://github.com/microsoft/windows-rs.git?rev=bbe2bf3a5a020893e785ca79b0a09fc8770ba7e7#bbe2bf3a5a020893e785ca79b0a09fc8770ba7e7"
dependencies = [
"proc-macro2",
"quote",
@@ -3686,7 +3688,7 @@ dependencies = [
[[package]]
name = "windows-interface"
version = "0.59.1"
source = "git+https://github.com/microsoft/windows-rs.git?rev=d09b4681de02560cf05bd3e57d7ea56b73f3b2f8#d09b4681de02560cf05bd3e57d7ea56b73f3b2f8"
source = "git+https://github.com/microsoft/windows-rs.git?rev=bbe2bf3a5a020893e785ca79b0a09fc8770ba7e7#bbe2bf3a5a020893e785ca79b0a09fc8770ba7e7"
dependencies = [
"proc-macro2",
"quote",
@@ -3702,7 +3704,7 @@ checksum = "76840935b766e1b0a05c0066835fb9ec80071d4c09a16f6bd5f7e655e3c14c38"
[[package]]
name = "windows-link"
version = "0.1.1"
source = "git+https://github.com/microsoft/windows-rs.git?rev=d09b4681de02560cf05bd3e57d7ea56b73f3b2f8#d09b4681de02560cf05bd3e57d7ea56b73f3b2f8"
source = "git+https://github.com/microsoft/windows-rs.git?rev=bbe2bf3a5a020893e785ca79b0a09fc8770ba7e7#bbe2bf3a5a020893e785ca79b0a09fc8770ba7e7"
[[package]]
name = "windows-numerics"
@@ -3717,10 +3719,10 @@ dependencies = [
[[package]]
name = "windows-numerics"
version = "0.2.0"
source = "git+https://github.com/microsoft/windows-rs.git?rev=d09b4681de02560cf05bd3e57d7ea56b73f3b2f8#d09b4681de02560cf05bd3e57d7ea56b73f3b2f8"
source = "git+https://github.com/microsoft/windows-rs.git?rev=bbe2bf3a5a020893e785ca79b0a09fc8770ba7e7#bbe2bf3a5a020893e785ca79b0a09fc8770ba7e7"
dependencies = [
"windows-core 0.61.2",
"windows-link 0.1.1 (git+https://github.com/microsoft/windows-rs.git?rev=d09b4681de02560cf05bd3e57d7ea56b73f3b2f8)",
"windows-link 0.1.1 (git+https://github.com/microsoft/windows-rs.git?rev=bbe2bf3a5a020893e785ca79b0a09fc8770ba7e7)",
]
[[package]]
@@ -3755,9 +3757,9 @@ dependencies = [
[[package]]
name = "windows-result"
version = "0.3.4"
source = "git+https://github.com/microsoft/windows-rs.git?rev=d09b4681de02560cf05bd3e57d7ea56b73f3b2f8#d09b4681de02560cf05bd3e57d7ea56b73f3b2f8"
source = "git+https://github.com/microsoft/windows-rs.git?rev=bbe2bf3a5a020893e785ca79b0a09fc8770ba7e7#bbe2bf3a5a020893e785ca79b0a09fc8770ba7e7"
dependencies = [
"windows-link 0.1.1 (git+https://github.com/microsoft/windows-rs.git?rev=d09b4681de02560cf05bd3e57d7ea56b73f3b2f8)",
"windows-link 0.1.1 (git+https://github.com/microsoft/windows-rs.git?rev=bbe2bf3a5a020893e785ca79b0a09fc8770ba7e7)",
]
[[package]]
@@ -3772,9 +3774,9 @@ dependencies = [
[[package]]
name = "windows-strings"
version = "0.4.2"
source = "git+https://github.com/microsoft/windows-rs.git?rev=d09b4681de02560cf05bd3e57d7ea56b73f3b2f8#d09b4681de02560cf05bd3e57d7ea56b73f3b2f8"
source = "git+https://github.com/microsoft/windows-rs.git?rev=bbe2bf3a5a020893e785ca79b0a09fc8770ba7e7#bbe2bf3a5a020893e785ca79b0a09fc8770ba7e7"
dependencies = [
"windows-link 0.1.1 (git+https://github.com/microsoft/windows-rs.git?rev=d09b4681de02560cf05bd3e57d7ea56b73f3b2f8)",
"windows-link 0.1.1 (git+https://github.com/microsoft/windows-rs.git?rev=bbe2bf3a5a020893e785ca79b0a09fc8770ba7e7)",
]
[[package]]
@@ -3829,9 +3831,9 @@ dependencies = [
[[package]]
name = "windows-threading"
version = "0.1.0"
source = "git+https://github.com/microsoft/windows-rs.git?rev=d09b4681de02560cf05bd3e57d7ea56b73f3b2f8#d09b4681de02560cf05bd3e57d7ea56b73f3b2f8"
source = "git+https://github.com/microsoft/windows-rs.git?rev=bbe2bf3a5a020893e785ca79b0a09fc8770ba7e7#bbe2bf3a5a020893e785ca79b0a09fc8770ba7e7"
dependencies = [
"windows-link 0.1.1 (git+https://github.com/microsoft/windows-rs.git?rev=d09b4681de02560cf05bd3e57d7ea56b73f3b2f8)",
"windows-link 0.1.1 (git+https://github.com/microsoft/windows-rs.git?rev=bbe2bf3a5a020893e785ca79b0a09fc8770ba7e7)",
]
[[package]]

View File

@@ -6,5 +6,6 @@ edition.workspace = true
publish.workspace = true
[dependencies]
windows = { git = "https://github.com/microsoft/windows-rs.git", rev = "d09b4681de02560cf05bd3e57d7ea56b73f3b2f8", features = ["Win32_UI_Input_KeyboardAndMouse"] }
windows-core = { git = "https://github.com/microsoft/windows-rs.git", rev = "d09b4681de02560cf05bd3e57d7ea56b73f3b2f8" }
windows = { git = "https://github.com/microsoft/windows-rs.git", rev = "bbe2bf3a5a020893e785ca79b0a09fc8770ba7e7", features = ["Win32_UI_Input_KeyboardAndMouse"] }
windows-core = { git = "https://github.com/microsoft/windows-rs.git", rev = "bbe2bf3a5a020893e785ca79b0a09fc8770ba7e7" }
windows-result = { git = "https://github.com/microsoft/windows-rs.git", rev = "bbe2bf3a5a020893e785ca79b0a09fc8770ba7e7" }

View File

@@ -1,4 +1,5 @@
use windows::Win32::UI::Input::KeyboardAndMouse::{RegisterHotKey, HOT_KEY_MODIFIERS, MOD_ALT};
use windows_result::*;
/*
A safe wrapper around the unsafe RegisterHotKey Win32 function.
@@ -6,23 +7,12 @@ use windows::Win32::UI::Input::KeyboardAndMouse::{RegisterHotKey, HOT_KEY_MODIFI
https://microsoft.github.io/windows-docs-rs/doc/windows/Win32/UI/Input/KeyboardAndMouse/fn.RegisterHotKey.html
https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-registerhotkey
*/
pub fn register_hotkey() -> std::result::Result<(), String> {
pub fn register_hotkey() -> std::result::Result<String, String> {
let r = unsafe { RegisterHotKey(None, 1, MOD_ALT, 42) }; // ALT + b
Ok(())
}
pub fn add(left: u64, right: u64) -> u64 {
left + right
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn it_works() {
let result = add(2, 2);
assert_eq!(result, 4);
if let windows_result::Result::Err(e) = r {
return std::result::Result::Err(e.message());
}
Ok(String::from("it works!"))
}

View File

@@ -30,6 +30,7 @@ tokio-stream = { workspace = true }
[target.'cfg(windows)'.dependencies]
windows-registry = { workspace = true }
windows_plugin_authenticator = { path = "../windows_plugin_authenticator" }
autotype = { path = "../autotype" }
[build-dependencies]
napi-build = { workspace = true }

View File

@@ -198,3 +198,6 @@ export declare namespace logging {
}
export function initNapiLog(jsLogFn: (err: Error | null, arg0: LogLevel, arg1: string) => any): void
}
export declare namespace autotype {
export function registerHotkey(): string
}

View File

@@ -0,0 +1,3 @@
pub fn register() -> std::result::Result<String, String> {
bail!("Not implemented")
}

View File

@@ -0,0 +1,4 @@
#[cfg_attr(target_os = "windows", path = "windows.rs")]
#[cfg_attr(not(target_os = "windows"), path = "dummy.rs")]
mod internal;
pub use internal::*;

View File

@@ -0,0 +1,3 @@
pub fn register_hotkey() -> std::result::Result<String, String> {
autotype::register_hotkey()
}

View File

@@ -1,6 +1,7 @@
#[macro_use]
extern crate napi_derive;
mod autotype_internal;
mod passkey_authenticator_internal;
mod registry;
@@ -875,3 +876,13 @@ pub mod logging {
fn flush(&self) {}
}
}
#[napi]
pub mod autotype {
#[napi]
pub fn register_hotkey() -> napi::Result<String, napi::Status> {
crate::autotype_internal::register_hotkey().map_err(|e| {
napi::Error::from_reason(e)
})
}
}

View File

@@ -3,6 +3,8 @@ import * as path from "path";
import { app } from "electron";
import { autotype } from "@bitwarden/desktop-napi"
if (
process.platform === "darwin" &&
process.argv.some((arg) => arg.indexOf("chrome-extension://") !== -1 || arg.indexOf("{") !== -1)
@@ -40,6 +42,10 @@ if (
// eslint-disable-next-line
const Main = require("./main").Main;
let r = autotype.registerHotkey();
console.log("registerHotkey(): ");
console.log(r);
const main = new Main();
main.bootstrap();
}

30
package-lock.json generated
View File

@@ -1018,6 +1018,36 @@
"webpack": "^5.1.0"
}
},
"node_modules/@angular-devkit/build-angular/node_modules/copy-webpack-plugin/node_modules/fast-glob": {
"version": "3.3.3",
"resolved": "https://registry.npmjs.org/fast-glob/-/fast-glob-3.3.3.tgz",
"integrity": "sha512-7MptL8U0cqcFdzIzwOTHoilX9x5BrNqye7Z/LuC7kCMRio1EMSyqRK3BEAUD7sXRq4iT4AzTVuZdhgQ2TCvYLg==",
"dev": true,
"license": "MIT",
"dependencies": {
"@nodelib/fs.stat": "^2.0.2",
"@nodelib/fs.walk": "^1.2.3",
"glob-parent": "^5.1.2",
"merge2": "^1.3.0",
"micromatch": "^4.0.8"
},
"engines": {
"node": ">=8.6.0"
}
},
"node_modules/@angular-devkit/build-angular/node_modules/copy-webpack-plugin/node_modules/fast-glob/node_modules/glob-parent": {
"version": "5.1.2",
"resolved": "https://registry.npmjs.org/glob-parent/-/glob-parent-5.1.2.tgz",
"integrity": "sha512-AOIgSQCepiJYwP3ARnGx+5VnTu2HBYdzbGP45eLw1vr3zB3vZLeyed1sC9hnbcOc9/SrMyM5RPQrkGz4aS9Zow==",
"dev": true,
"license": "ISC",
"dependencies": {
"is-glob": "^4.0.1"
},
"engines": {
"node": ">= 6"
}
},
"node_modules/@angular-devkit/build-angular/node_modules/define-lazy-prop": {
"version": "3.0.0",
"resolved": "https://registry.npmjs.org/define-lazy-prop/-/define-lazy-prop-3.0.0.tgz",