Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Heuristically create a Key value in KeyboardEvent::Constructor based …
…on the string provided.
  • Loading branch information
aweinstock314 committed May 6, 2015
1 parent b742eec commit 29f8306
Showing 1 changed file with 180 additions and 1 deletion.
181 changes: 180 additions & 1 deletion components/script/dom/keyboardevent.rs
Expand Up @@ -15,6 +15,7 @@ use dom::uievent::UIEvent;
use dom::window::Window;
use msg::constellation_msg;
use msg::constellation_msg::{Key, KeyModifiers};
use msg::constellation_msg::{SHIFT, CONTROL, ALT, SUPER};
use util::str::DOMString;

use std::borrow::ToOwned;
Expand Down Expand Up @@ -108,11 +109,16 @@ impl KeyboardEvent {
pub fn Constructor(global: GlobalRef,
type_: DOMString,
init: &KeyboardEventBinding::KeyboardEventInit) -> Fallible<Temporary<KeyboardEvent>> {
let key = if let Some((key, _)) = key_from_string(init.key.as_slice()) {
Some(key)
} else {
None
};
let event = KeyboardEvent::new(global.as_window(), type_,
init.parent.parent.parent.bubbles,
init.parent.parent.parent.cancelable,
init.parent.parent.view.r(),
init.parent.parent.detail, None,
init.parent.parent.detail, key,
init.key.clone(), init.code.clone(), init.location,
init.repeat, init.isComposing, init.parent.ctrlKey,
init.parent.altKey, init.parent.shiftKey, init.parent.metaKey,
Expand Down Expand Up @@ -335,6 +341,179 @@ pub fn key_value(key: Key, mods: KeyModifiers) -> &'static str {
}
}

pub fn key_from_string(key_string: &str) -> Option<(Key, KeyModifiers)> {
match key_string {
" " => Some((Key::Space, KeyModifiers::empty())),
"\"" => Some((Key::Apostrophe, SHIFT)),
"'" => Some((Key::Apostrophe, KeyModifiers::empty())),
"<" => Some((Key::Comma, SHIFT)),
"," => Some((Key::Comma, KeyModifiers::empty())),
"_" => Some((Key::Minus, SHIFT)),
"-" => Some((Key::Minus, KeyModifiers::empty())),
">" => Some((Key::Period, SHIFT)),
"." => Some((Key::Period, KeyModifiers::empty())),
"?" => Some((Key::Slash, SHIFT)),
"/" => Some((Key::Slash, KeyModifiers::empty())),
"~" => Some((Key::GraveAccent, SHIFT)),
"`" => Some((Key::GraveAccent, KeyModifiers::empty())),
")" => Some((Key::Num0, SHIFT)),
"0" => Some((Key::Num0, KeyModifiers::empty())),
"!" => Some((Key::Num1, SHIFT)),
"1" => Some((Key::Num1, KeyModifiers::empty())),
"@" => Some((Key::Num2, SHIFT)),
"2" => Some((Key::Num2, KeyModifiers::empty())),
"#" => Some((Key::Num3, SHIFT)),
"3" => Some((Key::Num3, KeyModifiers::empty())),
"$" => Some((Key::Num4, SHIFT)),
"4" => Some((Key::Num4, KeyModifiers::empty())),
"%" => Some((Key::Num5, SHIFT)),
"5" => Some((Key::Num5, KeyModifiers::empty())),
"^" => Some((Key::Num6, SHIFT)),
"6" => Some((Key::Num6, KeyModifiers::empty())),
"&" => Some((Key::Num7, SHIFT)),
"7" => Some((Key::Num7, KeyModifiers::empty())),
"*" => Some((Key::Num8, SHIFT)),
"8" => Some((Key::Num8, KeyModifiers::empty())),
"(" => Some((Key::Num9, SHIFT)),
"9" => Some((Key::Num9, KeyModifiers::empty())),
":" => Some((Key::Semicolon, SHIFT)),
";" => Some((Key::Semicolon, KeyModifiers::empty())),
"+" => Some((Key::Equal, SHIFT)),
"=" => Some((Key::Equal, KeyModifiers::empty())),
"A" => Some((Key::A, SHIFT)),
"a" => Some((Key::A, KeyModifiers::empty())),
"B" => Some((Key::B, SHIFT)),
"b" => Some((Key::B, KeyModifiers::empty())),
"C" => Some((Key::C, SHIFT)),
"c" => Some((Key::C, KeyModifiers::empty())),
"D" => Some((Key::D, SHIFT)),
"d" => Some((Key::D, KeyModifiers::empty())),
"E" => Some((Key::E, SHIFT)),
"e" => Some((Key::E, KeyModifiers::empty())),
"F" => Some((Key::F, SHIFT)),
"f" => Some((Key::F, KeyModifiers::empty())),
"G" => Some((Key::G, SHIFT)),
"g" => Some((Key::G, KeyModifiers::empty())),
"H" => Some((Key::H, SHIFT)),
"h" => Some((Key::H, KeyModifiers::empty())),
"I" => Some((Key::I, SHIFT)),
"i" => Some((Key::I, KeyModifiers::empty())),
"J" => Some((Key::J, SHIFT)),
"j" => Some((Key::J, KeyModifiers::empty())),
"K" => Some((Key::K, SHIFT)),
"k" => Some((Key::K, KeyModifiers::empty())),
"L" => Some((Key::L, SHIFT)),
"l" => Some((Key::L, KeyModifiers::empty())),
"M" => Some((Key::M, SHIFT)),
"m" => Some((Key::M, KeyModifiers::empty())),
"N" => Some((Key::N, SHIFT)),
"n" => Some((Key::N, KeyModifiers::empty())),
"O" => Some((Key::O, SHIFT)),
"o" => Some((Key::O, KeyModifiers::empty())),
"P" => Some((Key::P, SHIFT)),
"p" => Some((Key::P, KeyModifiers::empty())),
"Q" => Some((Key::Q, SHIFT)),
"q" => Some((Key::Q, KeyModifiers::empty())),
"R" => Some((Key::R, SHIFT)),
"r" => Some((Key::R, KeyModifiers::empty())),
"S" => Some((Key::S, SHIFT)),
"s" => Some((Key::S, KeyModifiers::empty())),
"T" => Some((Key::T, SHIFT)),
"t" => Some((Key::T, KeyModifiers::empty())),
"U" => Some((Key::U, SHIFT)),
"u" => Some((Key::U, KeyModifiers::empty())),
"V" => Some((Key::V, SHIFT)),
"v" => Some((Key::V, KeyModifiers::empty())),
"W" => Some((Key::W, SHIFT)),
"w" => Some((Key::W, KeyModifiers::empty())),
"X" => Some((Key::X, SHIFT)),
"x" => Some((Key::X, KeyModifiers::empty())),
"Y" => Some((Key::Y, SHIFT)),
"y" => Some((Key::Y, KeyModifiers::empty())),
"Z" => Some((Key::Z, SHIFT)),
"z" => Some((Key::Z, KeyModifiers::empty())),
"{" => Some((Key::LeftBracket, SHIFT)),
"[" => Some((Key::LeftBracket, KeyModifiers::empty())),
"|" => Some((Key::Backslash, SHIFT)),
"\\" => Some((Key::Backslash, KeyModifiers::empty())),
"}" => Some((Key::RightBracket, SHIFT)),
"]" => Some((Key::RightBracket, KeyModifiers::empty())),
"Unidentified" => Some((Key::World1, KeyModifiers::empty())),
/*"Unidentified" => Some((Key::World2, KeyModifiers::empty())),*/
"Escape" => Some((Key::Escape, KeyModifiers::empty())),
"Enter" => Some((Key::Enter, KeyModifiers::empty())),
"Tab" => Some((Key::Tab, KeyModifiers::empty())),
"Backspace" => Some((Key::Backspace, KeyModifiers::empty())),
"Insert" => Some((Key::Insert, KeyModifiers::empty())),
"Delete" => Some((Key::Delete, KeyModifiers::empty())),
"ArrowRight" => Some((Key::Right, KeyModifiers::empty())),
"ArrowLeft" => Some((Key::Left, KeyModifiers::empty())),
"ArrowDown" => Some((Key::Down, KeyModifiers::empty())),
"ArrowUp" => Some((Key::Up, KeyModifiers::empty())),
"PageUp" => Some((Key::PageUp, KeyModifiers::empty())),
"PageDown" => Some((Key::PageDown, KeyModifiers::empty())),
"Home" => Some((Key::Home, KeyModifiers::empty())),
"End" => Some((Key::End, KeyModifiers::empty())),
"CapsLock" => Some((Key::CapsLock, KeyModifiers::empty())),
"ScrollLock" => Some((Key::ScrollLock, KeyModifiers::empty())),
"NumLock" => Some((Key::NumLock, KeyModifiers::empty())),
"PrintScreen" => Some((Key::PrintScreen, KeyModifiers::empty())),
"Pause" => Some((Key::Pause, KeyModifiers::empty())),
"F1" => Some((Key::F1, KeyModifiers::empty())),
"F2" => Some((Key::F2, KeyModifiers::empty())),
"F3" => Some((Key::F3, KeyModifiers::empty())),
"F4" => Some((Key::F4, KeyModifiers::empty())),
"F5" => Some((Key::F5, KeyModifiers::empty())),
"F6" => Some((Key::F6, KeyModifiers::empty())),
"F7" => Some((Key::F7, KeyModifiers::empty())),
"F8" => Some((Key::F8, KeyModifiers::empty())),
"F9" => Some((Key::F9, KeyModifiers::empty())),
"F10" => Some((Key::F10, KeyModifiers::empty())),
"F11" => Some((Key::F11, KeyModifiers::empty())),
"F12" => Some((Key::F12, KeyModifiers::empty())),
"F13" => Some((Key::F13, KeyModifiers::empty())),
"F14" => Some((Key::F14, KeyModifiers::empty())),
"F15" => Some((Key::F15, KeyModifiers::empty())),
"F16" => Some((Key::F16, KeyModifiers::empty())),
"F17" => Some((Key::F17, KeyModifiers::empty())),
"F18" => Some((Key::F18, KeyModifiers::empty())),
"F19" => Some((Key::F19, KeyModifiers::empty())),
"F20" => Some((Key::F20, KeyModifiers::empty())),
"F21" => Some((Key::F21, KeyModifiers::empty())),
"F22" => Some((Key::F22, KeyModifiers::empty())),
"F23" => Some((Key::F23, KeyModifiers::empty())),
"F24" => Some((Key::F24, KeyModifiers::empty())),
"F25" => Some((Key::F25, KeyModifiers::empty())),
/*"0" => Some((Key::Kp0, KeyModifiers::empty())),
"1" => Some((Key::Kp1, KeyModifiers::empty())),
"2" => Some((Key::Kp2, KeyModifiers::empty())),
"3" => Some((Key::Kp3, KeyModifiers::empty())),
"4" => Some((Key::Kp4, KeyModifiers::empty())),
"5" => Some((Key::Kp5, KeyModifiers::empty())),
"6" => Some((Key::Kp6, KeyModifiers::empty())),
"7" => Some((Key::Kp7, KeyModifiers::empty())),
"8" => Some((Key::Kp8, KeyModifiers::empty())),
"9" => Some((Key::Kp9, KeyModifiers::empty())),
"." => Some((Key::KpDecimal, KeyModifiers::empty())),
"/" => Some((Key::KpDivide, KeyModifiers::empty())),
"*" => Some((Key::KpMultiply, KeyModifiers::empty())),
"-" => Some((Key::KpSubtract, KeyModifiers::empty())),
"+" => Some((Key::KpAdd, KeyModifiers::empty())),
"Enter" => Some((Key::KpEnter, KeyModifiers::empty())),
"=" => Some((Key::KpEqual, KeyModifiers::empty())),*/
"Shift" => Some((Key::LeftShift, SHIFT)),
"Control" => Some((Key::LeftControl, CONTROL)),
"Alt" => Some((Key::LeftAlt, ALT)),
"Super" => Some((Key::LeftSuper, SUPER)),
/*"Shift" => Some((Key::RightShift, SHIFT)),
"Control" => Some((Key::RightControl, CONTROL)),
"Alt" => Some((Key::RightAlt, ALT)),
"Super" => Some((Key::RightSuper, SUPER)),*/
"ContextMenu" => Some((Key::Menu, KeyModifiers::empty())),
_ => None
}
}

// https://dvcs.w3.org/hg/dom3events/raw-file/tip/html/DOM3Events-code.html
fn code_value(key: Key) -> &'static str {
match key {
Expand Down

0 comments on commit 29f8306

Please sign in to comment.