Skip to content

Commit 443570d

Browse files
committed
Delegate tao key events to tauri
1 parent 8e9339e commit 443570d

File tree

12 files changed

+620
-0
lines changed

12 files changed

+620
-0
lines changed

.changes/expose-keyboard-inputs.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
tauri: 'minor:feat'
3+
---
4+
5+
Expose window keyboard events from `tao`, as `WindowEvent::KeyboardInput`.

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/tauri-runtime-wry/src/keyboard.rs

Lines changed: 559 additions & 0 deletions
Large diffs are not rendered by default.

crates/tauri-runtime-wry/src/lib.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ use std::{
125125
pub type WebviewId = u32;
126126
type IpcHandler = dyn Fn(Request<String>) + 'static;
127127

128+
mod keyboard;
128129
#[cfg(any(
129130
windows,
130131
target_os = "linux",
@@ -532,6 +533,11 @@ impl<'a> From<&TaoWindowEvent<'a>> for WindowEventWrapper {
532533
},
533534
#[cfg(any(target_os = "linux", target_os = "macos"))]
534535
TaoWindowEvent::Focused(focused) => WindowEvent::Focused(*focused),
536+
TaoWindowEvent::KeyboardInput { event, .. } => {
537+
let event = keyboard::convert_key_event(event);
538+
539+
WindowEvent::KeyboardInput { event }
540+
}
535541
TaoWindowEvent::ThemeChanged(theme) => WindowEvent::ThemeChanged(map_theme(theme)),
536542
_ => return Self(None),
537543
};

crates/tauri-runtime/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ http = "1"
3434
raw-window-handle = "0.6"
3535
url = { version = "2" }
3636
dpi = { version = "0.1", features = ["serde"] }
37+
keyboard-types = "0.7.0"
3738

3839
[target."cfg(windows)".dependencies.windows]
3940
version = "0.58"

crates/tauri-runtime/src/keyboard.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
use serde::{Deserialize, Serialize};
2+
3+
pub use keyboard_types::{Code, Key, KeyState, Location, Modifiers};
4+
5+
/// Keyboard events are issued for all pressed and released keys.
6+
#[derive(Clone, Debug, Default, Eq, Hash, PartialEq, Serialize, Deserialize)]
7+
pub struct KeyboardEvent {
8+
/// Whether the key is pressed or released.
9+
pub state: KeyState,
10+
/// Logical key value.
11+
pub key: Key,
12+
/// Physical key position.
13+
pub code: Code,
14+
/// Location for keys with multiple instances on common keyboards.
15+
pub location: Location,
16+
/// True if the key is currently auto-repeated.
17+
pub repeat: bool,
18+
}
19+
20+
impl KeyboardEvent {
21+
pub fn is_down(&self) -> bool {
22+
self.state == KeyState::Down
23+
}
24+
25+
pub fn is_up(&self) -> bool {
26+
self.state == KeyState::Up
27+
}
28+
}

crates/tauri-runtime/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ use tauri_utils::Theme;
2121
use url::Url;
2222
use webview::{DetachedWebview, PendingWebview};
2323

24+
pub mod keyboard;
2425
/// Types useful for interacting with a user's monitors.
2526
pub mod monitor;
2627
pub mod webview;

crates/tauri-runtime/src/window.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
//! A layer between raw [`Runtime`] windows and Tauri.
66
77
use crate::{
8+
keyboard::KeyboardEvent,
89
webview::{DetachedWebview, PendingWebview},
910
Icon, Runtime, UserEvent, WindowDispatch,
1011
};
@@ -43,6 +44,10 @@ pub enum WindowEvent {
4344
///
4445
/// The parameter is true if the window has gained focus, and false if it has lost focus.
4546
Focused(bool),
47+
48+
/// An event from the keyboard has been received.
49+
KeyboardInput { event: KeyboardEvent },
50+
4651
/// The window's scale factor has changed.
4752
///
4853
/// The following user actions can cause DPI changes:

crates/tauri/src/app.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use crate::{
88
channel::ChannelDataIpcQueue, CallbackFn, CommandArg, CommandItem, Invoke, InvokeError,
99
InvokeHandler, InvokeResponseBody,
1010
},
11+
keyboard::KeyboardEvent,
1112
manager::{webview::UriSchemeProtocol, AppManager, Asset},
1213
plugin::{Plugin, PluginStore},
1314
resources::ResourceTable,
@@ -115,6 +116,8 @@ pub enum WindowEvent {
115116
///
116117
/// The parameter is true if the window has gained focus, and false if it has lost focus.
117118
Focused(bool),
119+
/// An event from the keyboard has been received.
120+
KeyboardInput(KeyboardEvent),
118121
/// The window's scale factor has changed.
119122
///
120123
/// The following user actions can cause DPI changes:
@@ -151,6 +154,7 @@ impl From<RuntimeWindowEvent> for WindowEvent {
151154
},
152155
RuntimeWindowEvent::Destroyed => Self::Destroyed,
153156
RuntimeWindowEvent::Focused(flag) => Self::Focused(flag),
157+
RuntimeWindowEvent::KeyboardInput { event } => Self::KeyboardInput(event),
154158
RuntimeWindowEvent::ScaleFactorChanged {
155159
scale_factor,
156160
new_inner_size,

crates/tauri/src/keyboard/mod.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// Copyright 2019-2024 Tauri Programme within The Commons Conservancy
2+
// SPDX-License-Identifier: Apache-2.0
3+
// SPDX-License-Identifier: MIT
4+
5+
pub use tauri_runtime::keyboard::{Code, Key, KeyState, KeyboardEvent, Location, Modifiers};

crates/tauri/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ pub mod async_runtime;
8282
mod error;
8383
mod event;
8484
pub mod ipc;
85+
pub mod keyboard;
8586
mod manager;
8687
mod pattern;
8788
pub mod plugin;

crates/tauri/src/manager/window.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ const WINDOW_MOVED_EVENT: EventName<&str> = EventName::from_str("tauri://move");
2828
const WINDOW_CLOSE_REQUESTED_EVENT: EventName<&str> =
2929
EventName::from_str("tauri://close-requested");
3030
const WINDOW_DESTROYED_EVENT: EventName<&str> = EventName::from_str("tauri://destroyed");
31+
const WINDOW_KEYBOARD_INPUT_EVENT: EventName<&str> = EventName::from_str("tauri://keyboard-input");
3132
const WINDOW_FOCUS_EVENT: EventName<&str> = EventName::from_str("tauri://focus");
3233
const WINDOW_BLUR_EVENT: EventName<&str> = EventName::from_str("tauri://blur");
3334
const WINDOW_SCALE_FACTOR_CHANGED_EVENT: EventName<&str> =
@@ -180,6 +181,9 @@ fn on_window_event<R: Runtime>(window: &Window<R>, event: &WindowEvent) -> crate
180181
},
181182
&(),
182183
)?,
184+
WindowEvent::KeyboardInput(event) => {
185+
window.emit_to_window(WINDOW_KEYBOARD_INPUT_EVENT, event)?;
186+
}
183187
WindowEvent::ScaleFactorChanged {
184188
scale_factor,
185189
new_inner_size,

0 commit comments

Comments
 (0)