For some definition of improve, at least. On Windows, ALT is basically reserved by the Window management layer for functions such as ALT-space, ALT-F4 and so on. Windows doesn't provide a method by which an application can test whether a given key would be processed by the default window procedure so we're in a bit of a bind in terms of allowing ALT+a keypress to do something meaningful in the terminal. What I've settled on for now is: On Windows only, if ALT is pressed, allow matching key assignments that include ALT to be matched. If there are no key assignments, then DON'T pass the key press to the active pane, and instead allow it to be passed to DefWindowProc. This allows ALT-space to be handled correctly, provided the user hasn't defined an ALT-space key assignment of their own. This may have some unforeseen consequences. For example, ALT-<number> is a readline binding that repeats an argument a number of times. This change "breaks" that, but the user can provide a key assignment to `SendString` the equivalent sequence to restore that behavior. I'm kindof hoping that no one notices, but I'm prepared to explicitly add default key assignments for that. The other aspect of this commit is that I now understand a bit better what a dead key is and how they should be handled. I've tested the behavior of wezterm with these changes and the behavior is consistent with a regular CMD window when I have the DEU keymap active. Specifically, using the on-screen keyboard, if I click `^` then click `e` wezterm will emit `ê`. If I click `^` then `^` then wezterm emits `^^`. refs: #275 refs: #296
220 lines
5.4 KiB
Rust
220 lines
5.4 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;
|
|
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
|
|
}
|
|
}
|