Skip to content

Commit d287fa9

Browse files
committed
Add keyboard modifiers to input event
Making applications track modifier keys results in unnecessary work for consumers, it's error prone, and it turns out to have unavoidable bugs. For example, alt-tabbing with x11 results in the alt modifier state getting stuck. To resolve these problems, this patch adds a Mods value to the keyboard input event. The modifiers are as delivered by the windowing API. Currently, this is just x11, but support for the other APIs is coming.
1 parent 9a26f29 commit d287fa9

5 files changed

Lines changed: 52 additions & 7 deletions

File tree

‎Cargo.toml‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ build = "build.rs"
1414
lazy_static = "0.2.0"
1515
libc = "0.2"
1616
shared_library = "0.1.0"
17+
bitflags = "0.7"
1718

1819
[build-dependencies]
1920
gl_generator = "0.5"
@@ -41,7 +42,7 @@ dwmapi-sys = "0.1"
4142

4243
[target.'cfg(any(target_os = "linux", target_os = "freebsd", target_os="dragonfly", target_os="openbsd"))'.dependencies]
4344
osmesa-sys = "0.1.0"
44-
wayland-client = { version = "0.5.4", features = ["egl", "dlopen"] }
45+
wayland-client = { version = "0.5.12", features = ["egl", "dlopen"] }
4546
wayland-kbd = "0.3.3"
46-
wayland-window = "0.2.2"
47+
wayland-window = "0.2.3"
4748
x11-dl = "2.4"

‎src/api/wayland/keyboard.rs‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ use Event as GlutinEvent;
44
use ElementState;
55
use VirtualKeyCode;
66

7+
use events::Mods;
8+
79
use wayland_client::ProxyId;
810
use wayland_client::wayland::seat::{WlKeyboardEvent,WlKeyboardKeyState};
911

@@ -43,7 +45,8 @@ pub fn translate_kbd_events(
4345
WlKeyboardKeyState::Released =>ElementState::Released
4446
},
4547
(kevt.keycode & 0xff) as u8,
46-
vkcode
48+
vkcode,
49+
Mods::empty(), // TODO
4750
),
4851
surface
4952
));
@@ -54,7 +57,6 @@ pub fn translate_kbd_events(
5457
));
5558
}
5659
}
57-
5860
}
5961
MappedKeyboardEvent::Other(oevt) => match oevt {
6062
WlKeyboardEvent::Enter(_, surface, _) => {
@@ -226,4 +228,4 @@ pub fn keysym_to_vkey(keysym: u32) -> Option<VirtualKeyCode> {
226228
// fallback
227229
_ => None
228230
}
229-
}
231+
}

‎src/api/x11/input.rs‎

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use std::slice::from_raw_parts;
88
use WindowAttributes;
99

1010
use events::Event;
11+
use events::{mods, Mods};
1112

1213
use super::{events, ffi};
1314
use super::XConnection;
@@ -139,6 +140,8 @@ impl XInputEventHandler {
139140

140141
let mut kp_keysym = 0;
141142

143+
let mut ev_mods = Mods::empty();
144+
142145
let written = unsafe {
143146
use std::str;
144147

@@ -148,6 +151,26 @@ impl XInputEventHandler {
148151
mem::transmute(buffer.as_mut_ptr()),
149152
buffer.len() as libc::c_int, &mut kp_keysym, ptr::null_mut());
150153

154+
{
155+
// Translate x event state to mods
156+
let state = event.state;
157+
if (state & ffi::Mod1Mask) != 0 {
158+
ev_mods.insert(mods::ALT);
159+
}
160+
161+
if (state & ffi::ShiftMask) != 0 {
162+
ev_mods.insert(mods::SHIFT);
163+
}
164+
165+
if (state & ffi::ControlMask) != 0 {
166+
ev_mods.insert(mods::CONTROL);
167+
}
168+
169+
if (state & ffi::Mod4Mask) != 0 {
170+
ev_mods.insert(mods::SUPER);
171+
}
172+
}
173+
151174
str::from_utf8(&buffer[..count as usize]).unwrap_or("").to_string()
152175
};
153176

@@ -165,7 +188,7 @@ impl XInputEventHandler {
165188

166189
let vkey = events::keycode_to_element(keysym as libc::c_uint);
167190

168-
translated_events.push(KeyboardInput(state, event.keycode as u8, vkey));
191+
translated_events.push(KeyboardInput(state, event.keycode as u8, vkey, ev_mods));
169192
translated_events
170193
}
171194

‎src/events.rs‎

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,21 @@
11
use std::path::PathBuf;
22

3+
pub mod mods {
4+
bitflags! {
5+
pub flags Mods: u8 {
6+
const ALT = 0b0001,
7+
const SHIFT = 0b0010,
8+
const CONTROL = 0b0100,
9+
const SUPER = 0b1000,
10+
11+
const ANY = ALT.bits | SHIFT.bits | CONTROL.bits | SUPER.bits,
12+
const NONE = 0b0000,
13+
}
14+
}
15+
}
16+
17+
pub use mods::Mods;
18+
319
#[derive(Clone, Debug)]
420
pub enum Event {
521
/// The size of the window has changed.
@@ -23,7 +39,7 @@ pub enum Event {
2339
Focused(bool),
2440

2541
/// An event from the keyboard has been received.
26-
KeyboardInput(ElementState, ScanCode, Option<VirtualKeyCode>),
42+
KeyboardInput(ElementState, ScanCode, Option<VirtualKeyCode>, Mods),
2743

2844
/// The cursor has moved on the window.
2945
///

‎src/lib.rs‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ extern crate shared_library;
3131

3232
extern crate libc;
3333

34+
#[macro_use]
35+
extern crate bitflags;
36+
3437
#[cfg(target_os = "windows")]
3538
extern crate winapi;
3639
#[cfg(target_os = "windows")]

0 commit comments

Comments
 (0)