use postcard::experimental::max_size::MaxSize;
use serde::{Deserialize, Serialize};
use strum::FromRepr;
use super::consumer::ConsumerKey;
use super::system_control::SystemControlKey;
use crate::modifier::ModifierCombination;
#[repr(u8)]
#[derive(Debug, Copy, Clone, PartialEq, Eq, Serialize, Deserialize, PartialOrd, Ord, FromRepr, MaxSize)]
#[cfg_attr(feature = "_codegen", derive(strum::EnumIter, strum::VariantNames))]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[cfg_attr(feature = "wasm", derive(tsify::Tsify))]
#[cfg_attr(feature = "wasm", tsify(into_wasm_abi, from_wasm_abi))]
pub enum HidKeyCode {
No = 0x0000,
ErrorRollover = 0x0001,
PostFail = 0x0002,
ErrorUndefined = 0x0003,
A = 0x0004,
B = 0x0005,
C = 0x0006,
D = 0x0007,
E = 0x0008,
F = 0x0009,
G = 0x000A,
H = 0x000B,
I = 0x000C,
J = 0x000D,
K = 0x000E,
L = 0x000F,
M = 0x0010,
N = 0x0011,
O = 0x0012,
P = 0x0013,
Q = 0x0014,
R = 0x0015,
S = 0x0016,
T = 0x0017,
U = 0x0018,
V = 0x0019,
W = 0x001A,
X = 0x001B,
Y = 0x001C,
Z = 0x001D,
Kc1 = 0x001E,
Kc2 = 0x001F,
Kc3 = 0x0020,
Kc4 = 0x0021,
Kc5 = 0x0022,
Kc6 = 0x0023,
Kc7 = 0x0024,
Kc8 = 0x0025,
Kc9 = 0x0026,
Kc0 = 0x0027,
Enter = 0x0028,
Escape = 0x0029,
Backspace = 0x002A,
Tab = 0x002B,
Space = 0x002C,
Minus = 0x002D,
Equal = 0x002E,
LeftBracket = 0x002F,
RightBracket = 0x0030,
Backslash = 0x0031,
NonusHash = 0x0032,
Semicolon = 0x0033,
Quote = 0x0034,
Grave = 0x0035,
Comma = 0x0036,
Dot = 0x0037,
Slash = 0x0038,
CapsLock = 0x0039,
F1 = 0x003A,
F2 = 0x003B,
F3 = 0x003C,
F4 = 0x003D,
F5 = 0x003E,
F6 = 0x003F,
F7 = 0x0040,
F8 = 0x0041,
F9 = 0x0042,
F10 = 0x0043,
F11 = 0x0044,
F12 = 0x0045,
PrintScreen = 0x0046,
ScrollLock = 0x0047,
Pause = 0x0048,
Insert = 0x0049,
Home = 0x004A,
PageUp = 0x004B,
Delete = 0x004C,
End = 0x004D,
PageDown = 0x004E,
Right = 0x004F,
Left = 0x0050,
Down = 0x0051,
Up = 0x0052,
NumLock = 0x0053,
KpSlash = 0x0054,
KpAsterisk = 0x0055,
KpMinus = 0x0056,
KpPlus = 0x0057,
KpEnter = 0x0058,
Kp1 = 0x0059,
Kp2 = 0x005A,
Kp3 = 0x005B,
Kp4 = 0x005C,
Kp5 = 0x005D,
Kp6 = 0x005E,
Kp7 = 0x005F,
Kp8 = 0x0060,
Kp9 = 0x0061,
Kp0 = 0x0062,
KpDot = 0x0063,
NonusBackslash = 0x0064,
Application = 0x0065,
KbPower = 0x0066,
KpEqual = 0x0067,
F13 = 0x0068,
F14 = 0x0069,
F15 = 0x006A,
F16 = 0x006B,
F17 = 0x006C,
F18 = 0x006D,
F19 = 0x006E,
F20 = 0x006F,
F21 = 0x0070,
F22 = 0x0071,
F23 = 0x0072,
F24 = 0x0073,
Execute = 0x0074,
Help = 0x0075,
Menu = 0x0076,
Select = 0x0077,
Stop = 0x0078,
Again = 0x0079,
Undo = 0x007A,
Cut = 0x007B,
Copy = 0x007C,
Paste = 0x007D,
Find = 0x007E,
KbMute = 0x007F,
KbVolumeUp = 0x0080,
KbVolumeDown = 0x0081,
LockingCapsLock = 0x0082,
LockingNumLock = 0x0083,
LockingScrollLock = 0x0084,
KpComma = 0x0085,
KpEqualAs400 = 0x0086,
International1 = 0x0087,
International2 = 0x0088,
International3 = 0x0089,
International4 = 0x008A,
International5 = 0x008B,
International6 = 0x008C,
International7 = 0x008D,
International8 = 0x008E,
International9 = 0x008F,
Language1 = 0x0090,
Language2 = 0x0091,
Language3 = 0x0092,
Language4 = 0x0093,
Language5 = 0x0094,
Language6 = 0x0095,
Language7 = 0x0096,
Language8 = 0x0097,
Language9 = 0x0098,
AlternateErase = 0x0099,
SystemRequest = 0x009A,
Cancel = 0x009B,
Clear = 0x009C,
Prior = 0x009D,
Return = 0x009E,
Separator = 0x009F,
Out = 0x00A0,
Oper = 0x00A1,
ClearAgain = 0x00A2,
Crsel = 0x00A3,
Exsel = 0x00A4,
SystemPower = 0x00A5,
SystemSleep = 0x00A6,
SystemWake = 0x00A7,
AudioMute = 0x00A8,
AudioVolUp = 0x00A9,
AudioVolDown = 0x00AA,
MediaNextTrack = 0x00AB,
MediaPrevTrack = 0x00AC,
MediaStop = 0x00AD,
MediaPlayPause = 0x00AE,
MediaSelect = 0x00AF,
MediaEject = 0x00B0,
Mail = 0x00B1,
Calculator = 0x00B2,
MyComputer = 0x00B3,
WwwSearch = 0x00B4,
WwwHome = 0x00B5,
WwwBack = 0x00B6,
WwwForward = 0x00B7,
WwwStop = 0x00B8,
WwwRefresh = 0x00B9,
WwwFavorites = 0x00BA,
MediaFastForward = 0x00BB,
MediaRewind = 0x00BC,
BrightnessUp = 0x00BD,
BrightnessDown = 0x00BE,
ControlPanel = 0x00BF,
Assistant = 0x00C0,
MissionControl = 0x00C1,
Launchpad = 0x00C2,
MouseUp = 0x00CD,
MouseDown = 0x00CE,
MouseLeft = 0x00CF,
MouseRight = 0x00D0,
MouseBtn1 = 0x00D1,
MouseBtn2 = 0x00D2,
MouseBtn3 = 0x00D3,
MouseBtn4 = 0x00D4,
MouseBtn5 = 0x00D5,
MouseBtn6 = 0x00D6,
MouseBtn7 = 0x00D7,
MouseBtn8 = 0x00D8,
MouseWheelUp = 0x00D9,
MouseWheelDown = 0x00DA,
MouseWheelLeft = 0x00DB,
MouseWheelRight = 0x00DC,
MouseAccel0 = 0x00DD,
MouseAccel1 = 0x00DE,
MouseAccel2 = 0x00DF,
LCtrl = 0x00E0,
LShift = 0x00E1,
LAlt = 0x00E2,
LGui = 0x00E3,
RCtrl = 0x00E4,
RShift = 0x00E5,
RAlt = 0x00E6,
RGui = 0x00E7,
}
impl HidKeyCode {
#[cfg(feature = "_codegen")]
pub fn all() -> impl Iterator<Item = Self> {
<Self as strum::IntoEnumIterator>::iter()
}
pub fn is_simple_key(self) -> bool {
HidKeyCode::No <= self && self <= HidKeyCode::MouseAccel2
}
pub fn is_modifier(self) -> bool {
HidKeyCode::LCtrl <= self && self <= HidKeyCode::RGui
}
pub fn is_mouse_key(self) -> bool {
HidKeyCode::MouseUp <= self && self <= HidKeyCode::MouseAccel2
}
pub fn to_hid_modifiers(self) -> ModifierCombination {
match self {
HidKeyCode::LCtrl => ModifierCombination::LCTRL,
HidKeyCode::LShift => ModifierCombination::LSHIFT,
HidKeyCode::LAlt => ModifierCombination::LALT,
HidKeyCode::LGui => ModifierCombination::LGUI,
HidKeyCode::RCtrl => ModifierCombination::RCTRL,
HidKeyCode::RShift => ModifierCombination::RSHIFT,
HidKeyCode::RAlt => ModifierCombination::RALT,
HidKeyCode::RGui => ModifierCombination::RGUI,
_ => ModifierCombination::new(),
}
}
pub fn is_caps_word_continue_key(self) -> bool {
if self >= HidKeyCode::A && self <= HidKeyCode::Z {
return true;
}
if self >= HidKeyCode::Kc1 && self <= HidKeyCode::Kc0 {
return true;
}
if self == HidKeyCode::Minus || self == HidKeyCode::Backspace || self == HidKeyCode::Delete {
return true;
}
false
}
pub fn is_caps_word_shifted_key(self) -> bool {
if self >= HidKeyCode::A && self <= HidKeyCode::Z {
return true;
}
if self == HidKeyCode::Minus {
return true;
}
false
}
pub fn process_as_consumer(&self) -> Option<ConsumerKey> {
match self {
HidKeyCode::AudioMute => Some(ConsumerKey::Mute),
HidKeyCode::AudioVolUp => Some(ConsumerKey::VolumeIncrement),
HidKeyCode::AudioVolDown => Some(ConsumerKey::VolumeDecrement),
HidKeyCode::MediaNextTrack => Some(ConsumerKey::NextTrack),
HidKeyCode::MediaPrevTrack => Some(ConsumerKey::PrevTrack),
HidKeyCode::MediaStop => Some(ConsumerKey::StopPlay),
HidKeyCode::MediaPlayPause => Some(ConsumerKey::PlayPause),
HidKeyCode::MediaSelect => Some(ConsumerKey::Record),
HidKeyCode::MediaEject => Some(ConsumerKey::Eject),
HidKeyCode::Mail => Some(ConsumerKey::Email),
HidKeyCode::Calculator => Some(ConsumerKey::Calculator),
HidKeyCode::MyComputer => Some(ConsumerKey::LocalBrowser),
HidKeyCode::WwwSearch => Some(ConsumerKey::Search),
HidKeyCode::WwwHome => Some(ConsumerKey::Home),
HidKeyCode::WwwBack => Some(ConsumerKey::Back),
HidKeyCode::WwwForward => Some(ConsumerKey::Forward),
HidKeyCode::WwwStop => Some(ConsumerKey::Stop),
HidKeyCode::WwwRefresh => Some(ConsumerKey::Refresh),
HidKeyCode::WwwFavorites => Some(ConsumerKey::Bookmarks),
HidKeyCode::MediaFastForward => Some(ConsumerKey::FastForward),
HidKeyCode::MediaRewind => Some(ConsumerKey::Rewind),
HidKeyCode::BrightnessUp => Some(ConsumerKey::BrightnessUp),
HidKeyCode::BrightnessDown => Some(ConsumerKey::BrightnessDown),
HidKeyCode::ControlPanel => Some(ConsumerKey::ControlPanel),
HidKeyCode::Assistant => Some(ConsumerKey::Assistant),
HidKeyCode::MissionControl => Some(ConsumerKey::DesktopShowAllWindows),
HidKeyCode::Launchpad => Some(ConsumerKey::AcSoftKeyLeft),
_ => None,
}
}
pub fn process_as_system_control(&self) -> Option<SystemControlKey> {
match self {
HidKeyCode::SystemPower => Some(SystemControlKey::PowerDown),
HidKeyCode::SystemSleep => Some(SystemControlKey::Sleep),
HidKeyCode::SystemWake => Some(SystemControlKey::WakeUp),
_ => None,
}
}
}
impl From<u8> for HidKeyCode {
fn from(value: u8) -> Self {
Self::from_repr(value).unwrap_or(HidKeyCode::No)
}
}