Files
LLeMbas-TTY/window/src/input.rs
T
Wez Furlong e2311aaa73 window: more fun with dead keys on Windows
This is a bit more involved than I'd like, but it seems more
deterministic than using `TranslateMessage` or `ToUnicode` in all cases.

This commit expands the depth of the keyboard layout probing that
is performed when we detect a changed keyboard layout.

We know detect starting `(Modifier, VK) -> char` for a dead key press,
as well as the map of terminating `(Modifier, VK) -> char` for valid
dead key presses.

This information allows us to simply lookup the mapping without
calling `ToUnicode`.  Avoiding `ToUnicode` is desirable because it
maintains a global state and it is unpredictable what else is
manipulating that same state.  In particular, for the ESP keyboard
layout where `~` is a dead key that is reached via `AltGr 4`, there
doesn't appear to be a reliable way to extract the correct mapping
from it when calling `ToUnicode` in response to the various KEYUP,
KEYDOWN messages.   We could get it if we always called
`TranslateMessage` and only looked at `WM_CHAR`, but that means that
we cannot decompose `WM_CHAR` back to the raw key events when we
need to.  Bleh!

Test Plan for this commit:

* With ENG layout active, check that CTRL, ALT and so on have the
  intended effect in the terminal; eg: CTRL-C, CTRL-W (in vim).
* Switch to pinyin layout, check that typing still invokes the
  IME and that it can insert text
* Switch to DEU.  Check that `AltGr m` produces a `mu` symbol.
  Check that grave (`\``) (a dead key) doesn't immediately output
  anything, then press `e`; that produces an `e` with a grave
  diacritic.  Grave followed by space emits grave.  Grave
  followed by grave emits a grave and holds the second grave; pressing
  `e` at this point now emits `e` with a grave diacritic.
  (This is a difference from the "normal" system behavior, which
  would just emit two graves in a row, then a regular `e`).
* Switch to ESP.  Check that `AltGr 4` (tilde) doesn't immediately
  output anything, then press `n`; that produces an `n` with the
  tilde diacritic.
* Change `use_dead_keys = false`.  Now verify in DEU that `grave`
  just emits grave.  In ESP, verify that `AltGr 4` just emits
  a tilde.
* Switch back to ENG.  Verify that `ALT-space` pops up the system
  menu.

refs: #275
refs: #305
2020-10-23 08:47:11 -07:00

243 lines
6.0 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,
}
impl KeyCode {
/// Return true if the key represents a modifier key.
pub fn is_modifier(&self) -> bool {
match self {
Self::Hyper
| Self::Super
| Self::Meta
| Self::Shift
| Self::LeftShift
| Self::RightShift
| Self::Control
| Self::LeftControl
| Self::RightControl
| Self::Alt
| Self::LeftAlt
| Self::RightAlt
| Self::LeftWindows
| Self::RightWindows => true,
_ => false,
}
}
}
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;
const LEFT_ALT = 1<<5;
const RIGHT_ALT = 1<<6;
}
}
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,
}
fn normalize_shift(key: KeyCode, modifiers: Modifiers) -> (KeyCode, Modifiers) {
if modifiers.contains(Modifiers::SHIFT) {
match key {
KeyCode::Char(c) if c.is_ascii_uppercase() => (key, modifiers - Modifiers::SHIFT),
KeyCode::Char(c) if c.is_ascii_lowercase() => (
KeyCode::Char(c.to_ascii_uppercase()),
modifiers - Modifiers::SHIFT,
),
_ => (key, modifiers),
}
} else {
(key, modifiers)
}
}
fn normalize_ctrl(key: KeyCode, modifiers: Modifiers) -> (KeyCode, Modifiers) {
if modifiers.contains(Modifiers::CTRL) {
if let KeyCode::Char(c) = key {
if (c as u32) < 0x20 {
let de_ctrl = ((c as u8) | 0x40) as char;
return (KeyCode::Char(de_ctrl.to_ascii_lowercase()), modifiers);
}
}
}
(key, modifiers)
}
impl KeyEvent {
/// if SHIFT is held and we have KeyCode::Char('c') we want to normalize
/// that keycode to KeyCode::Char('C'); that is what this function does.
pub fn normalize_shift(mut self) -> Self {
let (key, modifiers) = normalize_shift(self.key, self.modifiers);
self.key = key;
self.modifiers = modifiers;
if let Some(raw) = self.raw_key.take() {
let (key, modifiers) = normalize_shift(raw, self.raw_modifiers);
self.raw_key.replace(key);
self.raw_modifiers = modifiers;
}
self
}
/// If CTRL is held down and we have KeyCode::Char(_) with the
/// ASCII control value encoded, decode it back to the ASCII
/// alpha keycode instead.
pub fn normalize_ctrl(mut self) -> Self {
let (key, modifiers) = normalize_ctrl(self.key, self.modifiers);
self.key = key;
self.modifiers = modifiers;
if let Some(raw) = self.raw_key.take() {
let (key, modifiers) = normalize_ctrl(raw, self.raw_modifiers);
self.raw_key.replace(key);
self.raw_modifiers = modifiers;
}
self
}
}