mirror of
https://github.com/italicsjenga/winit-sonoma-fix.git
synced 2025-01-11 21:31:29 +11:00
Merge pull request #92 from tomaka/remove-modifiers
Remove key modifiers in KeyboardInput
This commit is contained in:
commit
b55f18f135
|
@ -18,7 +18,7 @@ pub enum Event {
|
||||||
Focused(bool),
|
Focused(bool),
|
||||||
|
|
||||||
/// An event from the keyboard has been received.
|
/// An event from the keyboard has been received.
|
||||||
KeyboardInput(ElementState, ScanCode, Option<VirtualKeyCode>, KeyModifiers),
|
KeyboardInput(ElementState, ScanCode, Option<VirtualKeyCode>),
|
||||||
|
|
||||||
/// The cursor has moved on the window.
|
/// The cursor has moved on the window.
|
||||||
///
|
///
|
||||||
|
@ -35,20 +35,6 @@ pub enum Event {
|
||||||
|
|
||||||
pub type ScanCode = u8;
|
pub type ScanCode = u8;
|
||||||
|
|
||||||
bitflags!(
|
|
||||||
#[deriving(Show)]
|
|
||||||
flags KeyModifiers: u8 {
|
|
||||||
const LEFT_CONTROL_MODIFIER = 1,
|
|
||||||
const RIGHT_CONTROL_MODIFIER = 2,
|
|
||||||
const LEFT_SHIFT_MODIFIER = 4,
|
|
||||||
const RIGHT_SHIFT_MODIFIER = 8,
|
|
||||||
const LEFT_ALT_MODIFIER = 16,
|
|
||||||
const RIGHT_ALT_MODIFIER = 32,
|
|
||||||
const NUM_LOCK_MODIFIER = 64,
|
|
||||||
const CAPS_LOCK_MODIFIER = 128
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
#[deriving(Show, Hash, PartialEq, Eq, Clone)]
|
#[deriving(Show, Hash, PartialEq, Eq, Clone)]
|
||||||
pub enum ElementState {
|
pub enum ElementState {
|
||||||
Pressed,
|
Pressed,
|
||||||
|
|
|
@ -1,25 +1,7 @@
|
||||||
use events;
|
use events;
|
||||||
use events::KeyModifiers;
|
|
||||||
use cocoa::base::NSUInteger;
|
use cocoa::base::NSUInteger;
|
||||||
use cocoa::appkit;
|
use cocoa::appkit;
|
||||||
|
|
||||||
pub fn modifierflag_to_element(flag: NSUInteger) -> KeyModifiers {
|
|
||||||
let mut modifiers = KeyModifiers::empty();
|
|
||||||
if flag & appkit::NSShiftKeyMask as u64 == appkit::NSShiftKeyMask as u64 {
|
|
||||||
modifiers = modifiers | events::LEFT_SHIFT_MODIFIER;
|
|
||||||
}
|
|
||||||
if flag & appkit::NSControlKeyMask as u64 == appkit::NSControlKeyMask as u64 {
|
|
||||||
modifiers = modifiers | events::LEFT_CONTROL_MODIFIER;
|
|
||||||
}
|
|
||||||
if flag & appkit::NSAlternateKeyMask as u64 == appkit::NSAlternateKeyMask as u64 {
|
|
||||||
modifiers = modifiers | events::LEFT_ALT_MODIFIER;
|
|
||||||
}
|
|
||||||
if flag & appkit::NSNumericPadKeyMask as u64 == appkit::NSNumericPadKeyMask as u64 {
|
|
||||||
modifiers = modifiers | events::NUM_LOCK_MODIFIER;
|
|
||||||
}
|
|
||||||
modifiers
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn vkeycode_to_element(code: u16) -> Option<events::VirtualKeyCode> {
|
pub fn vkeycode_to_element(code: u16) -> Option<events::VirtualKeyCode> {
|
||||||
Some(match code {
|
Some(match code {
|
||||||
0x00 => events::A,
|
0x00 => events::A,
|
||||||
|
|
|
@ -18,7 +18,7 @@ use core_foundation::bundle::{CFBundleGetBundleWithIdentifier, CFBundleGetFuncti
|
||||||
|
|
||||||
use std::c_str::CString;
|
use std::c_str::CString;
|
||||||
use {MouseInput, Pressed, Released, LeftMouseButton, RightMouseButton, MouseMoved, ReceivedCharacter,
|
use {MouseInput, Pressed, Released, LeftMouseButton, RightMouseButton, MouseMoved, ReceivedCharacter,
|
||||||
KeyboardInput, KeyModifiers};
|
KeyboardInput};
|
||||||
|
|
||||||
use events;
|
use events;
|
||||||
|
|
||||||
|
@ -252,13 +252,11 @@ impl Window {
|
||||||
}
|
}
|
||||||
|
|
||||||
let vkey = event::vkeycode_to_element(event.keycode());
|
let vkey = event::vkeycode_to_element(event.keycode());
|
||||||
let modifiers = event::modifierflag_to_element(event.modifierFlags());
|
events.push(KeyboardInput(Pressed, event.keycode() as u8, vkey));
|
||||||
events.push(KeyboardInput(Pressed, event.keycode() as u8, vkey, modifiers));
|
|
||||||
},
|
},
|
||||||
NSKeyUp => {
|
NSKeyUp => {
|
||||||
let vkey = event::vkeycode_to_element(event.keycode());
|
let vkey = event::vkeycode_to_element(event.keycode());
|
||||||
let modifiers = event::modifierflag_to_element(event.modifierFlags());
|
events.push(KeyboardInput(Released, event.keycode() as u8, vkey));
|
||||||
events.push(KeyboardInput(Released, event.keycode() as u8, vkey, modifiers));
|
|
||||||
},
|
},
|
||||||
NSFlagsChanged => {
|
NSFlagsChanged => {
|
||||||
let shift_modifier = Window::modifier_event(event, appkit::NSShiftKeyMask as u64, events::LShift, shift_pressed);
|
let shift_modifier = Window::modifier_event(event, appkit::NSShiftKeyMask as u64, events::LShift, shift_pressed);
|
||||||
|
@ -295,10 +293,10 @@ impl Window {
|
||||||
|
|
||||||
unsafe fn modifier_event(event: id, keymask: u64, key: events::VirtualKeyCode, key_pressed: bool) -> Option<Event> {
|
unsafe fn modifier_event(event: id, keymask: u64, key: events::VirtualKeyCode, key_pressed: bool) -> Option<Event> {
|
||||||
if !key_pressed && Window::modifier_key_pressed(event, keymask) {
|
if !key_pressed && Window::modifier_key_pressed(event, keymask) {
|
||||||
return Some(KeyboardInput(Pressed, event.keycode() as u8, Some(key), KeyModifiers::empty()));
|
return Some(KeyboardInput(Pressed, event.keycode() as u8, Some(key)));
|
||||||
}
|
}
|
||||||
else if key_pressed && !Window::modifier_key_pressed(event, keymask) {
|
else if key_pressed && !Window::modifier_key_pressed(event, keymask) {
|
||||||
return Some(KeyboardInput(Released, event.keycode() as u8, Some(key), KeyModifiers::empty()));
|
return Some(KeyboardInput(Released, event.keycode() as u8, Some(key)));
|
||||||
}
|
}
|
||||||
|
|
||||||
return None;
|
return None;
|
||||||
|
|
|
@ -485,18 +485,18 @@ extern "stdcall" fn callback(window: ffi::HWND, msg: ffi::UINT,
|
||||||
},
|
},
|
||||||
|
|
||||||
ffi::WM_KEYDOWN => {
|
ffi::WM_KEYDOWN => {
|
||||||
use events::{KeyboardInput, KeyModifiers, Pressed};
|
use events::{KeyboardInput, Pressed};
|
||||||
let scancode = ((lparam >> 16) & 0xff) as u8;
|
let scancode = ((lparam >> 16) & 0xff) as u8;
|
||||||
let vkey = event::vkeycode_to_element(wparam);
|
let vkey = event::vkeycode_to_element(wparam);
|
||||||
send_event(window, KeyboardInput(Pressed, scancode, vkey, KeyModifiers::empty()));
|
send_event(window, KeyboardInput(Pressed, scancode, vkey));
|
||||||
0
|
0
|
||||||
},
|
},
|
||||||
|
|
||||||
ffi::WM_KEYUP => {
|
ffi::WM_KEYUP => {
|
||||||
use events::{KeyboardInput, KeyModifiers, Released};
|
use events::{KeyboardInput, Released};
|
||||||
let scancode = ((lparam >> 16) & 0xff) as u8;
|
let scancode = ((lparam >> 16) & 0xff) as u8;
|
||||||
let vkey = event::vkeycode_to_element(wparam);
|
let vkey = event::vkeycode_to_element(wparam);
|
||||||
send_event(window, KeyboardInput(Released, scancode, vkey, KeyModifiers::empty()));
|
send_event(window, KeyboardInput(Released, scancode, vkey));
|
||||||
0
|
0
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
use {Event, WindowBuilder, KeyModifiers};
|
use {Event, WindowBuilder};
|
||||||
use {CreationError, OsError};
|
use {CreationError, OsError};
|
||||||
use libc;
|
use libc;
|
||||||
use std::{mem, ptr};
|
use std::{mem, ptr};
|
||||||
|
@ -33,7 +33,6 @@ pub struct Window {
|
||||||
xf86_desk_mode: *mut ffi::XF86VidModeModeInfo,
|
xf86_desk_mode: *mut ffi::XF86VidModeModeInfo,
|
||||||
screen_id: libc::c_int,
|
screen_id: libc::c_int,
|
||||||
is_fullscreen: bool,
|
is_fullscreen: bool,
|
||||||
current_modifiers: Cell<KeyModifiers>,
|
|
||||||
current_size: Cell<(libc::c_int, libc::c_int)>,
|
current_size: Cell<(libc::c_int, libc::c_int)>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -265,7 +264,6 @@ impl Window {
|
||||||
xf86_desk_mode: xf86_desk_mode,
|
xf86_desk_mode: xf86_desk_mode,
|
||||||
screen_id: screen_id,
|
screen_id: screen_id,
|
||||||
is_fullscreen: builder.monitor.is_some(),
|
is_fullscreen: builder.monitor.is_some(),
|
||||||
current_modifiers: Cell::new(KeyModifiers::empty()),
|
|
||||||
current_size: Cell::new((0, 0)),
|
current_size: Cell::new((0, 0)),
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -399,9 +397,6 @@ impl Window {
|
||||||
|
|
||||||
ffi::KeyPress | ffi::KeyRelease => {
|
ffi::KeyPress | ffi::KeyRelease => {
|
||||||
use {KeyboardInput, Pressed, Released, ReceivedCharacter};
|
use {KeyboardInput, Pressed, Released, ReceivedCharacter};
|
||||||
use {LEFT_CONTROL_MODIFIER, RIGHT_CONTROL_MODIFIER};
|
|
||||||
use {LEFT_SHIFT_MODIFIER, RIGHT_SHIFT_MODIFIER};
|
|
||||||
use {LEFT_ALT_MODIFIER, RIGHT_ALT_MODIFIER, CAPS_LOCK_MODIFIER};
|
|
||||||
let event: &mut ffi::XKeyEvent = unsafe { mem::transmute(&xev) };
|
let event: &mut ffi::XKeyEvent = unsafe { mem::transmute(&xev) };
|
||||||
|
|
||||||
if event.type_ == ffi::KeyPress {
|
if event.type_ == ffi::KeyPress {
|
||||||
|
@ -432,33 +427,9 @@ impl Window {
|
||||||
ffi::XKeycodeToKeysym(self.display, event.keycode as ffi::KeyCode, 0)
|
ffi::XKeycodeToKeysym(self.display, event.keycode as ffi::KeyCode, 0)
|
||||||
};
|
};
|
||||||
|
|
||||||
let modifier_flag = match keysym as u32 {
|
|
||||||
ffi::XK_Shift_L => Some(LEFT_SHIFT_MODIFIER),
|
|
||||||
ffi::XK_Shift_R => Some(RIGHT_SHIFT_MODIFIER),
|
|
||||||
ffi::XK_Control_L => Some(LEFT_CONTROL_MODIFIER),
|
|
||||||
ffi::XK_Control_R => Some(RIGHT_CONTROL_MODIFIER),
|
|
||||||
ffi::XK_Caps_Lock => Some(CAPS_LOCK_MODIFIER),
|
|
||||||
ffi::XK_Meta_L => Some(LEFT_ALT_MODIFIER),
|
|
||||||
ffi::XK_Meta_R => Some(RIGHT_ALT_MODIFIER),
|
|
||||||
_ => None,
|
|
||||||
};
|
|
||||||
match modifier_flag {
|
|
||||||
Some(flag) => {
|
|
||||||
let mut current_modifiers = self.current_modifiers.get();
|
|
||||||
match state {
|
|
||||||
Pressed => current_modifiers.insert(flag),
|
|
||||||
Released => current_modifiers.remove(flag),
|
|
||||||
}
|
|
||||||
self.current_modifiers.set(current_modifiers);
|
|
||||||
}
|
|
||||||
None => {}
|
|
||||||
}
|
|
||||||
|
|
||||||
let vkey = events::keycode_to_element(keysym as libc::c_uint);
|
let vkey = events::keycode_to_element(keysym as libc::c_uint);
|
||||||
|
|
||||||
events.push(KeyboardInput(state, event.keycode as u8,
|
events.push(KeyboardInput(state, event.keycode as u8, vkey));
|
||||||
vkey, self.current_modifiers.get()));
|
|
||||||
//
|
|
||||||
},
|
},
|
||||||
|
|
||||||
ffi::ButtonPress | ffi::ButtonRelease => {
|
ffi::ButtonPress | ffi::ButtonRelease => {
|
||||||
|
|
Loading…
Reference in a new issue