-
Notifications
You must be signed in to change notification settings - Fork 0
/
key.go
114 lines (98 loc) · 1.42 KB
/
key.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
package main
import (
"sync"
"github.com/veandco/go-sdl2/sdl"
)
type Key struct {
mu sync.Mutex
KeyboardEvent sdl.Keycode
MappedKey byte
IsPressed bool
}
func (k *Key) SetKeyboardEvent(e sdl.Keycode) {
k.mu.Lock()
defer k.mu.Unlock()
k.KeyboardEvent = e
}
func (k *Key) SetIsPressed(b bool) {
k.mu.Lock()
defer k.mu.Unlock()
k.IsPressed = b
}
func (k *Key) GetIsPressed() bool {
k.mu.Lock()
defer k.mu.Unlock()
return k.IsPressed
}
func (k *Key) SetMappedKey() {
k.mu.Lock()
defer k.mu.Unlock()
k.MappedKey = MapKey(byte(k.KeyboardEvent))
}
func (k *Key) GetMappedKey() byte {
k.mu.Lock()
defer k.mu.Unlock()
return k.MappedKey
}
func (k *Key) ClearMappedKey() {
k.mu.Lock()
defer k.mu.Unlock()
k.MappedKey = 0xFF
}
/*
Key Mapping:
1 -> 1
2 -> 2
3 -> 3
4 -> C
q -> 4
w -> 5
e -> 6
r -> D
a -> 7
s -> 8
d -> 9
f -> E
z -> A
x -> 0
c -> B
v -> F
*/
func MapKey(c byte) byte {
switch c {
case byte('1'):
return 0x1
case byte('2'):
return 0x2
case byte('3'):
return 0x3
case byte('4'):
return 0xC
case byte('q'):
return 0x4
case byte('w'):
return 0x5
case byte('e'):
return 0x6
case byte('r'):
return 0xD
case byte('a'):
return 0x7
case byte('s'):
return 0x8
case byte('d'):
return 0x9
case byte('f'):
return 0xE
case byte('z'):
return 0xA
case byte('x'):
return 0x0
case byte('c'):
return 0xB
case byte('v'):
return 0xF
default:
return c
}
}