Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Expose window keyboard events to Tauri #12677

Draft
wants to merge 1 commit into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changes/expose-keyboard-inputs.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
tauri: 'minor:feat'
---

Expose window keyboard events from `tao`, as `WindowEvent::KeyboardInput`.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

559 changes: 559 additions & 0 deletions crates/tauri-runtime-wry/src/keyboard.rs

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions crates/tauri-runtime-wry/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ use std::{
pub type WebviewId = u32;
type IpcHandler = dyn Fn(Request<String>) + 'static;

mod keyboard;
#[cfg(any(
windows,
target_os = "linux",
Expand Down Expand Up @@ -532,6 +533,11 @@ impl<'a> From<&TaoWindowEvent<'a>> for WindowEventWrapper {
},
#[cfg(any(target_os = "linux", target_os = "macos"))]
TaoWindowEvent::Focused(focused) => WindowEvent::Focused(*focused),
TaoWindowEvent::KeyboardInput { event, .. } => {
let event = keyboard::convert_key_event(event);

WindowEvent::KeyboardInput { event }
}
TaoWindowEvent::ThemeChanged(theme) => WindowEvent::ThemeChanged(map_theme(theme)),
_ => return Self(None),
};
Expand Down
1 change: 1 addition & 0 deletions crates/tauri-runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ http = "1"
raw-window-handle = "0.6"
url = { version = "2" }
dpi = { version = "0.1", features = ["serde"] }
keyboard-types = "0.7.0"

[target."cfg(windows)".dependencies.windows]
version = "0.58"
Expand Down
18 changes: 18 additions & 0 deletions crates/tauri-runtime/src/keyboard.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
use serde::{Deserialize, Serialize};

pub use keyboard_types::{Code, Key, KeyState, Location, Modifiers};

/// Keyboard events are issued for all pressed and released keys.
#[derive(Clone, Debug, Default, Eq, Hash, PartialEq, Serialize, Deserialize)]
pub struct KeyboardEvent {
/// Whether the key is pressed or released.
pub state: KeyState,
/// Logical key value.
pub key: Key,
/// Physical key position.
pub code: Code,
/// Location for keys with multiple instances on common keyboards.
pub location: Location,
/// True if the key is currently auto-repeated.
pub repeat: bool,
}
1 change: 1 addition & 0 deletions crates/tauri-runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use tauri_utils::Theme;
use url::Url;
use webview::{DetachedWebview, PendingWebview};

pub mod keyboard;
/// Types useful for interacting with a user's monitors.
pub mod monitor;
pub mod webview;
Expand Down
5 changes: 5 additions & 0 deletions crates/tauri-runtime/src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
//! A layer between raw [`Runtime`] windows and Tauri.

use crate::{
keyboard::KeyboardEvent,
webview::{DetachedWebview, PendingWebview},
Icon, Runtime, UserEvent, WindowDispatch,
};
Expand Down Expand Up @@ -43,6 +44,10 @@ pub enum WindowEvent {
///
/// The parameter is true if the window has gained focus, and false if it has lost focus.
Focused(bool),

/// An event from the keyboard has been received.
KeyboardInput { event: KeyboardEvent },

/// The window's scale factor has changed.
///
/// The following user actions can cause DPI changes:
Expand Down
4 changes: 4 additions & 0 deletions crates/tauri/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ use tauri_macros::default_runtime;
use tauri_runtime::EventLoopProxy;
use tauri_runtime::{
dpi::{PhysicalPosition, PhysicalSize},
keyboard::KeyboardEvent,
window::DragDropEvent,
RuntimeInitArgs,
};
Expand Down Expand Up @@ -115,6 +116,8 @@ pub enum WindowEvent {
///
/// The parameter is true if the window has gained focus, and false if it has lost focus.
Focused(bool),
/// An event from the keyboard has been received.
KeyboardInput(KeyboardEvent),
/// The window's scale factor has changed.
///
/// The following user actions can cause DPI changes:
Expand Down Expand Up @@ -151,6 +154,7 @@ impl From<RuntimeWindowEvent> for WindowEvent {
},
RuntimeWindowEvent::Destroyed => Self::Destroyed,
RuntimeWindowEvent::Focused(flag) => Self::Focused(flag),
RuntimeWindowEvent::KeyboardInput { event } => Self::KeyboardInput(event),
RuntimeWindowEvent::ScaleFactorChanged {
scale_factor,
new_inner_size,
Expand Down
4 changes: 4 additions & 0 deletions crates/tauri/src/manager/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ const WINDOW_MOVED_EVENT: EventName<&str> = EventName::from_str("tauri://move");
const WINDOW_CLOSE_REQUESTED_EVENT: EventName<&str> =
EventName::from_str("tauri://close-requested");
const WINDOW_DESTROYED_EVENT: EventName<&str> = EventName::from_str("tauri://destroyed");
const WINDOW_KEYBOARD_INPUT_EVENT: EventName<&str> = EventName::from_str("tauri://keyboard-input");
const WINDOW_FOCUS_EVENT: EventName<&str> = EventName::from_str("tauri://focus");
const WINDOW_BLUR_EVENT: EventName<&str> = EventName::from_str("tauri://blur");
const WINDOW_SCALE_FACTOR_CHANGED_EVENT: EventName<&str> =
Expand Down Expand Up @@ -180,6 +181,9 @@ fn on_window_event<R: Runtime>(window: &Window<R>, event: &WindowEvent) -> crate
},
&(),
)?,
WindowEvent::KeyboardInput(event) => {
window.emit_to_window(WINDOW_KEYBOARD_INPUT_EVENT, event)?;
}
WindowEvent::ScaleFactorChanged {
scale_factor,
new_inner_size,
Expand Down