Skip to content

Commit

Permalink
Fix key text + require console_log + adjust style
Browse files Browse the repository at this point in the history
This commit addresses comments in the code review:

Key text:
  - web_sys returns the name of each pressed key from which we can
    generate a viable printable string. This mechanism is revised to
    include all non-printable keys as listed in MDN.
  - notably, the tab character and numpad characters have gained
    printable text in this commit.

console_log:
  - console_log is not required in wasm builds.
  - integrated console_log with the use_simple_logger api. `console_log`
    is initialized with log level `trace` as is done in simple logger by
    default.

Style:
  - non std use statements are now before third party use statements.
  - key_to_text function signature refactor
  - specify the log module when calling log::{warn, error} explicitly.
  • Loading branch information
elrnv committed Apr 3, 2020
1 parent 3b54361 commit 3813e3c
Show file tree
Hide file tree
Showing 9 changed files with 379 additions and 37 deletions.
2 changes: 1 addition & 1 deletion druid-shell/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,4 @@ features = ["v3_20"]
wasm-bindgen = "0.2.59"
js-sys = "0.3.36"
web-sys = { version = "0.3.36", features = ["Window", "MouseEvent", "CssStyleDeclaration", "WheelEvent", "KeyEvent", "KeyboardEvent"] }
console_log = { version = "0.1.2", optional = true }
console_log = "0.1.2"
2 changes: 1 addition & 1 deletion druid-shell/examples/shello.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ impl WinHandler for HelloState {
}

