diff options
author | bors-servo <lbergstrom+bors@mozilla.com> | 2016-07-06 02:51:50 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-07-06 02:51:50 -0700 |
commit | 68fb9ebc413f9cfc1ad4ca578d904c164836db74 (patch) | |
tree | ad09aee288d37a96a8f3b8fec61bc8f1eba93b91 /components/script/dom/keyboardevent.rs | |
parent | 23f5264e1c0b48ed0b04defdaa070a4336781d95 (diff) | |
parent | 6496d73210ed26b919b7910400d5a624e11bf646 (diff) | |
download | servo-68fb9ebc413f9cfc1ad4ca578d904c164836db74.tar.gz servo-68fb9ebc413f9cfc1ad4ca578d904c164836db74.zip |
Auto merge of #11950 - jdm:keylayout2, r=emilio
Support non-QWERTY keyboards
Using the ReceivedCharacter event from glutin, we can obtain the actual key characters that the user is pressing and releasing. This gets passed to the script thread along with the physical key data, since KeyboardEvent needs both pieces of information, where they get merged into a single logical key that gets processed by clients like TextInput without any special changes.
Tested by switching my macbook keyboard to dvorak and looking at the output of keypress/keyup/keydown event listeners, as well as playing with tests/html/textarea.html. Non-content keybindings like reload work as expected, too - the remapped keybinding triggers the reload action.
---
- [X] `./mach build -d` does not report any errors
- [X] `./mach test-tidy` does not report any errors
- [X] These changes fix #4144
- [X] These changes do not require tests because I can't think of a way to test remapped keyboard input
Fixes #11991.
<!-- Reviewable:start -->
---
This change is [<img src="https://reviewable.io/review_button.svg" height="35" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/servo/11950)
<!-- Reviewable:end -->
Diffstat (limited to 'components/script/dom/keyboardevent.rs')
-rw-r--r-- | components/script/dom/keyboardevent.rs | 42 |
1 files changed, 23 insertions, 19 deletions
diff --git a/components/script/dom/keyboardevent.rs b/components/script/dom/keyboardevent.rs index 4cfa1a61011..71b00767a8e 100644 --- a/components/script/dom/keyboardevent.rs +++ b/components/script/dom/keyboardevent.rs @@ -17,6 +17,7 @@ use dom::uievent::UIEvent; use dom::window::Window; use msg::constellation_msg; use msg::constellation_msg::{Key, KeyModifiers}; +use std::borrow::Cow; use std::cell::Cell; no_jsmanaged_fields!(Key); @@ -36,6 +37,7 @@ pub struct KeyboardEvent { is_composing: Cell<bool>, char_code: Cell<Option<u32>>, key_code: Cell<u32>, + printable: Cell<Option<char>>, } impl KeyboardEvent { @@ -54,6 +56,7 @@ impl KeyboardEvent { is_composing: Cell::new(false), char_code: Cell::new(None), key_code: Cell::new(0), + printable: Cell::new(None), } } @@ -69,6 +72,7 @@ impl KeyboardEvent { cancelable: bool, view: Option<&Window>, _detail: i32, + ch: Option<char>, key: Option<Key>, key_string: DOMString, code: DOMString, @@ -91,6 +95,7 @@ impl KeyboardEvent { ev.shift.set(shiftKey); ev.meta.set(metaKey); ev.char_code.set(char_code); + ev.printable.set(ch); ev.key_code.set(key_code); ev.is_composing.set(isComposing); ev @@ -103,7 +108,9 @@ impl KeyboardEvent { init.parent.parent.parent.bubbles, init.parent.parent.parent.cancelable, init.parent.parent.view.r(), - init.parent.parent.detail, key_from_string(&init.key, init.location), + init.parent.parent.detail, + None, + key_from_string(&init.key, init.location), init.key.clone(), init.code.clone(), init.location, init.repeat, init.isComposing, init.parent.ctrlKey, init.parent.altKey, init.parent.shiftKey, init.parent.metaKey, @@ -111,13 +118,13 @@ impl KeyboardEvent { Ok(event) } - pub fn key_properties(key: Key, mods: KeyModifiers) + pub fn key_properties(ch: Option<char>, key: Key, mods: KeyModifiers) -> KeyEventProperties { KeyEventProperties { - key_string: key_value(key, mods), + key_string: key_value(ch, key, mods), code: code_value(key), location: key_location(key), - char_code: key_charcode(key, mods), + char_code: ch.map(|ch| ch as u32), key_code: key_keycode(key), } } @@ -125,6 +132,10 @@ impl KeyboardEvent { impl KeyboardEvent { + pub fn printable(&self) -> Option<char> { + self.printable.get() + } + pub fn get_key(&self) -> Option<Key> { self.key.get().clone() } @@ -147,11 +158,14 @@ impl KeyboardEvent { } } - // https://w3c.github.io/uievents-key/#key-value-tables -pub fn key_value(key: Key, mods: KeyModifiers) -> &'static str { +pub fn key_value(ch: Option<char>, key: Key, mods: KeyModifiers) -> Cow<'static, str> { + if let Some(ch) = ch { + return Cow::from(format!("{}", ch)); + } + let shift = mods.contains(constellation_msg::SHIFT); - match key { + Cow::from(match key { Key::Space => " ", Key::Apostrophe if shift => "\"", Key::Apostrophe => "'", @@ -321,7 +335,7 @@ pub fn key_value(key: Key, mods: KeyModifiers) -> &'static str { Key::Menu => "ContextMenu", Key::NavigateForward => "BrowserForward", Key::NavigateBackward => "BrowserBack", - } + }) } fn key_from_string(key_string: &str, location: u32) -> Option<Key> { @@ -647,16 +661,6 @@ fn key_location(key: Key) -> u32 { } } -// https://w3c.github.io/uievents/#dom-keyboardevent-charcode -fn key_charcode(key: Key, mods: KeyModifiers) -> Option<u32> { - let key_string = key_value(key, mods); - if key_string.len() == 1 { - Some(key_string.chars().next().unwrap() as u32) - } else { - None - } -} - // https://w3c.github.io/uievents/#legacy-key-models fn key_keycode(key: Key) -> u32 { match key { @@ -739,7 +743,7 @@ fn key_keycode(key: Key) -> u32 { #[derive(HeapSizeOf)] pub struct KeyEventProperties { - pub key_string: &'static str, + pub key_string: Cow<'static, str>, pub code: &'static str, pub location: u32, pub char_code: Option<u32>, |