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

New keyboard API for Windows #1788

Merged
merged 79 commits into from
Apr 25, 2021
Merged
Show file tree
Hide file tree
Changes from 76 commits
Commits
Show all changes
79 commits
Select commit Hold shift + click to select a range
ee85011
Introducing the new `KeyEvent` and renaming old stuff
ArturKovacs Nov 14, 2020
4b41fbd
Implemented physical_key on Windows
ArturKovacs Nov 15, 2020
2d9e144
Ran cargo fmt
ArturKovacs Nov 15, 2020
355692f
Progress with the keyboard's windows implementation
ArturKovacs Dec 5, 2020
62bbb46
Add proper handling of dead keys
ArturKovacs Nov 26, 2020
e6f4f37
Add translation for non-printable virtual keys
ArturKovacs Nov 28, 2020
edde42f
Run `cargo fmt`
ArturKovacs Nov 28, 2020
d851d17
Fix for AltGraph not being reported
ArturKovacs Nov 28, 2020
c9c648b
Synthesize key events when focus enters or leaves
ArturKovacs Dec 4, 2020
3ea6d14
Minor improvements
ArturKovacs Dec 5, 2020
3442c5d
Remove obsolete API
ArturKovacs Dec 5, 2020
68d229c
Fix numlock and pause not being reported correctly
ArturKovacs Dec 5, 2020
e25f35e
Ran `cargo fmt`
ArturKovacs Dec 5, 2020
ad9bf98
Fix numpad keys being reported incorrectly
ArturKovacs Dec 5, 2020
458c1be
Update examples
ArturKovacs Dec 5, 2020
217789a
Ran `cargo fmt`
ArturKovacs Dec 5, 2020
936dba8
Add documentation for `ScanCode`
ArturKovacs Dec 20, 2020
75b16a9
Add key binding example
ArturKovacs Dec 20, 2020
7bbd122
Use consistent modifier key names #1343
ArturKovacs Jan 2, 2021
e469a97
WONT COMPILE transitioning to new keyboard API
ArturKovacs Jan 3, 2021
2cb256d
WONT COMPILE Implement new keyboard layout preparation
ArturKovacs Jan 4, 2021
a94c3d8
WONT COMPILE new keyboard API progress
ArturKovacs Jan 5, 2021
ff2d7aa
Main compile errors fixed for keyboard
ArturKovacs Jan 7, 2021
eedf78e
Fix bugs in new keyboard handling
ArturKovacs Jan 8, 2021
7b67bbd
Remove obsolete code
ArturKovacs Jan 9, 2021
86f0da8
Fix examples
ArturKovacs Jan 9, 2021
bafaa9a
Merge branch 'master' into new-keyboard
ArturKovacs Jan 9, 2021
f4a3ee9
Ran `cargo fmt`
ArturKovacs Jan 9, 2021
0d43977
Fix build error with serde
ArturKovacs Jan 9, 2021
5d3b7b6
Ran `cargo fmt`
ArturKovacs Jan 9, 2021
1edbb63
Tweaks in the Windows keyboard implementation
ArturKovacs Jan 10, 2021
884e673
Add `KeyCodeExtScancode`
ArturKovacs Jan 10, 2021
5f5d87c
Add `reset_dead_keys`
ArturKovacs Jan 10, 2021
3226db1
Improve the documentation for `Key` and `KeyCode`
ArturKovacs Jan 10, 2021
b56abf1
Rename the meta key to super
ArturKovacs Jan 10, 2021
0527095
Address feedback for the keyboard API
ArturKovacs Jan 11, 2021
1ea30a7
Fix for rustdoc
ArturKovacs Jan 23, 2021
94eb458
Improve documentation
ArturKovacs Jan 23, 2021
9c3025d
Fix for arrow keys being reported as unidentified.
ArturKovacs Jan 23, 2021
d92a942
Fix media keys reporting Unidentified
ArturKovacs Jan 23, 2021
84a7ceb
Don't report text on key release events
ArturKovacs Jan 23, 2021
d3178b3
Fix for NumLock being reported as Pause in raw input
ArturKovacs Jan 23, 2021
76e7bca
Fix for strange behaviour around NumLock and Pause
ArturKovacs Jan 23, 2021
41855c9
Fix for NumLock being ineffective
ArturKovacs Jan 24, 2021
d99d6fc
Fix for location not being reported correctly
ArturKovacs Jan 24, 2021
3096e0e
`RawKeyEvent`s now report repeat
ArturKovacs Jan 24, 2021
37da87b
Don't report text for synthetic key releases
ArturKovacs Feb 4, 2021
1fbd967
Address feedback
ArturKovacs Feb 4, 2021
f857190
Filter `Unidentified` from PrtSc key device events
ArturKovacs Feb 6, 2021
ee21163
Don't report incorrect `RawKeyEvent` for shift + numpad
ArturKovacs Feb 6, 2021
df1a48d
AltGraph is not reported again
ArturKovacs Feb 6, 2021
c3e4f48
Document Windows specific behaviour for shift+numpad
ArturKovacs Feb 6, 2021
76c1ab3
Fix typo
ArturKovacs Feb 14, 2021
61775c1
Dead keys now affect characters from logical_key
ArturKovacs Feb 14, 2021
72e36b5
Prevent Pause and NumLock mappings in window events
ArturKovacs Feb 14, 2021
98246b9
Apply suggestions from code review
ArturKovacs Feb 15, 2021
37f696d
Merge branch 'new-keyboard' of https://github.com/ArturKovacs/winit i…
ArturKovacs Feb 15, 2021
0aa46fd
Ran `cargo fmt`
ArturKovacs Feb 15, 2021
ea98471
Add W3C license for `Key` and `KeyCode`
ArturKovacs Feb 15, 2021
3a94735
Extend documentation according to feedback
ArturKovacs Feb 16, 2021
70b2d6e
Ignore NumLock in `key_without_modifiers`
ArturKovacs Feb 20, 2021
0717b08
Remove unused `key_code_to_non_char_key`
ArturKovacs Feb 20, 2021
ba0bf1f
Merge branch 'master' into new-keyboard
ArturKovacs Feb 20, 2021
c88bf3d
Remove empty event.rs file
ArturKovacs Feb 20, 2021
8538486
Use space for resetting dead keys
ArturKovacs Feb 20, 2021
15cfca1
Fix reporting multiple graphemes in logical_key
ArturKovacs Feb 20, 2021
13bf3cd
Avoid incorrect synthetic keypress during setfocus
ArturKovacs Feb 28, 2021
57d550a
Fixed the AltGr keypress not being reported when the AltGr key is pre…
ArturKovacs Feb 28, 2021
0926ec7
Filter fake Ctrl events when pressing AltGr
ArturKovacs Feb 28, 2021
42055e3
Improve code quality
ArturKovacs Mar 3, 2021
03c8b0d
Remove `repeat` from `RawKeyEvent`
ArturKovacs Apr 1, 2021
166d79e
Allow fractional scroll in raw mouse events
ArturKovacs Apr 12, 2021
766c7dc
Fix typo
ArturKovacs Apr 12, 2021
37dae30
Remove unused imports
ArturKovacs Apr 12, 2021
a52f86d
Merge branch 'new-keyboard' of https://github.com/ArturKovacs/winit i…
ArturKovacs Apr 12, 2021
f0474f2
Remove unused variable
ArturKovacs Apr 12, 2021
f72d2f1
Remove unnecessary `unwrap()`
ArturKovacs Apr 25, 2021
b0ddbdb
Avoid using the deprecated `into_rgba()`
ArturKovacs Apr 25, 2021
d05ac2e
Fix IME crash
ArturKovacs Apr 25, 2021
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
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ log = "0.4"
serde = { version = "1", optional = true, features = ["serde_derive"] }
raw-window-handle = "0.3"
bitflags = "1"
nameof = "1"

