forked from quasilyte/ebitengine-input
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse_key.go
75 lines (71 loc) · 1.62 KB
/
parse_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
package input
import (
"errors"
"sort"
"strings"
)
// ParseKeys tries to construct an appropriate Key object given its name.
//
// It can also be used as a string->key constructor:
//
// ParseKey("left") // returns KeyLeft
// ParseKey("gamepad_left") // returns KeyGamepadLeft
//
// The format is one of the following:
//
// * keyname
// * mod+keyname
// * mod+mod+keyname
//
// Some valid input examples:
//
// * "gamepad_left"
// * "left"
// * "ctrl+left"
// * "ctrl+shift+left"
// * "shift+ctrl+left"
//
// See Handler.ActionKeyNames() for more information about the key names.
func ParseKey(s string) (Key, error) {
plusPos := strings.LastIndex(s, "+")
if plusPos == -1 {
k := keyByName(s)
if (k == Key{}) {
return k, errors.New("unknown key: " + s)
}
return k, nil
}
modName := s[:plusPos]
keyName := s[plusPos+1:]
mod := keyModifierByName(modName)
if mod == ModUnknown {
return Key{}, errors.New("unknown key modifier: " + modName)
}
k := keyByName(keyName)
if (k == Key{}) {
return k, errors.New("unknown key: " + keyName)
}
return KeyWithModifier(k, mod), nil
}
func keyModifierByName(name string) KeyModifier {
switch name {
case "ctrl":
return ModControl
case "shift":
return ModShift
case "ctrl+shift", "shift+ctrl":
return ModControlShift
default:
return ModUnknown
}
}
func keyByName(name string) Key {
// Keys are sorted by a name, so we can use a binary search here.
i := sort.Search(len(allKeys), func(i int) bool {
return allKeys[i].name >= name
})
if i < len(allKeys) && allKeys[i].name == name {
return allKeys[i]
}
return Key{}
}