Files
LLeMbas-TTY/window/src/input.rs
T
Wez Furlong 9ff9bf15fd window: allow for stronger separation between raw and composed keys
The goal at the window layer is to preserve enough useful information
for other layers.  In this specific circumstance on macos we'd like
to be able know both that eg: ALT-1 was pressed and that ALT-1 composes
to a different unmodified sequence and then allow the user's key
binding assignment to potentially match on both.

We sort of allowed for this, but didn't separate out the modifier keys.
This commit adds a `raw_modifiers` concept to the underlying event
struct so that we can carry both the raw key and modifier information
as well as the composed key and modifier information.

In the scenario above, we want the raw key/modifier tuple to be ALT-1
but the composed key/modifier to be eg: unmodified `¡` in my english
keymap.

refs: https://github.com/wez/wezterm/issues/158
2020-05-28 19:35:50 -07:00

156 lines
3.3 KiB
Rust

use bitflags::*;
/// Which key is pressed. Not all of these are probable to appear
/// on most systems. A lot of this list is @wez trawling docs and
/// making an entry for things that might be possible in this first pass.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum KeyCode {
/// The decoded unicode character
Char(char),
Composed(String),
Hyper,
Super,
Meta,
/// Ctrl-break on windows
Cancel,
// There is no `Backspace`; use `Char('\u{8}') instead
// There is no `Tab`; use `Char('\t')` instead
Clear,
// There is no `Enter`; use `Char('\r')` instead
Shift,
// There is no `Escape`; use `Char('\u{1b}') instead
LeftShift,
RightShift,
Control,
LeftControl,
RightControl,
Alt,
LeftAlt,
RightAlt,
Pause,
CapsLock,
PageUp,
PageDown,
End,
Home,
LeftArrow,
RightArrow,
UpArrow,
DownArrow,
Select,
Print,
Execute,
PrintScreen,
Insert,
// There is no `Delete`; use `Char('\u{7f}')` instead
Help,
LeftWindows,
RightWindows,
Applications,
Sleep,
/// Numeric keypad digits 0-9
Numpad(u8),
Multiply,
Add,
Separator,
Subtract,
Decimal,
Divide,
/// F1-F24 are possible
Function(u8),
NumLock,
ScrollLock,
BrowserBack,
BrowserForward,
BrowserRefresh,
BrowserStop,
BrowserSearch,
BrowserFavorites,
BrowserHome,
VolumeMute,
VolumeDown,
VolumeUp,
MediaNextTrack,
MediaPrevTrack,
MediaStop,
MediaPlayPause,
ApplicationLeftArrow,
ApplicationRightArrow,
ApplicationUpArrow,
ApplicationDownArrow,
}
bitflags! {
#[derive(Default)]
pub struct Modifiers: u8 {
const NONE = 0;
const SHIFT = 1<<1;
const ALT = 1<<2;
const CTRL = 1<<3;
const SUPER = 1<<4;
}
}
bitflags! {
#[derive(Default)]
pub struct MouseButtons: u8 {
const NONE = 0;
#[allow(clippy::identity_op)]
const LEFT = 1<<0;
const RIGHT = 1<<1;
const MIDDLE = 1<<2;
const X1 = 1<<3;
const X2 = 1<<4;
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum MousePress {
Left,
Right,
Middle,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum MouseEventKind {
Move,
Press(MousePress),
Release(MousePress),
VertWheel(i16),
HorzWheel(i16),
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct MouseEvent {
pub kind: MouseEventKind,
/// Coordinates of the mouse relative to the top left of the window
pub coords: crate::Point,
/// The mouse position in screen coordinates
pub screen_coords: crate::ScreenPoint,
pub mouse_buttons: MouseButtons,
pub modifiers: Modifiers,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct KeyEvent {
/// Which key was pressed.
/// This is the potentially processed/composed version
/// of the input.
pub key: KeyCode,
/// Which modifiers are down
pub modifiers: Modifiers,
/// The raw unprocessed key press if it was different from
/// the processed/composed version
pub raw_key: Option<KeyCode>,
pub raw_modifiers: Modifiers,
/// How many times this key repeats
pub repeat_count: u16,
/// If true, this is a key down rather than a key up event
pub key_is_down: bool,
}