fn key_down(&mut self, event: KeyEvent) -> bool {
let deadline = std::time::Instant::now() + std::time::Duration::from_millis(500);
let deadline = instant::Instant::now() + std::time::Duration::from_millis(500);
let id = self.handle.request_timer(deadline);
println!("keydown: {:?}, timer id = {:?}", event, id);
false
Expand Down
350 changes: 350 additions & 0 deletions druid-shell/src/platform/web/keycodes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -248,3 +248,353 @@ map_keys! {
//(KeyEvent::DOM_VK_PA1, (_)) => KeyCode::PA1,
//(KeyEvent::DOM_VK_WIN_OEM_CLEAR, (_)) => KeyCode::WIN_OEM_CLEAR,
}

/// A helper to convert the key string to the text that it is supposed to represent.
pub(crate) fn key_to_text(key: &str) -> &str {
// The following list was taken from
// https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/key/Key_Values
match key {
// Whitespace
"Enter" => "\n",
"Tab" => "\t",

// Numpad Keys
"Decimal" => ".",
"Multiply" => "*",
"Add" => "+",
"Divide" => "/",
"Subtract" => "-",

// Special
"Unidentified"
// Modifiers
| "Alt"
| "AltGraph"
| "CapsLock"
| "Control"
| "Fn"
| "FnLock"
| "Hyper"
| "Meta"
| "NumLock"
| "ScrollLock"
| "Shift"
| "Super"
| "Symbol"
| "SymbolLock"
// Navigation
| "ArrowDown"
| "ArrowLeft"
| "ArrowRight"
| "ArrowUp"
| "End"
| "Home"
| "PageDown"
| "PageUp"
// Editing
| "Backspace"
| "Clear"
| "Copy"
| "CrSel"
| "Cut"
| "Delete"
| "EraseEof"
| "ExSel"
| "Insert"
| "Paste"
| "Redo"
| "Undo"
// UI
| "Accept"
| "Again"
| "Attn"
| "Cancel"
| "ContextMenu"
| "Escape"
| "Execute"
| "Find"
| "Finish"
| "Help"
| "Pause"
| "Play"
| "Props"
| "Select"
| "ZoomIn"
| "ZoomOut"
// Device
| "BrightnessDown"
| "BrightnessUp"
| "Eject"
| "LogOff"
| "Power"
| "PowerOff"
| "PrintScreen"
| "Hibernate"
| "Standby"
| "WakeUp"
// IME
| "AllCandidates"
| "Alphanumeric"
| "CodeInput"
| "Compose"
| "Convert"
| "Dead"
| "FinalMode"
| "GroupFirst"
| "GroupLast"
| "GroupNext"
| "GroupPrevious"
| "ModeChange"
| "NextCandidate"
| "NonConvert"
| "PreviousCandidate"
| "Process"
| "SingleCandidate"
// Korean
| "HangulMode"
| "HanjaMode"
| "JunjaMode"
// Japanese
| "Eisu"
| "Hankaku"
| "Hiragana"
| "HiraganaKatakana"
| "KanaMode"
| "KanjiMode"
| "Katakana"
| "Romaji"
| "Zenkaku"
| "ZenkakuHanaku"
// Function
| "F1"
| "F2"
| "F3"
| "F4"
| "F5"
| "F6"
| "F7"
| "F8"
| "F9"
| "F10"
| "F11"
| "F12"
| "F13"
| "F14"
| "F15"
| "F16"
| "F17"
| "F18"
| "F19"
| "F20"
| "Soft1"
| "Soft2"
| "Soft3"
| "Soft4"
// Phone
| "AppSwitch"
| "Call"
| "Camera"
| "CameraFocus"
| "EndCall"
| "GoBack"
| "GoHome"
| "HeadsetHook"
| "LastNumberRedial"
| "Notification"
| "MannerMode"
| "VoiceDial"
// Multimedia
| "ChannelDown"
| "ChannelUp"
| "MediaFastForward"
| "MediaPause"
| "MediaPlay"
| "MediaPlayPause"
| "MediaRecord"
| "MediaRewind"
| "MediaStop"
| "MediaTrackNext"
| "MediaTrackPrevious"
// Audio
| "AudioBalanceLeft"
| "AudioBalanceRight"
| "AudioBassDown"
| "AudioBassBoostDown"
| "AudioBassBoostToggle"
| "AudioBassBoostUp"
| "AudioBassUp"
| "AudioFaderFront"
| "AudioFaderRear"
| "AudioSurroundModeNext"
| "AudioTrebleDown"
| "AudioTrebleUp"
| "AudioVolumeDown"
| "AudioVolumeMute"
| "AudioVolumeUp"
| "MicrophoneToggle"
| "MicrophoneVolumeDown"
| "MicrophoneVolumeMute"
| "MicrophoneVolumeUp"
// TV control
| "TV"
| "TV3DMode"
| "TVAntennaCable"
| "TVAudioDescription"
| "TVAudioDescriptionMixDown"
| "TVAudioDescriptionMixUp"
| "TVContentsMenu"
| "TVDataService"
| "TVInput"
| "TVInputComponent1"
| "TVInputComponent2"
| "TVInputComposite1"
| "TVInputComposite2"
| "TVInputHDMI1"
| "TVInputHDMI2"
| "TVInputHDMI3"
| "TVInputHDMI4"
| "TVInputVGA1"
| "TVMediaContext"
| "TVNetwork"
| "TVNumberEntry"
| "TVPower"
| "TVRadioService"
| "TVSatellite"
| "TVSatelliteBS"
| "TVSatelliteCS"
| "TVSatelliteToggle"
| "TVTerrestrialAnalog"
| "TVTerrestrialDigital"
| "TVTimer"
// Media Control (Possibly repeated)
| "AVRInput"
| "AVRPower"
| "ColorF0Red"
| "ColorF1Green"
| "ColorF2Yellow"
| "ColorF3Blue"
| "ColorF4Grey"
| "ColorF5Brown"
| "ClosedCaptionToggle"
| "Dimmer"
| "DisplaySwap"
| "DVR"
| "Exit"
| "FavoriteClear0"
| "FavoriteClear1"
| "FavoriteClear2"
| "FavoriteClear3"
| "FavoriteRecall0"
| "FavoriteRecall1"
| "FavoriteRecall2"
| "FavoriteRecall3"
| "FavoriteStore0"
| "FavoriteStore1"
| "FavoriteStore2"
| "FavoriteStore3"
| "Guide"
| "GuideNextDay"
| "GuidePreviousDay"
| "Info"
| "InstantReplay"
| "Link"
| "ListProgram"
| "LiveContent"
| "Lock"
| "MediaApps"
| "MediaAudioTrack"
| "MediaLast"
| "MediaSkipBackward"
| "MediaSkipForward"
| "MediaStepBackward"
| "MediaStepForward"
| "MediaTopMenu"
| "NavigateIn"
| "NavigateNext"
| "NavigateOut"
| "NavigatePrevious"
| "NextFavoriteChannel"
| "NextUserProfile"
| "OnDemand"
| "Pairing"
| "PinPDown"
| "PinPMove"
| "PinPToggle"
| "PinPUp"
| "PlaySpeedDown"
| "PlaySpeedReset"
| "PlaySpeedUp"
| "RandomToggle"
| "RcLowBattery"
| "RecordSpeedNext"
| "RfBypass"
| "ScanChannelsToggle"
| "ScreenModeNext"
| "Settings"
| "SplitScreenToggle"
| "STBInput"
| "STBPower"
| "Subtitle"
| "Teletext"
| "VideoModeNext"
| "Wink"
| "ZoomToggle"
// Speech recognition
| "SpeechCorrectionList"
| "SpeechInputToggle"
// Document
| "Close"
| "New"
| "Open"
| "Print"
| "Save"
| "SpellCheck"
| "MailForward"
| "MailReply"
| "MailSend"
// Application selector
| "LaunchCalculator"
| "LaunchCalendar"
| "LaunchContacts"
| "LaunchMail"
| "LaunchMediaPlayer"
| "LaunchMusicPlayer"
| "LaunchMyComputer"
| "LaunchPhone"
| "LaunchScreenSaver"
| "LaunchSpreadsheet"
| "LaunchWebBrowser"
| "LaunchWebCam"
| "LaunchWordProcessor"
| "LaunchApplication1"
| "LaunchApplication2"
| "LaunchApplication3"
| "LaunchApplication4"
| "LaunchApplication5"
| "LaunchApplication6"
| "LaunchApplication7"
| "LaunchApplication8"
| "LaunchApplication9"
| "LaunchApplication10"
| "LaunchApplication11"
| "LaunchApplication12"
| "LaunchApplication13"
| "LaunchApplication14"
| "LaunchApplication15"
| "LaunchApplication16"
// Browser Control
| "BrowserBack"
| "BrowserFavorites"
| "BrowserForward"
| "BrowserHome"
| "BrowserRefresh"
| "BrowserSearch"
| "BrowserStop"
// Numeric keypad
| "Key11"
| "Key12"
| "Separator" => "",

k => k,
}
}
Loading

0 comments on commit 3813e3c

Please sign in to comment.