-
Notifications
You must be signed in to change notification settings - Fork 10
/
keyboard.go
70 lines (59 loc) · 1.71 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
package izapple2
import (
"slices"
"unicode"
)
// KeyboardProvider provides a keyboard implementation
type KeyboardProvider interface {
GetKey(strobe bool) (key uint8, ok bool)
}
// KeyboardChannel is a possible implemetation of a Keyboard provider
type KeyboardChannel struct {
keyChannel chan uint8
a *Apple2
}
// NewKeyboardChannel returns an instance of KeyboardChannel
func NewKeyboardChannel(a *Apple2) *KeyboardChannel {
var k KeyboardChannel
k.keyChannel = make(chan uint8, 100)
k.a = a
a.SetKeyboardProvider(&k)
return &k
}
// PutText sends texts to the emulator as succesive chars
func (k *KeyboardChannel) PutText(text string) {
for _, ch := range text {
k.PutRune(ch)
}
}
var macOptionChars = []rune("ı•£‰⁄‘’≈œæ€®†¥øπå∫∂ƒ™¶§∑©√ßµ„…≤≥çñŒÆ€‡∏fl¯ˇ˘‹›◊˙˚˝")
var macOptionSubst = []rune("!·$%/()=qwertyopasdfghjkxcvbm,.<>cnQWETPGJKLZXVNM\"")
// PutRune sends a rune to the emulator if it is valid printable ASCII
func (k *KeyboardChannel) PutRune(ch rune) {
// Some substitutions useful for Macs that transform chars with the option key
pos := slices.Index(macOptionChars, ch)
if pos >= 0 {
ch = rune(macOptionSubst[pos])
}
// We will use computed text only for printable ASCII chars
if ch >= ' ' && ch <= '~' {
if k.a.IsForceCaps() && ch >= 'a' && ch <= 'z' {
ch = unicode.ToUpper(ch)
}
k.PutChar(uint8(ch))
}
}
// PutChar sends a character to the emulator
func (k *KeyboardChannel) PutChar(ch uint8) {
k.keyChannel <- ch
}
// GetKey returns a pressed key if available
func (k *KeyboardChannel) GetKey(_ bool) (key uint8, ok bool) {
select {
case key = <-k.keyChannel:
ok = true
default:
ok = false
}
return
}