#How to listen for keyboard events in Tauri?

17 messages · Page 1 of 1 (latest)

full estuary
#

Is there a way to listen for key inputs that aren't global in Tauri? Everything I have found, aside from rdev listens for global key inputs and that is not what I want as they consume the events even when the app is not focussed. rdev seems to be recommended however it just doesn't work. It recieves mouse events when the Tauri app is focussed but no keyboard events. It's only once you focus a different application that rdev recieves keyboard events.

Is there any cross-platform way to listen for keyboard events only while the window is focussed? I find it odd that everything is global seeing as almost all the programs I have ever used do not listen globally and don't consume the key events so I find it odd that it's such a common use case within Tauri.

knotty bridge
#

and if rdev's platform specific notes are a problem the only fallback that comes to mind are the browser's key events (that you could forward to rust via tauri events)

full estuary
#

Setting it to Always has solved the issue. I guess I just have to manually ignore the events if the window is not focussed then?
And also, although the platform specific notes of rdev are not an issue, when it says on the DeviceEventFilter that it's unsupported on all platforms except windows does that mean I will have this issue on other platforms as well (and be unable to fix it)? or is this api only available as this issue only exists on windows?

full estuary
#

On a bit of a side note, would it be possible to use add_AcceleratorKeyPressed for the Windows webview when calling the window.with_webview in Tauri?
Since Tauri doesn't rexport the crates it uses internally I'm having some version mismatch issues.
The second argument to the function requires an EventRegistrationToken from windows version 0.52 since tao uses that version. Any other version results in an error:

mismatched types
`EventRegistrationToken` and `EventRegistrationToken` have similar names, but are actually distinct types
perhaps two different versions of crate `windows` are being used?

The first argument requires a (I believe) an AcceleratorKeyPressedEventHandler from webview2_com, however when using windows version 0.52 there is yet another version mismatch error:

the trait bound `webview2_com::Microsoft::Web::WebView2::Win32::ICoreWebView2AcceleratorKeyPressedEventHandler: windows_core::param::IntoParam<webview2_com_sys::Microsoft::Web::WebView2::Win32::ICoreWebView2AcceleratorKeyPressedEventHandler, windows_core::r#type::ReferenceType>` is not satisfied
perhaps two different versions of crate `windows_core` are being used?

This is solved by using windows version 0.54 since wry uses that version. Obviously, using both windows versions 0.52 and 0.54 seems silly. Is there a better solution?

knotty bridge
full estuary
full estuary
# knotty bridge Tao's windows version should never matter in with_webview so i'd be interested i...

so I added these to the Cargo.toml:

webview2-com = "0.29.0"
windows = { version = "0.54", features = ["Win32", "Win32_System", "Win32_System_WinRT"]}

and the code is:

window.with_webview(|webview| unsafe {
    let token: *mut windows::Win32::System::WinRT::EventRegistrationToken =
        std::ptr::null_mut();

    let cb: webview2_com::Microsoft::Web::WebView2::Win32::ICoreWebView2AcceleratorKeyPressedEventHandler =
        AcceleratorKeyPressedEventHandler::create(Box::new(|controller, event| Ok(())))
            .into();

    webview
        .controller()
        .add_AcceleratorKeyPressed(cb, token)
        .unwrap();
});

and I get an error on both of the arguments with those crates.

knotty bridge
#

i think you looked at the wrong wry version. tauri v2 beta still uses 0.37 not 0.38. 0.37 uses webview2 0.28 and windows 0.52

#

still the tao error is weird but maybe rust is just confused

#

p.s. if you use tauri v1 then the versions are 0.19 and 0.39 i think

full estuary
#

okay fantastic that solved it thanks!

#

i will experiment with this and see if i prefer it over rdev

#

assuming it works the way i would like