123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312 |
- package getch
- import (
- "errors"
- "fmt"
- )
- var ErrNotImplemented = errors.New("Not implemented")
- type ErrUnknownEscapeSequence struct {
- Detail []byte
- }
- func (e *ErrUnknownEscapeSequence) Error() string {
- return fmt.Sprintf("Unknown escape sequence: %v", e.Detail)
- }
- type KeyCode string
- const (
-
- KeyF1 = "Key_F1"
- KeyF2 = "Key_F2"
- KeyF3 = "Key_F3"
- KeyF4 = "Key_F4"
- KeyF5 = "Key_F5"
- KeyF6 = "Key_F6"
- KeyF7 = "Key_F7"
- KeyF8 = "Key_F8"
- KeyF9 = "Key_F9"
- KeyF10 = "Key_F10"
- KeyF11 = "Key_F11"
- KeyF12 = "Key_F12"
-
- KeyEnter = "Key_ENTER"
- KeyBackspace = "Key_BACKSPACE"
- KeyEsc = "Key_ESC"
- KeyTab = "Key_TAB"
- KeyInsert = "Key_INSERT"
- KeyDelete = "Key_DELETE"
- KeyHome = "Key_HOME"
- KeyEnd = "Key_END"
- KeyPgup = "Key_PAGE_UP"
- KeyPgdn = "Key_PAGE_DOWN"
- KeyArrowUp = "Key_ARROW_UP"
- KeyArrowDown = "Key_ARROW_DOWN"
- KeyArrowLeft = "Key_ARROW_LEFT"
- KeyArrowRight = "Key_ARROW_RIGHT"
- KeyCommand = "Key_CMD"
-
- KeyA = "Key_A"
- KeyB = "Key_B"
- KeyC = "Key_C"
- KeyD = "Key_D"
- KeyE = "Key_E"
- KeyF = "Key_F"
- KeyG = "Key_G"
- KeyH = "Key_H"
- KeyI = "Key_I"
- KeyJ = "Key_J"
- KeyK = "Key_K"
- KeyL = "Key_L"
- KeyM = "Key_M"
- KeyN = "Key_N"
- KeyO = "Key_O"
- KeyP = "Key_P"
- KeyQ = "Key_Q"
- KeyR = "Key_R"
- KeyS = "Key_S"
- KeyT = "Key_T"
- KeyU = "Key_U"
- KeyV = "Key_V"
- KeyW = "Key_W"
- KeyX = "Key_X"
- KeyY = "Key_Y"
- KeyZ = "Key_Z"
-
- Key1 = "Key_1"
- Key2 = "Key_2"
- Key3 = "Key_3"
- Key4 = "Key_4"
- Key5 = "Key_5"
- Key6 = "Key_6"
- Key7 = "Key_7"
- Key8 = "Key_8"
- Key9 = "Key_9"
- Key0 = "Key_0"
-
- KeyBacktick = "Key_BACKTICK"
- KeyMinus = "Key_MINUS"
- KeyEqual = "Key_EQUAL"
- KeyBracketOpen = "Key_BRACKET_OPEN"
- KeyBracketClose = "Key_BRACKET_CLOSE"
- KeySemiColon = "Key_SEMICOLON"
- KeyQuote = "Key_QUOTE"
- KeyHash = "Key_HASH"
- KeyBackslash = "Key_BACKSLASH"
- KeyComma = "Key_COMMA"
- KeyDot = "Key_DOT"
- KeySlash = "Key_SLASH"
-
- KeyUnknown = "Key_UNKNOWN"
- )
- type KeyEvent struct {
- Code KeyCode
- Ctrl bool
- Alt bool
- Shift bool
- Rune rune
- RawBuf []byte
- }
- func (k *KeyEvent) String() string {
- ret := fmt.Sprintf("%v %c [%#v - 0x%x]", k.Code, k.Rune, k.Rune, k.Rune)
- if k.Shift {
- ret += " + SHIFT"
- }
- if k.Ctrl {
- ret += " + CTRL"
- }
- if k.Alt {
- ret += " + ALT"
- }
- return ret
- }
- type internalKeyEvent struct {
- *KeyEvent
- err error
- }
- type getch interface {
-
- GetKey() *internalKeyEvent
-
- GetKeyAsync() *internalKeyEvent
-
- CursorPosition() (int, int, error)
-
- SetCursorPosition(int, int) error
-
- Close()
- }
- var g getch
- func Start() error {
- var err error
- if g == nil {
- g, err = attachReader()
- }
- return err
- }
- func CursorPosition() (int, int, error) {
- var x, y int
- var err error
- if g != nil {
- x, y, err = g.CursorPosition()
- }
- return x, y, err
- }
- func SetCursorPosition(x, y int) error {
- var err error
- if g != nil {
- err = g.SetCursorPosition(x, y)
- }
- return err
- }
- func Getch() (*KeyEvent, error) {
- var ret *KeyEvent
- var err error
- if g != nil {
- ke := g.GetKey()
- if err = ke.err; err == nil {
- ret = ke.KeyEvent
- }
- }
- return ret, err
- }
- func Stop() {
- if g != nil {
- g.Close()
- g = nil
- }
- }
|