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

Add keyboard event processing #5

Merged
merged 2 commits into from
Sep 1, 2020
Merged
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
9 changes: 9 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,19 @@ edition = "2018"

[package.metadata.docs.rs]
default-target = "x86_64-pc-windows-msvc"
all-features = true

[features]
kb = ["keyboard-types"]

[dependencies.winapi]
version = "0.3.8"
features = ["winuser"]

[dependencies.keyboard-types]
version = "0.5.0"
optional = true
default-features = false

[dependencies]
wio = "0.2.2"
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,7 @@ One goal of the crate is to make it easier to reason about soundness, by providi
Another goal is to provide reasonably good documentation, including detailed links to official documentation and other resources. Many of these lessons have been hard-learned, as part of the Windows backend for druid, and other experiments.

The crate is "semi-opinionated" in that it nails down some details, especially the way threads work, but how you draw and the way you handle events is entirely up to you. It is a goal that anybody who creates a HWND from Rust should use this crate. If there's some reason it doesn't work for your use case, I'm curious why, so please file an issue.

There is an optional `kb` feature, which does the rather tricky and fiddly job of converting platform keyboard messages into `KeyboardEvent` structs from the [keyboard-types] crate, based firmly on W3C specs. It's possible that more such features will be added (dpi handling is a strong possibility).

[keyboard-types]: https://crates.io/crates/keyboard-types
44 changes: 35 additions & 9 deletions examples/hello-win.rs
Original file line number Diff line number Diff line change
@@ -1,30 +1,52 @@
#[allow(unused)]
use std::cell::RefCell;
use std::ptr::null_mut;

use winapi::shared::minwindef::{HINSTANCE, LPARAM, LRESULT, UINT, WPARAM};
use winapi::shared::windef::HWND;
use winapi::um::wingdi::CreateSolidBrush;
use winapi::um::winuser::{
LoadCursorW, LoadIconW, PostQuitMessage, ShowWindow, IDC_ARROW, IDI_APPLICATION, SW_SHOWNORMAL,
WM_DESTROY, WS_OVERLAPPEDWINDOW,
WM_CHAR, WM_DESTROY, WM_INPUTLANGCHANGE, WM_KEYDOWN, WM_KEYUP, WM_SYSCHAR, WM_SYSKEYDOWN,
WM_SYSKEYUP, WS_OVERLAPPEDWINDOW,
};

#[cfg(feature = "kb")]
use win_win::KeyboardState;

use win_win::{WindowBuilder, WindowClass, WindowProc};

struct MyWindowProc;
struct MyWindowProc {
#[cfg(feature = "kb")]
kb_state: RefCell<KeyboardState>,
}

impl WindowProc for MyWindowProc {
#[allow(unused)]
fn window_proc(
&self,
_hwnd: HWND,
hwnd: HWND,
msg: UINT,
_wparam: WPARAM,
_lparam: LPARAM,
wparam: WPARAM,
lparam: LPARAM,
) -> Option<LRESULT> {
println!("msg {}", msg);
if msg == WM_DESTROY {
unsafe {
match msg {
WM_DESTROY => unsafe {
PostQuitMessage(0);
},
WM_KEYDOWN | WM_SYSKEYDOWN | WM_KEYUP | WM_SYSKEYUP | WM_CHAR | WM_SYSCHAR
| WM_INPUTLANGCHANGE => {
#[cfg(feature = "kb")]
if let Some(event) = unsafe {
self.kb_state
.borrow_mut()
.process_message(hwnd, msg, wparam, lparam)
} {
println!("event: {:?}", event);
return Some(0);
}
}
_ => (),
}
None
}
Expand All @@ -41,7 +63,11 @@ fn main() {
.background(brush)
.build()
.unwrap();
let hwnd = WindowBuilder::new(MyWindowProc, &win_class)
let window_proc = MyWindowProc {
#[cfg(feature = "kb")]
kb_state: RefCell::new(KeyboardState::new()),
};
let hwnd = WindowBuilder::new(window_proc, &win_class)
.name("win-win example")
.style(WS_OVERLAPPEDWINDOW)
.build();
Expand Down
Loading