tauri icon indicating copy to clipboard operation
tauri copied to clipboard

[feat] Expose `WindowEvent::KeyboardInput` in Rust

Open stijnfrishert opened this issue 3 months ago • 11 comments

Describe the problem

I want to be able to react to key events coming from the application event loop.

I am building a shortcut system for my Desktop app, on macOS. I would like to be able to trigger hotkeys, even if the application doesn't have any windows open. This rules out any js key listener solutions, because they are tied to windows. Plus, this feels like something that should be done over in Rust-land, anyway.

The current recommended solution is to use rdev. This works up to a point (see https://github.com/tauri-apps/tauri/issues/11670), but requires my app to ask for full access to all OS-level keyboard inputs (Accessibility), which is a permission I would rather not depend on.

In reality, this is something that should be solvable by propagating events from the event loop. I scoured through Tao a bit and found key event types, so this seems to be within the realm of possibility?

Describe the solution you'd like

I want to be able to register a key event handler on my app. I envision this would look something like this, but that's of course up for debate:

tauri::Builder::default()
    .on_key_event(|app, event|{ }))
    .run()

I can imagine plug-ins might want to do this too, so maybe we should have a system to register (and unregister) multiple handlers.

Alternatives considered

  • Binding to key event listeners in js, but this can only be done on windows. Besides, imho this is something to be solved on the Rust side, using the application event loop.

  • Using rdev, but this requires my app to ask for full keyboard monitoring on an OS-level, which is not a permission I want to ask for, really.

    In addition, people have to twiddle with device_event_filter, which is neither intuitive nor easy to find.

Additional context

I've searched through the Discord and issue list, and it seems more people have been requesting this.

stijnfrishert avatar Nov 13 '24 09:11 stijnfrishert