Skip to content

Commit

Permalink
Support key release events for windows. (#745)
Browse files Browse the repository at this point in the history
  • Loading branch information
TimonPost authored Jan 28, 2023
1 parent a993a98 commit 318f810
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 22 deletions.
2 changes: 1 addition & 1 deletion src/cursor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ impl Command for Show {
}

/// A command that enables blinking of the terminal cursor.
///
///
/// # Notes
///
/// - Some Unix terminals (ex: GNOME and Konsole) as well as Windows versions lower than Windows 10 do not support this functionality.
Expand Down
10 changes: 2 additions & 8 deletions src/cursor/sys/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,20 +134,14 @@ impl ScreenBufferCursor {
if x < 0 {
return Err(io::Error::new(
io::ErrorKind::Other,
format!(
"Argument Out of Range Exception when setting cursor position to X: {}",
x
),
format!("Argument Out of Range Exception when setting cursor position to X: {x}"),
));
}

if y < 0 {
return Err(io::Error::new(
io::ErrorKind::Other,
format!(
"Argument Out of Range Exception when setting cursor position to Y: {}",
y
),
format!("Argument Out of Range Exception when setting cursor position to Y: {y}"),
));
}

Expand Down
4 changes: 3 additions & 1 deletion src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -643,7 +643,9 @@ pub struct KeyEvent {
pub modifiers: KeyModifiers,
/// Kind of event.
///
/// Only set if [`KeyboardEnhancementFlags::REPORT_EVENT_TYPES`] has been enabled with [`PushKeyboardEnhancementFlags`].
/// Only set if:
/// - Unix: [`KeyboardEnhancementFlags::REPORT_EVENT_TYPES`] has been enabled with [`PushKeyboardEnhancementFlags`].
/// - Windows: always
pub kind: KeyEventKind,
/// Keyboard state.
///
Expand Down
20 changes: 13 additions & 7 deletions src/event/sys/windows/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use winapi::um::{
};

use crate::{
event::{Event, KeyCode, KeyEvent, KeyModifiers, MouseButton, MouseEventKind},
event::{Event, KeyCode, KeyEvent, KeyEventKind, KeyModifiers, MouseButton, MouseEventKind},
Result,
};

Expand Down Expand Up @@ -221,7 +221,12 @@ fn parse_key_event_record(key_event: &KeyEventRecord) -> Option<WindowsKeyEvent>
// values.
let ch = std::char::from_u32(unicode_scalar_value as u32).unwrap();
let key_code = KeyCode::Char(ch);
let key_event = KeyEvent::new(key_code, modifiers);
let kind = if key_event.key_down {
KeyEventKind::Press
} else {
KeyEventKind::Release
};
let key_event = KeyEvent::new_with_kind(key_code, modifiers, kind);
return Some(WindowsKeyEvent::KeyEvent(key_event));
}
}
Expand All @@ -235,10 +240,6 @@ fn parse_key_event_record(key_event: &KeyEventRecord) -> Option<WindowsKeyEvent>
return None;
}

if !key_event.key_down {
return None;
}

let parse_result = match virtual_key_code {
VK_SHIFT | VK_CONTROL | VK_MENU => None,
VK_BACK => Some(KeyCode::Backspace),
Expand Down Expand Up @@ -283,7 +284,12 @@ fn parse_key_event_record(key_event: &KeyEventRecord) -> Option<WindowsKeyEvent>
};

if let Some(key_code) = parse_result {
let key_event = KeyEvent::new(key_code, modifiers);
let kind = if key_event.key_down {
KeyEventKind::Press
} else {
KeyEventKind::Release
};
let key_event = KeyEvent::new_with_kind(key_code, modifiers, kind);
return Some(WindowsKeyEvent::KeyEvent(key_event));
}

Expand Down
4 changes: 2 additions & 2 deletions src/style/types/colored.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,8 @@ impl fmt::Display for Colored {
Color::DarkCyan => f.write_str("5;6"),
Color::White => f.write_str("5;15"),
Color::Grey => f.write_str("5;7"),
Color::Rgb { r, g, b } => write!(f, "2;{};{};{}", r, g, b),
Color::AnsiValue(val) => write!(f, "5;{}", val),
Color::Rgb { r, g, b } => write!(f, "2;{r};{g};{b}"),
Color::AnsiValue(val) => write!(f, "5;{val}"),
_ => Ok(()),
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/terminal/sys/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,13 +195,13 @@ pub(crate) fn set_size(width: u16, height: u16) -> Result<()> {
if width > bounds.x {
return Err(ErrorKind::new(
io::ErrorKind::InvalidInput,
format!("terminal width {} too large", width),
format!("terminal width {width} too large"),
));
}
if height > bounds.y {
return Err(ErrorKind::new(
io::ErrorKind::InvalidInput,
format!("terminal height {} too large", height),
format!("terminal height {height} too large"),
));
}

Expand All @@ -218,7 +218,7 @@ pub(crate) fn set_window_title(title: impl fmt::Display) -> Result<()> {
}

let mut title_utf16 = Utf16Encoder(Vec::new());
write!(title_utf16, "{}", title).expect("formatting failed");
write!(title_utf16, "{title}").expect("formatting failed");
title_utf16.0.push(0);
let title = title_utf16.0;

Expand Down

0 comments on commit 318f810

Please sign in to comment.