[dev-dependencies]
image = "0.23.12"
Expand All @@ -57,6 +58,7 @@ features = ["display_link"]

[target.'cfg(target_os = "windows")'.dependencies]
parking_lot = "0.11"
unicode-segmentation = "1.7.1"

[target.'cfg(target_os = "windows")'.dependencies.winapi]
version = "0.3.6"
Expand All @@ -80,6 +82,7 @@ features = [
"winerror",
"wingdi",
"winnt",
"winnls",
"winuser",
]

Expand Down
25 changes: 14 additions & 11 deletions examples/control_flow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ use std::{thread, time};

use simple_logger::SimpleLogger;
use winit::{
event::{Event, KeyboardInput, WindowEvent},
event::{ElementState, Event, KeyEvent, WindowEvent},
event_loop::{ControlFlow, EventLoop},
keyboard::Key,
window::WindowBuilder,
};

Expand Down Expand Up @@ -38,7 +39,7 @@ fn main() {
let mut close_requested = false;

event_loop.run(move |event, _, control_flow| {
use winit::event::{ElementState, StartCause, VirtualKeyCode};
use winit::event::StartCause;
println!("{:?}", event);
match event {
Event::NewEvents(start_cause) => {
Expand All @@ -52,31 +53,33 @@ fn main() {
close_requested = true;
}
WindowEvent::KeyboardInput {
input:
KeyboardInput {
virtual_keycode: Some(virtual_code),
event:
KeyEvent {
logical_key: key,
state: ElementState::Pressed,
..
},
..
} => match virtual_code {
VirtualKeyCode::Key1 => {
} => match key {
// WARNING: Consider using `key_without_modifers()` if available on your platform.
// See the `key_binding` example
Key::Character("1") => {
mode = Mode::Wait;
println!("\nmode: {:?}\n", mode);
}
VirtualKeyCode::Key2 => {
Key::Character("2") => {
mode = Mode::WaitUntil;
println!("\nmode: {:?}\n", mode);
}
VirtualKeyCode::Key3 => {
Key::Character("3") => {
mode = Mode::Poll;
println!("\nmode: {:?}\n", mode);
}
VirtualKeyCode::R => {
Key::Character("r") => {
request_redraw = !request_redraw;
println!("\nrequest_redraw: {}\n", request_redraw);
}
VirtualKeyCode::Escape => {
Key::Escape => {
close_requested = true;
}
_ => (),
Expand Down
6 changes: 3 additions & 3 deletions examples/cursor.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use simple_logger::SimpleLogger;
use winit::{
event::{ElementState, Event, KeyboardInput, WindowEvent},
event::{ElementState, Event, KeyEvent, WindowEvent},
event_loop::{ControlFlow, EventLoop},
window::{CursorIcon, WindowBuilder},
};
Expand All @@ -21,8 +21,8 @@ fn main() {
Event::WindowEvent {
event:
WindowEvent::KeyboardInput {
input:
KeyboardInput {
event:
KeyEvent {
state: ElementState::Pressed,
..
},
Expand Down
21 changes: 13 additions & 8 deletions examples/cursor_grab.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use simple_logger::SimpleLogger;
use winit::{
event::{DeviceEvent, ElementState, Event, KeyboardInput, ModifiersState, WindowEvent},
event::{DeviceEvent, ElementState, Event, KeyEvent, WindowEvent},
event_loop::{ControlFlow, EventLoop},
keyboard::{Key, ModifiersState},
window::WindowBuilder,
};

Expand All @@ -23,19 +24,23 @@ fn main() {
Event::WindowEvent { event, .. } => match event {
WindowEvent::CloseRequested => *control_flow = ControlFlow::Exit,
WindowEvent::KeyboardInput {
input:
KeyboardInput {
event:
KeyEvent {
logical_key: key,
state: ElementState::Released,
virtual_keycode: Some(key),
..
},
..
} => {
use winit::event::VirtualKeyCode::*;
// WARNING: Consider using `key_without_modifers()` if available on your platform.
// See the `key_binding` example
match key {
Escape => *control_flow = ControlFlow::Exit,
G => window.set_cursor_grab(!modifiers.shift()).unwrap(),
H => window.set_cursor_visible(modifiers.shift()),
Key::Escape => *control_flow = ControlFlow::Exit,
Key::Character(ch) => match ch.to_lowercase().as_str() {
ArturKovacs marked this conversation as resolved.
Show resolved Hide resolved
"g" => window.set_cursor_grab(!modifiers.shift_key()).unwrap(),
"h" => window.set_cursor_visible(modifiers.shift_key()),
_ => (),
},
_ => (),
}
}
Expand Down
26 changes: 15 additions & 11 deletions examples/fullscreen.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use std::io::{stdin, stdout, Write};

use simple_logger::SimpleLogger;
use winit::event::{ElementState, Event, KeyboardInput, VirtualKeyCode, WindowEvent};
use winit::event::{ElementState, Event, KeyEvent, WindowEvent};
use winit::event_loop::{ControlFlow, EventLoop};
use winit::keyboard::Key;
use winit::monitor::{MonitorHandle, VideoMode};
use winit::window::{Fullscreen, WindowBuilder};

Expand Down Expand Up @@ -38,30 +39,33 @@ fn main() {
Event::WindowEvent { event, .. } => match event {
WindowEvent::CloseRequested => *control_flow = ControlFlow::Exit,
WindowEvent::KeyboardInput {
input:
KeyboardInput {
virtual_keycode: Some(virtual_code),
state,
event:
KeyEvent {
logical_key: key,
state: ElementState::Pressed,
..
},
..
} => match (virtual_code, state) {
(VirtualKeyCode::Escape, _) => *control_flow = ControlFlow::Exit,
(VirtualKeyCode::F, ElementState::Pressed) => {
} => match key {
Key::Escape => *control_flow = ControlFlow::Exit,

// WARNING: Consider using `key_without_modifers()` if available on your platform.
// See the `key_binding` example
Key::Character("f") => {
if window.fullscreen().is_some() {
window.set_fullscreen(None);
} else {
window.set_fullscreen(fullscreen.clone());
}
}
(VirtualKeyCode::S, ElementState::Pressed) => {
Key::Character("s") => {
println!("window.fullscreen {:?}", window.fullscreen());
}
(VirtualKeyCode::M, ElementState::Pressed) => {
Key::Character("m") => {
let is_maximized = window.is_maximized();
window.set_maximized(!is_maximized);
}
(VirtualKeyCode::D, ElementState::Pressed) => {
Key::Character("d") => {
decorations = !decorations;
window.set_decorations(decorations);
}
Expand Down
23 changes: 11 additions & 12 deletions examples/handling_close.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use simple_logger::SimpleLogger;
use winit::{
event::{Event, KeyboardInput, WindowEvent},
event::{ElementState, Event, KeyEvent, WindowEvent},
event_loop::{ControlFlow, EventLoop},
keyboard::Key,
window::WindowBuilder,
};

Expand All @@ -17,10 +18,6 @@ fn main() {
let mut close_requested = false;

event_loop.run(move |event, _, control_flow| {
use winit::event::{
ElementState::Released,
VirtualKeyCode::{N, Y},
};
*control_flow = ControlFlow::Wait;

match event {
Expand All @@ -44,16 +41,18 @@ fn main() {
// the Y key.
}
WindowEvent::KeyboardInput {
input:
KeyboardInput {
virtual_keycode: Some(virtual_code),
state: Released,
event:
KeyEvent {
logical_key: key,
state: ElementState::Released,
..
},
..
} => {
match virtual_code {
Y => {
// WARNING: Consider using `key_without_modifers()` if available on your platform.
// See the `key_binding` example
match key {
Key::Character("y") => {
if close_requested {
// This is where you'll want to do any cleanup you need.
println!("Buh-bye!");
Expand All @@ -66,7 +65,7 @@ fn main() {
*control_flow = ControlFlow::Exit;
}
}
N => {
Key::Character("n") => {
if close_requested {
println!("Your window will continue to stay by your side.");
close_requested = false;
Expand Down
58 changes: 58 additions & 0 deletions examples/key_binding.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
use simple_logger::SimpleLogger;
use winit::{
dpi::LogicalSize,
event::{ElementState, Event, KeyEvent, WindowEvent},
event_loop::{ControlFlow, EventLoop},
keyboard::{Key, ModifiersState},
window::WindowBuilder,
};

/////////////////////////////////////////////////////////////////////////////
// WARNING: This is not available on all platforms (for example on the web).
use winit::platform::modifier_supplement::KeyEventExtModifierSupplement;
/////////////////////////////////////////////////////////////////////////////

fn main() {
SimpleLogger::new().init().unwrap();
let event_loop = EventLoop::new();

let _window = WindowBuilder::new()
.with_inner_size(LogicalSize::new(400.0, 200.0))
.build(&event_loop)
.unwrap();

let mut modifiers = ModifiersState::default();

event_loop.run(move |event, _, control_flow| {
*control_flow = ControlFlow::Wait;

match event {
Event::WindowEvent { event, .. } => match event {
WindowEvent::CloseRequested => *control_flow = ControlFlow::Exit,
WindowEvent::ModifiersChanged(new_state) => {
modifiers = new_state;
}
WindowEvent::KeyboardInput { event, .. } => {
handle_key_event(modifiers, event);
}
_ => (),
},
_ => (),
};
});
}

fn handle_key_event(modifiers: ModifiersState, event: KeyEvent) {
if event.state == ElementState::Pressed && !event.repeat {
match event.key_without_modifiers() {
Key::Character("1") => {
if modifiers.shift_key() {
println!("Shift + 1 | logical_key: {:?}", event.logical_key);
} else {
println!("1");
}
}
_ => (),
}
}
}
12 changes: 8 additions & 4 deletions examples/minimize.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
extern crate winit;

use simple_logger::SimpleLogger;
use winit::event::{Event, VirtualKeyCode, WindowEvent};

use winit::event::{Event, WindowEvent};
use winit::event_loop::{ControlFlow, EventLoop};
use winit::keyboard::Key;
use winit::window::WindowBuilder;

fn main() {
Expand All @@ -25,12 +27,14 @@ fn main() {

// Keyboard input event to handle minimize via a hotkey
Event::WindowEvent {
event: WindowEvent::KeyboardInput { input, .. },
event: WindowEvent::KeyboardInput { event, .. },
window_id,
} => {
if window_id == window.id() {
// Pressing the 'M' key will minimize the window
if input.virtual_keycode == Some(VirtualKeyCode::M) {
// Pressing the 'm' key will minimize the window
// WARNING: Consider using `key_without_modifers()` if available on your platform.
// See the `key_binding` example
if let Key::Character("m") = event.logical_key {
window.set_minimized(true);
}
}
Expand Down
Loading