From aec5cc76b42fbe8a453a5e5402864b2a27edf839 Mon Sep 17 00:00:00 2001 From: sawyer bristol Date: Wed, 18 Sep 2024 14:51:52 -0600 Subject: [PATCH] fix keys including backspace and enter --- src/engines/ultralight.rs | 648 ++++++++++++++++++++++++++++---------- 1 file changed, 490 insertions(+), 158 deletions(-) diff --git a/src/engines/ultralight.rs b/src/engines/ultralight.rs index a46a11d..794f8b6 100644 --- a/src/engines/ultralight.rs +++ b/src/engines/ultralight.rs @@ -2,6 +2,7 @@ use iced::keyboard::{self}; use iced::mouse::{self, ScrollDelta}; use iced::{Point, Size}; use smol_str::SmolStr; +use std::str::FromStr; use std::sync::{Arc, RwLock}; use ul_next::{ config::Config, @@ -374,7 +375,7 @@ impl BrowserEngine for Ultralight { } } -#[derive(PartialEq, Eq)] +#[derive(Debug, PartialEq, Eq)] enum KeyPress { Press, Unpress, @@ -387,185 +388,516 @@ fn iced_key_to_ultralight_key( modifiers: keyboard::Modifiers, text: Option, ) -> Option { - let mut ty = match press { - KeyPress::Press => event::KeyEventType::KeyDown, - KeyPress::Unpress => event::KeyEventType::KeyUp, - }; - let modifiers = event::KeyEventModifiers { - alt: modifiers.alt(), - ctrl: modifiers.control(), - meta: modifiers.logo(), - shift: modifiers.shift(), - }; - - let (virtual_key, native_key) = { + let (text, virtual_key, native_key) = { if let Some(key) = key { - match key { - #[cfg(windows)] - keyboard::Key::Named(key) => match key { - keyboard::key::Named::Control => (VirtualKeyCode::Control, 17), - keyboard::key::Named::Shift => (VirtualKeyCode::Shift, 16), - keyboard::key::Named::Enter => (VirtualKeyCode::Return, 13), - keyboard::key::Named::Tab => (VirtualKeyCode::Tab, 9), - keyboard::key::Named::Space => (VirtualKeyCode::Space, 32), - keyboard::key::Named::ArrowDown => (VirtualKeyCode::Down, 40), - keyboard::key::Named::ArrowLeft => (VirtualKeyCode::Right, 37), - keyboard::key::Named::ArrowRight => (VirtualKeyCode::Up, 39), - keyboard::key::Named::ArrowUp => (VirtualKeyCode::Left, 33), - keyboard::key::Named::End => (VirtualKeyCode::End, 35), - keyboard::key::Named::Home => (VirtualKeyCode::Home, 36), - keyboard::key::Named::Backspace => (VirtualKeyCode::Back, 8), - keyboard::key::Named::Clear => (VirtualKeyCode::Clear, 12), - keyboard::key::Named::Delete => (VirtualKeyCode::Delete, 46), - keyboard::key::Named::Insert => (VirtualKeyCode::Insert, 45), - keyboard::key::Named::Escape => (VirtualKeyCode::Escape, 27), - keyboard::key::Named::F1 => (VirtualKeyCode::F1, 112), - keyboard::key::Named::F2 => (VirtualKeyCode::F2, 113), - keyboard::key::Named::F3 => (VirtualKeyCode::F3, 114), - keyboard::key::Named::F4 => (VirtualKeyCode::F4, 115), - keyboard::key::Named::F5 => (VirtualKeyCode::F5, 116), - keyboard::key::Named::F6 => (VirtualKeyCode::F6, 117), - keyboard::key::Named::F7 => (VirtualKeyCode::F7, 118), - keyboard::key::Named::F8 => (VirtualKeyCode::F8, 119), - keyboard::key::Named::F9 => (VirtualKeyCode::F9, 120), - keyboard::key::Named::F10 => (VirtualKeyCode::F10, 121), - keyboard::key::Named::F11 => (VirtualKeyCode::F11, 122), - keyboard::key::Named::F12 => (VirtualKeyCode::F12, 123), - _ => return None, + let text = match key { + keyboard::Key::Named(_) => String::from(""), + keyboard::Key::Character(_) => match text { + Some(text) => text.to_string(), + None => String::from(""), }, - #[cfg(unix)] + keyboard::Key::Unidentified => return None, + }; + let (virtual_key, native_key) = match key { keyboard::Key::Named(key) => match key { - keyboard::key::Named::Control => (VirtualKeyCode::Control, 29), - keyboard::key::Named::Shift => (VirtualKeyCode::Shift, 42), - keyboard::key::Named::Enter => (VirtualKeyCode::Return, 28), - keyboard::key::Named::Tab => (VirtualKeyCode::Tab, 15), - keyboard::key::Named::Space => (VirtualKeyCode::Space, 57), - keyboard::key::Named::ArrowDown => (VirtualKeyCode::Down, 108), - keyboard::key::Named::ArrowLeft => (VirtualKeyCode::Right, 106), - keyboard::key::Named::ArrowRight => (VirtualKeyCode::Up, 103), - keyboard::key::Named::ArrowUp => (VirtualKeyCode::Left, 105), - keyboard::key::Named::End => (VirtualKeyCode::End, 107), - keyboard::key::Named::Home => (VirtualKeyCode::Home, 102), - keyboard::key::Named::Backspace => (VirtualKeyCode::Back, 14), - keyboard::key::Named::Delete => (VirtualKeyCode::Delete, 11), - keyboard::key::Named::Insert => (VirtualKeyCode::Insert, 110), - keyboard::key::Named::Escape => (VirtualKeyCode::Escape, 1), - keyboard::key::Named::F1 => (VirtualKeyCode::F1, 59), - keyboard::key::Named::F2 => (VirtualKeyCode::F2, 60), - keyboard::key::Named::F3 => (VirtualKeyCode::F3, 61), - keyboard::key::Named::F4 => (VirtualKeyCode::F4, 62), - keyboard::key::Named::F5 => (VirtualKeyCode::F5, 63), - keyboard::key::Named::F6 => (VirtualKeyCode::F6, 64), - keyboard::key::Named::F7 => (VirtualKeyCode::F7, 65), - keyboard::key::Named::F8 => (VirtualKeyCode::F8, 66), - keyboard::key::Named::F9 => (VirtualKeyCode::F9, 67), - keyboard::key::Named::F10 => (VirtualKeyCode::F10, 68), - keyboard::key::Named::F11 => (VirtualKeyCode::F11, 69), - keyboard::key::Named::F12 => (VirtualKeyCode::F12, 70), + keyboard::key::Named::Control => ( + VirtualKeyCode::Control, + #[cfg(windows)] + 17, + #[cfg(unix)] + 29, + ), + keyboard::key::Named::Shift => ( + VirtualKeyCode::Shift, + #[cfg(windows)] + 16, + #[cfg(unix)] + 42, + ), + keyboard::key::Named::Enter => ( + VirtualKeyCode::Return, + #[cfg(windows)] + 13, + #[cfg(unix)] + 28, + ), + keyboard::key::Named::Tab => ( + VirtualKeyCode::Tab, + #[cfg(windows)] + 9, + #[cfg(unix)] + 15, + ), + keyboard::key::Named::Space => ( + VirtualKeyCode::Space, + #[cfg(windows)] + 32, + #[cfg(unix)] + 57, + ), + keyboard::key::Named::ArrowDown => ( + VirtualKeyCode::Down, + #[cfg(windows)] + 40, + #[cfg(unix)] + 108, + ), + keyboard::key::Named::ArrowLeft => ( + VirtualKeyCode::Right, + #[cfg(windows)] + 37, + #[cfg(unix)] + 106, + ), + keyboard::key::Named::ArrowRight => ( + VirtualKeyCode::Up, + #[cfg(windows)] + 39, + #[cfg(unix)] + 103, + ), + keyboard::key::Named::ArrowUp => ( + VirtualKeyCode::Left, + #[cfg(windows)] + 33, + #[cfg(unix)] + 105, + ), + keyboard::key::Named::End => ( + VirtualKeyCode::End, + #[cfg(windows)] + 35, + #[cfg(unix)] + 107, + ), + keyboard::key::Named::Home => ( + VirtualKeyCode::Home, + #[cfg(windows)] + 36, + #[cfg(unix)] + 102, + ), + keyboard::key::Named::Backspace => ( + VirtualKeyCode::Back, + #[cfg(windows)] + 8, + #[cfg(unix)] + 14, + ), + keyboard::key::Named::Delete => ( + VirtualKeyCode::Delete, + #[cfg(windows)] + 46, + #[cfg(unix)] + 11, + ), + keyboard::key::Named::Insert => ( + VirtualKeyCode::Insert, + #[cfg(windows)] + 45, + #[cfg(unix)] + 110, + ), + keyboard::key::Named::Escape => ( + VirtualKeyCode::Escape, + #[cfg(windows)] + 27, + #[cfg(unix)] + 1, + ), + keyboard::key::Named::F1 => ( + VirtualKeyCode::F1, + #[cfg(windows)] + 112, + #[cfg(unix)] + 59, + ), + keyboard::key::Named::F2 => ( + VirtualKeyCode::F2, + #[cfg(windows)] + 113, + #[cfg(unix)] + 60, + ), + keyboard::key::Named::F3 => ( + VirtualKeyCode::F3, + #[cfg(windows)] + 114, + #[cfg(unix)] + 61, + ), + keyboard::key::Named::F4 => ( + VirtualKeyCode::F4, + #[cfg(windows)] + 115, + #[cfg(unix)] + 62, + ), + keyboard::key::Named::F5 => ( + VirtualKeyCode::F5, + #[cfg(windows)] + 116, + #[cfg(unix)] + 63, + ), + keyboard::key::Named::F6 => ( + VirtualKeyCode::F6, + #[cfg(windows)] + 117, + #[cfg(unix)] + 64, + ), + keyboard::key::Named::F7 => ( + VirtualKeyCode::F7, + #[cfg(windows)] + 118, + #[cfg(unix)] + 65, + ), + keyboard::key::Named::F8 => ( + VirtualKeyCode::F8, + #[cfg(windows)] + 119, + #[cfg(unix)] + 66, + ), + keyboard::key::Named::F9 => ( + VirtualKeyCode::F9, + #[cfg(windows)] + 120, + #[cfg(unix)] + 67, + ), + keyboard::key::Named::F10 => ( + VirtualKeyCode::F10, + #[cfg(windows)] + 121, + #[cfg(unix)] + 68, + ), + keyboard::key::Named::F11 => ( + VirtualKeyCode::F11, + #[cfg(windows)] + 122, + #[cfg(unix)] + 69, + ), + keyboard::key::Named::F12 => ( + VirtualKeyCode::F12, + #[cfg(windows)] + 122, + #[cfg(unix)] + 70, + ), _ => return None, }, - #[cfg(windows)] keyboard::Key::Character(key) => match key.as_str() { - "a" => (VirtualKeyCode::A, 65), - "b" => (VirtualKeyCode::B, 66), - "c" => (VirtualKeyCode::C, 67), - "d" => (VirtualKeyCode::D, 68), - "e" => (VirtualKeyCode::E, 69), - "f" => (VirtualKeyCode::F, 70), - "g" => (VirtualKeyCode::G, 71), - "h" => (VirtualKeyCode::H, 72), - "i" => (VirtualKeyCode::I, 73), - "j" => (VirtualKeyCode::J, 74), - "k" => (VirtualKeyCode::K, 75), - "l" => (VirtualKeyCode::L, 76), - "m" => (VirtualKeyCode::M, 77), - "n" => (VirtualKeyCode::N, 78), - "o" => (VirtualKeyCode::O, 79), - "p" => (VirtualKeyCode::P, 80), - "q" => (VirtualKeyCode::Q, 81), - "r" => (VirtualKeyCode::R, 82), - "s" => (VirtualKeyCode::S, 83), - "t" => (VirtualKeyCode::T, 84), - "u" => (VirtualKeyCode::U, 85), - "v" => (VirtualKeyCode::V, 86), - "w" => (VirtualKeyCode::W, 87), - "x" => (VirtualKeyCode::X, 88), - "y" => (VirtualKeyCode::Y, 89), - "z" => (VirtualKeyCode::Z, 90), - "," => (VirtualKeyCode::OemComma, 188), - "." => (VirtualKeyCode::OemPeriod, 190), - ";" => (VirtualKeyCode::OemPeriod, 186), - _ => return None, - }, - #[cfg(unix)] - keyboard::Key::Character(key) => match key.as_str() { - "1" => (VirtualKeyCode::Key1, 2), - "2" => (VirtualKeyCode::Key2, 3), - "3" => (VirtualKeyCode::Key3, 4), - "4" => (VirtualKeyCode::Key4, 5), - "5" => (VirtualKeyCode::Key5, 6), - "6" => (VirtualKeyCode::Key6, 7), - "7" => (VirtualKeyCode::Key7, 8), - "8" => (VirtualKeyCode::Key8, 9), - "9" => (VirtualKeyCode::Key9, 10), - "0" => (VirtualKeyCode::Key0, 11), - "a" => (VirtualKeyCode::A, 30), - "b" => (VirtualKeyCode::B, 48), - "c" => (VirtualKeyCode::C, 46), - "d" => (VirtualKeyCode::D, 32), - "e" => (VirtualKeyCode::E, 18), - "f" => (VirtualKeyCode::F, 33), - "g" => (VirtualKeyCode::G, 34), - "h" => (VirtualKeyCode::H, 35), - "i" => (VirtualKeyCode::I, 23), - "j" => (VirtualKeyCode::J, 36), - "k" => (VirtualKeyCode::K, 37), - "l" => (VirtualKeyCode::L, 38), - "m" => (VirtualKeyCode::M, 50), - "n" => (VirtualKeyCode::N, 49), - "o" => (VirtualKeyCode::O, 24), - "p" => (VirtualKeyCode::P, 25), - "q" => (VirtualKeyCode::Q, 16), - "r" => (VirtualKeyCode::R, 19), - "s" => (VirtualKeyCode::S, 31), - "t" => (VirtualKeyCode::T, 20), - "u" => (VirtualKeyCode::U, 22), - "v" => (VirtualKeyCode::V, 47), - "w" => (VirtualKeyCode::W, 17), - "x" => (VirtualKeyCode::X, 47), - "y" => (VirtualKeyCode::Y, 21), - "z" => (VirtualKeyCode::Z, 44), - "," => (VirtualKeyCode::OemComma, 51), - "." => (VirtualKeyCode::OemPeriod, 52), - ";" => (VirtualKeyCode::OemPeriod, 39), + "a" => ( + VirtualKeyCode::A, + #[cfg(windows)] + 65, + #[cfg(unix)] + 30, + ), + "b" => ( + VirtualKeyCode::B, + #[cfg(windows)] + 66, + #[cfg(unix)] + 48, + ), + "c" => ( + VirtualKeyCode::C, + #[cfg(windows)] + 67, + #[cfg(unix)] + 46, + ), + "d" => ( + VirtualKeyCode::D, + #[cfg(windows)] + 68, + #[cfg(unix)] + 32, + ), + "e" => ( + VirtualKeyCode::E, + #[cfg(windows)] + 69, + #[cfg(unix)] + 18, + ), + "f" => ( + VirtualKeyCode::F, + #[cfg(windows)] + 70, + #[cfg(unix)] + 33, + ), + "g" => ( + VirtualKeyCode::G, + #[cfg(windows)] + 71, + #[cfg(unix)] + 34, + ), + "h" => ( + VirtualKeyCode::H, + #[cfg(windows)] + 72, + #[cfg(unix)] + 35, + ), + "i" => ( + VirtualKeyCode::I, + #[cfg(windows)] + 73, + #[cfg(unix)] + 23, + ), + "j" => ( + VirtualKeyCode::J, + #[cfg(windows)] + 74, + #[cfg(unix)] + 36, + ), + "k" => ( + VirtualKeyCode::K, + #[cfg(windows)] + 75, + #[cfg(unix)] + 37, + ), + "l" => ( + VirtualKeyCode::L, + #[cfg(windows)] + 76, + #[cfg(unix)] + 38, + ), + "m" => ( + VirtualKeyCode::M, + #[cfg(windows)] + 77, + #[cfg(unix)] + 50, + ), + "n" => ( + VirtualKeyCode::N, + #[cfg(windows)] + 78, + #[cfg(unix)] + 49, + ), + "o" => ( + VirtualKeyCode::O, + #[cfg(windows)] + 79, + #[cfg(unix)] + 24, + ), + "p" => ( + VirtualKeyCode::P, + #[cfg(windows)] + 80, + #[cfg(unix)] + 25, + ), + "q" => ( + VirtualKeyCode::Q, + #[cfg(windows)] + 81, + #[cfg(unix)] + 16, + ), + "r" => ( + VirtualKeyCode::R, + #[cfg(windows)] + 82, + #[cfg(unix)] + 19, + ), + "s" => ( + VirtualKeyCode::S, + #[cfg(windows)] + 83, + #[cfg(unix)] + 31, + ), + "t" => ( + VirtualKeyCode::T, + #[cfg(windows)] + 84, + #[cfg(unix)] + 20, + ), + "u" => ( + VirtualKeyCode::U, + #[cfg(windows)] + 85, + #[cfg(unix)] + 22, + ), + "v" => ( + VirtualKeyCode::V, + #[cfg(windows)] + 86, + #[cfg(unix)] + 47, + ), + "w" => ( + VirtualKeyCode::W, + #[cfg(windows)] + 87, + #[cfg(unix)] + 17, + ), + "x" => ( + VirtualKeyCode::X, + #[cfg(windows)] + 88, + #[cfg(unix)] + 47, + ), + "y" => ( + VirtualKeyCode::Y, + #[cfg(windows)] + 89, + #[cfg(unix)] + 21, + ), + "z" => ( + VirtualKeyCode::Z, + #[cfg(windows)] + 90, + #[cfg(unix)] + 44, + ), + "0" => ( + VirtualKeyCode::Key0, + #[cfg(windows)] + 48, + #[cfg(unix)] + 11, + ), + "1" => ( + VirtualKeyCode::Key1, + #[cfg(windows)] + 49, + #[cfg(unix)] + 2, + ), + "2" => ( + VirtualKeyCode::Key2, + #[cfg(windows)] + 50, + #[cfg(unix)] + 3, + ), + "3" => ( + VirtualKeyCode::Key3, + #[cfg(windows)] + 51, + #[cfg(unix)] + 4, + ), + "4" => ( + VirtualKeyCode::Key4, + #[cfg(windows)] + 52, + #[cfg(unix)] + 5, + ), + "5" => ( + VirtualKeyCode::Key5, + #[cfg(windows)] + 53, + #[cfg(unix)] + 6, + ), + "6" => ( + VirtualKeyCode::Key6, + #[cfg(windows)] + 54, + #[cfg(unix)] + 7, + ), + "7" => ( + VirtualKeyCode::Key7, + #[cfg(windows)] + 55, + #[cfg(unix)] + 8, + ), + "8" => ( + VirtualKeyCode::Key8, + #[cfg(windows)] + 56, + #[cfg(unix)] + 9, + ), + "9" => ( + VirtualKeyCode::Key9, + #[cfg(windows)] + 57, + #[cfg(unix)] + 10, + ), + "," => ( + VirtualKeyCode::OemComma, + #[cfg(windows)] + 188, + #[cfg(unix)] + 51, + ), + "." => ( + VirtualKeyCode::OemPeriod, + #[cfg(windows)] + 190, + #[cfg(unix)] + 52, + ), + ";" => ( + VirtualKeyCode::OemPeriod, + #[cfg(windows)] + 186, + #[cfg(unix)] + 39, + ), _ => return None, }, keyboard::Key::Unidentified => return None, - } + }; + (text, virtual_key, native_key) } else { return None; } }; - let text: &str = match text { - Some(text) => &text.to_string(), - None => "", + let ty = if !text.is_empty() && text.is_ascii() && press == KeyPress::Press { + event::KeyEventType::Char + } else { + match press { + KeyPress::Press => event::KeyEventType::RawKeyDown, + KeyPress::Unpress => event::KeyEventType::KeyUp, + } }; - // solution to char events being ignored: - // Note that only KeyEventType::Char events actually generate text in input fields. - // if text has char rewrite event type to char - if !text.is_empty() { - ty = event::KeyEventType::Char - } + let modifiers = event::KeyEventModifiers { + alt: modifiers.alt(), + ctrl: modifiers.control(), + meta: modifiers.logo(), + shift: modifiers.shift(), + }; let creation_info = KeyEventCreationInfo { ty, modifiers, virtual_key_code: virtual_key, native_key_code: native_key, - text, - unmodified_text: text, + text: text.as_str(), + unmodified_text: text.as_str(), is_keypad: false, is_auto_repeat: false, #[cfg(windows)]