-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkeyboard.go
80 lines (64 loc) · 1.45 KB
/
keyboard.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
package main
import (
"fmt"
rl "github.com/gen2brain/raylib-go/raylib"
)
type Keyboard struct {
KeyMaps map[int32]int
}
type KeyAction interface {
keyPress(uint8) int
GetKeyPressed() int
}
// Keep map convert modern keys to chip 8 keys
var keymap = map[int32]int{
49: 0x1, // 1
50: 0x2, // 2
51: 0x3, // 3
52: 0xc, // 4
113: 0x4, // Q
119: 0x5, // W
101: 0x6, // E
114: 0xD, // R
97: 0x7, // A
115: 0x8, // S
100: 0x9, // D
102: 0xE, // F
122: 0xA, // Z
120: 0x0, // X
99: 0xB, // C
118: 0xF, // V
}
func (k *Keyboard) Init() {
k.KeyMaps = keymap
}
// Part of keyboard struct, pass in key value we want to check for
// and return 1 or 0 if we did get that correct key value
func (k *Keyboard) KeyPress(keyValue uint8) int {
key := rl.GetCharPressed()
fmt.Println("KeyPress", keyValue, key)
chip8Key := k.KeyMaps[key]
if uint8(chip8Key) == keyValue {
return 1
}
// if rl.IsKeyDown(rl.KeyKp1) {
// fmt.Printf("pressed")
// return 1
// }
// return 0
return 0
}
// Checks to see if any key is pressed and returns that key value that was pressed
// Key value is base on keymap array
// Returns int and bool, keyvalue and wether key was pressed
func (k *Keyboard) GetKeyPressed() (int, bool) {
key := rl.GetCharPressed()
chip8Key := k.KeyMaps[key]
isKeyPressed := false
if key != 0 {
isKeyPressed = true
fmt.Println(key)
// fmt.Println(k.KeyMaps[key])
}
return chip8Key, isKeyPressed //return zero if not in map
}