Handle text input from keyboards better

This commit is contained in:
Richard Feldman 2020-05-30 01:42:46 -04:00
parent c734c7779a
commit fe218a5966

View file

@ -111,17 +111,32 @@ fn run_event_loop() -> Result<(), Box<dyn Error>> {
},
);
}
winit::event::Event::WindowEvent {
event: winit::event::WindowEvent::ReceivedCharacter(ch),
..
} => {
match ch {
'\u{8}' => {
// Backspace deletes a character.
text_state.pop();
}
'\u{e000}'..='\u{f8ff}'
| '\u{f0000}'..='\u{ffffd}'
| '\u{100000}'..='\u{10fffd}' => {
// These are private use characters; ignore them.
// See http://www.unicode.org/faq/private_use.html
}
_ => {
text_state.push(ch);
}
}
}
winit::event::Event::WindowEvent {
event: winit::event::WindowEvent::KeyboardInput { input, .. },
..
} => {
if let Some(virtual_keycode) = input.virtual_keycode {
handle_text_input(
&mut text_state,
input.state,
virtual_keycode,
keyboard_modifiers,
);
handle_keydown(input.state, virtual_keycode, keyboard_modifiers);
}
}
winit::event::Event::WindowEvent {
@ -171,7 +186,7 @@ fn run_event_loop() -> Result<(), Box<dyn Error>> {
glyph_brush.queue(Section {
screen_position: (30.0, 90.0),
bounds: (size.width as f32, size.height as f32),
text: vec![Text::new(text_state.as_str())
text: vec![Text::new(format!("{}|", text_state).as_str())
.with_color([1.0, 1.0, 1.0, 1.0])
.with_scale(40.0)],
..Section::default()
@ -191,8 +206,7 @@ fn run_event_loop() -> Result<(), Box<dyn Error>> {
})
}
fn handle_text_input(
text_state: &mut String,
fn handle_keydown(
elem_state: ElementState,
virtual_keycode: VirtualKeyCode,
_modifiers: ModifiersState,
@ -204,109 +218,6 @@ fn handle_text_input(
}
match virtual_keycode {
Key1 | Numpad1 => text_state.push_str("1"),
Key2 | Numpad2 => text_state.push_str("2"),
Key3 | Numpad3 => text_state.push_str("3"),
Key4 | Numpad4 => text_state.push_str("4"),
Key5 | Numpad5 => text_state.push_str("5"),
Key6 | Numpad6 => text_state.push_str("6"),
Key7 | Numpad7 => text_state.push_str("7"),
Key8 | Numpad8 => text_state.push_str("8"),
Key9 | Numpad9 => text_state.push_str("9"),
Key0 | Numpad0 => text_state.push_str("0"),
A => text_state.push_str("a"),
B => text_state.push_str("b"),
C => text_state.push_str("c"),
D => text_state.push_str("d"),
E => text_state.push_str("e"),
F => text_state.push_str("f"),
G => text_state.push_str("g"),
H => text_state.push_str("h"),
I => text_state.push_str("i"),
J => text_state.push_str("j"),
K => text_state.push_str("k"),
L => text_state.push_str("l"),
M => text_state.push_str("m"),
N => text_state.push_str("n"),
O => text_state.push_str("o"),
P => text_state.push_str("p"),
Q => text_state.push_str("q"),
R => text_state.push_str("r"),
S => text_state.push_str("s"),
T => text_state.push_str("t"),
U => text_state.push_str("u"),
V => text_state.push_str("v"),
W => text_state.push_str("w"),
X => text_state.push_str("x"),
Y => text_state.push_str("y"),
Z => text_state.push_str("z"),
Escape | F1 | F2 | F3 | F4 | F5 | F6 | F7 | F8 | F9 | F10 | F11 | F12 | F13 | F14 | F15
| F16 | F17 | F18 | F19 | F20 | F21 | F22 | F23 | F24 | Snapshot | Scroll | Pause
| Insert | Home | Delete | End | PageDown | PageUp | Left | Up | Right | Down | Compose
| Caret | Numlock | AbntC1 | AbntC2 | Ax | Calculator | Capital | Convert | Kana
| Kanji | LAlt | LBracket | LControl | LShift | LWin | Mail | MediaSelect | PlayPause
| Power | PrevTrack | MediaStop | Mute | MyComputer | NavigateForward
| NavigateBackward | NextTrack | NoConvert | OEM102 | RAlt | Sysrq | RBracket
| RControl | RShift | RWin | Sleep | Stop | Unlabeled | VolumeDown | VolumeUp | Wake
| WebBack | WebFavorites | WebForward | WebHome | WebRefresh | WebSearch | Apps | Tab
| WebStop => {
// TODO handle
dbg!(virtual_keycode);
}
Back => {
text_state.pop();
}
Return | NumpadEnter => {
text_state.push_str("\n");
}
Space => {
text_state.push_str(" ");
}
Comma | NumpadComma => {
text_state.push_str(",");
}
Add => {
text_state.push_str("+");
}
Apostrophe => {
text_state.push_str("'");
}
At => {
text_state.push_str("@");
}
Backslash => {
text_state.push_str("\\");
}
Colon => {
text_state.push_str(":");
}
Period | Decimal => {
text_state.push_str(".");
}
Equals | NumpadEquals => {
text_state.push_str("=");
}
Grave => {
text_state.push_str("`");
}
Minus | Subtract => {
text_state.push_str("-");
}
Multiply => {
text_state.push_str("*");
}
Semicolon => {
text_state.push_str(";");
}
Slash | Divide => {
text_state.push_str("/");
}
Underline => {
text_state.push_str("_");
}
Yen => {
text_state.push_str("¥");
}
Copy => {
todo!("copy");
}
@ -316,5 +227,6 @@ fn handle_text_input(
Cut => {
todo!("cut");
}
_ => {}
}
}