From 95c4ce0ddc1cf68ef39317fc4d2649361f2f2e6e Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Sat, 14 Apr 2018 03:20:43 +0900 Subject: [PATCH] input: Add (Key).String() --- genkeys.go | 11 +++ keys.go | 201 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 212 insertions(+) diff --git a/genkeys.go b/genkeys.go index abac2c8f9..8a6cc370f 100644 --- a/genkeys.go +++ b/genkeys.go @@ -183,6 +183,17 @@ const ( {{end}} KeyMax Key = Key{{.LastKeyName}} ) +// String returns a string representing the key. +// +// If k is an undefined key, String returns an empty string. +func (k Key) String() string { + switch k { + {{range $name := .KeyNames}}case Key{{$name}}: + return {{$name | printf "%q"}} + {{end}}} + return "" +} + func keyNameToKey(name string) (Key, bool) { switch strings.ToLower(name) { {{range $name := .KeyNames}}case {{$name | printf "%q" | ToLower}}: diff --git a/keys.go b/keys.go index b7266d264..8deea2658 100644 --- a/keys.go +++ b/keys.go @@ -130,6 +130,207 @@ const ( KeyMax Key = KeyUp ) +// String returns a string representing the key. +// +// If k is an undefined key, String returns an empty string. +func (k Key) String() string { + switch k { + case Key0: + return "0" + case Key1: + return "1" + case Key2: + return "2" + case Key3: + return "3" + case Key4: + return "4" + case Key5: + return "5" + case Key6: + return "6" + case Key7: + return "7" + case Key8: + return "8" + case Key9: + return "9" + case KeyA: + return "A" + case KeyB: + return "B" + case KeyC: + return "C" + case KeyD: + return "D" + case KeyE: + return "E" + case KeyF: + return "F" + case KeyG: + return "G" + case KeyH: + return "H" + case KeyI: + return "I" + case KeyJ: + return "J" + case KeyK: + return "K" + case KeyL: + return "L" + case KeyM: + return "M" + case KeyN: + return "N" + case KeyO: + return "O" + case KeyP: + return "P" + case KeyQ: + return "Q" + case KeyR: + return "R" + case KeyS: + return "S" + case KeyT: + return "T" + case KeyU: + return "U" + case KeyV: + return "V" + case KeyW: + return "W" + case KeyX: + return "X" + case KeyY: + return "Y" + case KeyZ: + return "Z" + case KeyAlt: + return "Alt" + case KeyApostrophe: + return "Apostrophe" + case KeyBackslash: + return "Backslash" + case KeyBackspace: + return "Backspace" + case KeyCapsLock: + return "CapsLock" + case KeyComma: + return "Comma" + case KeyControl: + return "Control" + case KeyDelete: + return "Delete" + case KeyDown: + return "Down" + case KeyEnd: + return "End" + case KeyEnter: + return "Enter" + case KeyEqual: + return "Equal" + case KeyEscape: + return "Escape" + case KeyF1: + return "F1" + case KeyF2: + return "F2" + case KeyF3: + return "F3" + case KeyF4: + return "F4" + case KeyF5: + return "F5" + case KeyF6: + return "F6" + case KeyF7: + return "F7" + case KeyF8: + return "F8" + case KeyF9: + return "F9" + case KeyF10: + return "F10" + case KeyF11: + return "F11" + case KeyF12: + return "F12" + case KeyGraveAccent: + return "GraveAccent" + case KeyHome: + return "Home" + case KeyInsert: + return "Insert" + case KeyKP0: + return "KP0" + case KeyKP1: + return "KP1" + case KeyKP2: + return "KP2" + case KeyKP3: + return "KP3" + case KeyKP4: + return "KP4" + case KeyKP5: + return "KP5" + case KeyKP6: + return "KP6" + case KeyKP7: + return "KP7" + case KeyKP8: + return "KP8" + case KeyKP9: + return "KP9" + case KeyKPAdd: + return "KPAdd" + case KeyKPDecimal: + return "KPDecimal" + case KeyKPDivide: + return "KPDivide" + case KeyKPEnter: + return "KPEnter" + case KeyKPEqual: + return "KPEqual" + case KeyKPMultiply: + return "KPMultiply" + case KeyKPSubtract: + return "KPSubtract" + case KeyLeft: + return "Left" + case KeyLeftBracket: + return "LeftBracket" + case KeyMinus: + return "Minus" + case KeyNumLock: + return "NumLock" + case KeyPageDown: + return "PageDown" + case KeyPageUp: + return "PageUp" + case KeyPeriod: + return "Period" + case KeyRight: + return "Right" + case KeyRightBracket: + return "RightBracket" + case KeySemicolon: + return "Semicolon" + case KeyShift: + return "Shift" + case KeySlash: + return "Slash" + case KeySpace: + return "Space" + case KeyTab: + return "Tab" + case KeyUp: + return "Up" + } + return "" +} + func keyNameToKey(name string) (Key, bool) { switch strings.ToLower(name) { case "0":