use std::fmt;
pub use code::Code;
pub use key::Key;
pub use location::Location;
pub use modifiers::Modifiers;
pub use shortcuts::ShortcutMatcher;
#[macro_use]
extern crate bitflags;
#[cfg(feature = "serde")]
#[macro_use]
extern crate serde;
#[cfg(feature="webdriver")]
extern crate unicode_segmentation;
mod code;
mod key;
mod location;
mod modifiers;
mod shortcuts;
#[cfg(feature="webdriver")]
pub mod webdriver;
#[derive(Copy, Clone, Debug, Eq, Hash, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum KeyState {
Down,
Up,
}
#[derive(Clone, Debug, Default, Eq, Hash, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct KeyboardEvent {
pub state: KeyState,
pub key: Key,
pub code: Code,
pub location: Location,
pub modifiers: Modifiers,
pub repeat: bool,
pub is_composing: bool,
}
#[derive(Copy, Clone, Debug, Eq, Hash, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum CompositionState {
Start,
Update,
End,
}
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct CompositionEvent {
pub state: CompositionState,
pub data: String,
}
impl fmt::Display for KeyState {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
KeyState::Down => f.write_str("keydown"),
KeyState::Up => f.write_str("keyup"),
}
}
}
impl Key {
pub fn legacy_charcode(&self) -> u32 {
match self {
Key::Character(ref c) => c.chars().next().unwrap_or('\0') as u32,
_ => 0,
}
}
pub fn legacy_keycode(&self) -> u32 {
match self {
Key::Backspace => 8,
Key::Tab => 9,
Key::Enter => 13,
Key::Shift => 16,
Key::Control => 17,
Key::Alt => 18,
Key::CapsLock => 20,
Key::Escape => 27,
Key::PageUp => 33,
Key::PageDown => 34,
Key::End => 35,
Key::Home => 36,
Key::ArrowLeft => 37,
Key::ArrowUp => 38,
Key::ArrowRight => 39,
Key::ArrowDown => 40,
Key::Delete => 46,
Key::Character(ref c) if c.len() == 1 => match first_char(c) {
' ' => 32,
x @ '0'...'9' => x as u32,
x @ 'a'...'z' => x.to_ascii_uppercase() as u32,
x @ 'A'...'Z' => x as u32,
';' | ':' => 186,
'=' | '+' => 187,
',' | '<' => 188,
'-' | '_' => 189,
'.' | '>' => 190,
'/' | '?' => 191,
'`' | '~' => 192,
'[' | '{' => 219,
'\\' | '|' => 220,
']' | '}' => 221,
'\'' | '\"' => 222,
_ => 0,
},
_ => 0,
}
}
}
impl Default for KeyState {
fn default() -> KeyState {
KeyState::Down
}
}
impl Default for Key {
fn default() -> Key {
Key::Unidentified
}
}
impl Default for Code {
fn default() -> Code {
Code::Unidentified
}
}
impl Default for Location {
fn default() -> Location {
Location::Standard
}
}
fn first_char(s: &str) -> char {
s.chars().next().expect("empty string")
}