-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathinput.go
169 lines (152 loc) · 3.45 KB
/
input.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package main // unit: Input
import (
"github.com/gdamore/tcell"
)
const (
KEY_BACKSPACE = '\x08'
KEY_TAB = '\t'
KEY_ENTER = '\r'
KEY_CTRL_Y = '\x19'
KEY_ESCAPE = '\x1b'
KEY_F1 = '\xbb'
KEY_F2 = '\xbc'
KEY_F3 = '\xbd'
KEY_F4 = '\xbe'
KEY_UP = '\xc8'
KEY_PAGE_UP = '\xc9'
KEY_LEFT = '\xcb'
KEY_RIGHT = '\xcd'
KEY_DOWN = '\xd0'
KEY_PAGE_DOWN = '\xd1'
KEY_INSERT = '\xd2'
KEY_DELETE = '\xd3'
KEY_HOME = '\xd4'
KEY_END = '\xd5'
)
var (
InputDeltaX, InputDeltaY int16
InputShiftPressed bool
InputKeyPressed byte
KeysShiftHeld bool
keyChan chan byte
)
// implementation uses: Dos, Crt, Keys, Sounds
var (
InputLastDeltaX, InputLastDeltaY int16
InputKeyBuffer string
)
func InputUpdate() {
InputUpdateWithKey(0)
}
func InputUpdateWithKey(keyRead byte) {
InputDeltaX = 0
InputDeltaY = 0
InputShiftPressed = false
if keyRead == 0 {
checkForKeys := true
for checkForKeys {
select {
case key := <-keyChan:
InputKeyPressed = key
InputKeyBuffer += string([]byte{InputKeyPressed})
default:
checkForKeys = false
}
}
} else {
InputKeyPressed = keyRead
InputKeyBuffer += string([]byte{InputKeyPressed})
}
if Length(InputKeyBuffer) != 0 {
InputKeyPressed = InputKeyBuffer[0]
if Length(InputKeyBuffer) == 1 {
InputKeyBuffer = ""
} else {
InputKeyBuffer = Copy(InputKeyBuffer, Length(InputKeyBuffer)-1, 1)
}
switch InputKeyPressed {
case KEY_UP, '8':
InputDeltaX = 0
InputDeltaY = -1
case KEY_LEFT, '4':
InputDeltaX = -1
InputDeltaY = 0
case KEY_RIGHT, '6':
InputDeltaX = 1
InputDeltaY = 0
case KEY_DOWN, '2':
InputDeltaX = 0
InputDeltaY = 1
}
} else {
InputKeyPressed = '\x00'
}
if InputDeltaX != 0 || InputDeltaY != 0 {
InputShiftPressed = KeysShiftHeld
}
if InputDeltaX != 0 || InputDeltaY != 0 {
InputLastDeltaX = InputDeltaX
InputLastDeltaY = InputDeltaY
}
}
func InputReadWaitKey() {
key := <-keyChan
InputUpdateWithKey(key)
}
func InputStartPoller(screen tcell.Screen) {
keyChan = make(chan byte)
go InputKeyPoller(screen, keyChan)
}
func InputKeyPoller(screen tcell.Screen, keyChan chan byte) {
for {
event := screen.PollEvent()
switch event := event.(type) {
case *tcell.EventKey:
// TODO: this doesn't work for shift-up and shift-down in tcell :-(
KeysShiftHeld = event.Modifiers()&tcell.ModShift != 0
switch event.Key() {
case tcell.KeyRune:
r := event.Rune()
if r >= 32 && r <= 126 {
keyChan <- byte(r)
}
default:
key := tcellToKey[event.Key()]
if key != 0 {
keyChan <- key
}
}
case *tcell.EventResize:
screen.Sync()
}
}
}
var tcellToKey = map[tcell.Key]byte{
tcell.KeyBackspace: KEY_BACKSPACE,
tcell.KeyCtrlY: KEY_CTRL_Y,
tcell.KeyDelete: KEY_DELETE,
tcell.KeyDown: KEY_DOWN,
tcell.KeyEnd: KEY_END,
tcell.KeyEnter: KEY_ENTER,
tcell.KeyEscape: KEY_ESCAPE,
tcell.KeyF1: KEY_F1,
tcell.KeyF2: KEY_F2,
tcell.KeyF3: KEY_F3,
tcell.KeyF4: KEY_F4,
tcell.KeyHome: KEY_HOME,
tcell.KeyInsert: KEY_INSERT,
tcell.KeyLeft: KEY_LEFT,
tcell.KeyPgDn: KEY_PAGE_DOWN,
tcell.KeyPgUp: KEY_PAGE_UP,
tcell.KeyRight: KEY_RIGHT,
tcell.KeyTab: KEY_TAB,
tcell.KeyUp: KEY_UP,
}
func init() {
InputLastDeltaX = 0
InputLastDeltaY = 0
InputDeltaX = 0
InputDeltaY = 0
InputShiftPressed = false
InputKeyBuffer = ""
}