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

Report the Delete key in text #2013

Closed
Closed
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
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"
mint = { version = "0.5.6", optional = true }

[dev-dependencies]
Expand Down Expand Up @@ -59,6 +60,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 @@ -82,6 +84,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() {
"g" => window.set_cursor_grab(!modifiers.shift_key()).unwrap(),
"h" => window.set_cursor_visible(modifiers.shift_key()),
_ => (),
},
_ => (),
}
}
Expand Down
11 changes: 5 additions & 6 deletions examples/drag_window.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use simple_logger::SimpleLogger;
use winit::{
event::{
ElementState, Event, KeyboardInput, MouseButton, StartCause, VirtualKeyCode, WindowEvent,
},
event::{ElementState, Event, KeyEvent, MouseButton, StartCause, WindowEvent},
event_loop::{ControlFlow, EventLoop},
keyboard::Key,
window::{Window, WindowBuilder, WindowId},
};

Expand Down Expand Up @@ -43,10 +42,10 @@ fn main() {
name_windows(entered_id, switched, &window_1, &window_2)
}
WindowEvent::KeyboardInput {
input:
KeyboardInput {
event:
KeyEvent {
state: ElementState::Released,
virtual_keycode: Some(VirtualKeyCode::X),
logical_key: Key::Character("x"),
..
},
..
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