|
| 1 | +package keyboard |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "os/exec" |
| 6 | +) |
| 7 | + |
| 8 | +type bytes [3]byte |
| 9 | + |
| 10 | +type KeyEvent struct { |
| 11 | + Bytes bytes |
| 12 | + Key int |
| 13 | + Char string |
| 14 | +} |
| 15 | + |
| 16 | +const ( |
| 17 | + Tilde = iota + 96 |
| 18 | + A |
| 19 | + B |
| 20 | + C |
| 21 | + D |
| 22 | + E |
| 23 | + F |
| 24 | + G |
| 25 | + H |
| 26 | + I |
| 27 | + J |
| 28 | + K |
| 29 | + L |
| 30 | + M |
| 31 | + N |
| 32 | + O |
| 33 | + P |
| 34 | + Q |
| 35 | + R |
| 36 | + S |
| 37 | + T |
| 38 | + U |
| 39 | + V |
| 40 | + W |
| 41 | + X |
| 42 | + Y |
| 43 | + Z |
| 44 | +) |
| 45 | + |
| 46 | +const ( |
| 47 | + Escape = 27 |
| 48 | + Spacebar = 32 |
| 49 | +) |
| 50 | + |
| 51 | +const ( |
| 52 | + Zero = iota + 48 |
| 53 | + One |
| 54 | + Two |
| 55 | + Three |
| 56 | + Four |
| 57 | + Five |
| 58 | + Six |
| 59 | + Seven |
| 60 | + Eight |
| 61 | + Nine |
| 62 | +) |
| 63 | + |
| 64 | +const ( |
| 65 | + ArrowUp = iota + 65 |
| 66 | + ArrowDown |
| 67 | + ArrowRight |
| 68 | + ArrowLeft |
| 69 | +) |
| 70 | + |
| 71 | +// used to hold the original stty state |
| 72 | +var originalState string |
| 73 | + |
| 74 | +func Parse(input bytes) KeyEvent { |
| 75 | + var event = KeyEvent{Bytes: input, Char: string(input[:])} |
| 76 | + |
| 77 | + var code byte |
| 78 | + |
| 79 | + // basic input codes |
| 80 | + if input[1] == 0 && input[2] == 0 { |
| 81 | + code = input[0] |
| 82 | + |
| 83 | + // space bar |
| 84 | + if code == Spacebar { |
| 85 | + event.Key = Spacebar |
| 86 | + } |
| 87 | + |
| 88 | + // vanilla escape |
| 89 | + if code == Escape { |
| 90 | + event.Key = Escape |
| 91 | + } |
| 92 | + |
| 93 | + // number keys |
| 94 | + if code >= 48 && code <= 57 { |
| 95 | + event.Key = int(code) |
| 96 | + } |
| 97 | + |
| 98 | + // alphabet |
| 99 | + if code >= 97 && code <= 122 { |
| 100 | + event.Key = int(code) |
| 101 | + } |
| 102 | + |
| 103 | + return event |
| 104 | + } |
| 105 | + |
| 106 | + // arrow keys |
| 107 | + if input[0] == Escape && input[1] == 91 { |
| 108 | + code = input[2] |
| 109 | + |
| 110 | + if code >= 65 && code <= 68 { |
| 111 | + event.Key = int(code) |
| 112 | + return event |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + return event |
| 117 | +} |
| 118 | + |
| 119 | +// fetches original state, sets up TTY for raw (unbuffered) input |
| 120 | +func configure() (err error) { |
| 121 | + state, err := stty("-g") |
| 122 | + if err != nil { |
| 123 | + return err |
| 124 | + } |
| 125 | + |
| 126 | + originalState = state |
| 127 | + |
| 128 | + // -echo: terminal doesn't echo typed characters back to the terminal |
| 129 | + // -icanon: terminal doesn't interpret special characters (like backspace) |
| 130 | + if _, err := stty("-echo", "-icanon"); err != nil { |
| 131 | + return err |
| 132 | + } |
| 133 | + |
| 134 | + return |
| 135 | +} |
| 136 | + |
| 137 | +// restores the TTY to the original state |
| 138 | +func restore() (errs []error) { |
| 139 | + if _, err := stty(originalState); err != nil { |
| 140 | + return []error{err} |
| 141 | + } |
| 142 | + |
| 143 | + return |
| 144 | +} |
| 145 | + |
| 146 | +func stty(args ...string) (string, error) { |
| 147 | + cmd := exec.Command("stty", args...) |
| 148 | + cmd.Stdin = os.Stdin |
| 149 | + |
| 150 | + out, err := cmd.Output() |
| 151 | + if err != nil { |
| 152 | + return "", err |
| 153 | + } |
| 154 | + |
| 155 | + return string(out), nil |
| 156 | +} |
0 commit comments