From c6d2a9a0f5bca7c00107a8f1f6a8cb76ace41e1e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jon=20Vald=C3=A9s?= Date: Tue, 4 Feb 2020 01:01:40 +0100 Subject: [PATCH 1/3] Hack to interpret more keyboard events on Linux --- src/bridge/keybindings.rs | 2 +- src/window.rs | 22 +++++++++++++++++++++- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/src/bridge/keybindings.rs b/src/bridge/keybindings.rs index e3b0a39..0c67360 100644 --- a/src/bridge/keybindings.rs +++ b/src/bridge/keybindings.rs @@ -94,7 +94,7 @@ fn parse_keycode(keycode: VirtualKeyCode) -> Option<(&'static str, bool)> { } } -fn append_modifiers(modifiers: ModifiersState, keycode_text: &str, special: bool) -> String { +pub fn append_modifiers(modifiers: ModifiersState, keycode_text: &str, special: bool) -> String { let mut result = keycode_text.to_string(); let mut special = special; diff --git a/src/window.rs b/src/window.rs index 014e02b..4d2dba0 100644 --- a/src/window.rs +++ b/src/window.rs @@ -5,7 +5,7 @@ use std::time::{Duration, Instant}; use image::{load_from_memory, GenericImageView, Pixel}; use skulpin::{CoordinateSystem, RendererBuilder, PresentMode}; use skulpin::winit::dpi::LogicalSize; -use skulpin::winit::event::{ElementState, Event, MouseScrollDelta, StartCause, WindowEvent}; +use skulpin::winit::event::{ElementState, Event, MouseScrollDelta, StartCause, WindowEvent, ModifiersState}; use skulpin::winit::event_loop::{ControlFlow, EventLoop}; use skulpin::winit::window::{Icon, WindowBuilder}; use log::{info, debug, trace, error}; @@ -73,6 +73,9 @@ pub fn ui_loop() { let mut mouse_down = false; let mut mouse_pos = (0, 0); + let mut allow_next_char = false; + let mut next_char_modifiers = ModifiersState::empty(); + info!("Starting window event loop"); event_loop.run(move |event, _window_target, control_flow| { trace!("Window Event: {:?}", event); @@ -101,11 +104,28 @@ pub fn ui_loop() { }, .. } => { + if input.virtual_keycode == None { + allow_next_char = true; + }else { + allow_next_char = false; + } + next_char_modifiers = input.modifiers; + construct_keybinding_string(input) .map(UiCommand::Keyboard) .map(|keybinding_string| BRIDGE.queue_command(keybinding_string)); }, + Event::WindowEvent { + event: WindowEvent::ReceivedCharacter(c), + .. + } => { + if allow_next_char { + let keybinding = super::bridge::append_modifiers(next_char_modifiers, &c.to_string(), false); + BRIDGE.queue_command(UiCommand::Keyboard(keybinding)); + } + }, + Event::WindowEvent { event: WindowEvent::CursorMoved { position, From 7cfe6257772061606068d68bb571ac470e10dce1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jon=20Vald=C3=A9s?= Date: Tue, 4 Feb 2020 01:23:58 +0100 Subject: [PATCH 2/3] Improved hack. By ignoring the shift modifier, it works even better --- src/window.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/window.rs b/src/window.rs index 4d2dba0..eaaca33 100644 --- a/src/window.rs +++ b/src/window.rs @@ -121,6 +121,7 @@ pub fn ui_loop() { .. } => { if allow_next_char { + next_char_modifiers.remove(ModifiersState::SHIFT); let keybinding = super::bridge::append_modifiers(next_char_modifiers, &c.to_string(), false); BRIDGE.queue_command(UiCommand::Keyboard(keybinding)); } From f273833f96834fed596aa8a3d1454ad1577bfdb3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jon=20Vald=C3=A9s?= Date: Tue, 4 Feb 2020 19:53:56 +0100 Subject: [PATCH 3/3] Added comment explaining Linux input hack --- src/window.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/window.rs b/src/window.rs index eaaca33..27dcdf6 100644 --- a/src/window.rs +++ b/src/window.rs @@ -104,6 +104,9 @@ pub fn ui_loop() { }, .. } => { + // Only interpret 'char' events when we get a previous event without a virtual + // keycode (which we ignore for KeyboardInput events). + // This is a hack so we don't lose a bunch of input events on Linux if input.virtual_keycode == None { allow_next_char = true; }else {