-
Notifications
You must be signed in to change notification settings - Fork 4
/
modifiers.go
67 lines (59 loc) · 1.81 KB
/
modifiers.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
package gowinkey
import (
"math/bits"
"strconv"
"strings"
)
// Modifiers represents modifiers that are pressed
// alongside some virtual key. See the flags below
// for more info.
type Modifiers uint
// These flags define which modifiers are being pressed alongside
// some virtual key. Bits are or'ed to build a modifier field. See
// KeyEvent.Modifiers for more info.
//
// Note that these flags do not differentiate between the
// 'left' and 'right' versions of keys. So, for instance,
// pressing either 'left shift' or 'right shift' will simply
// result in ModifierShift being used to build the modifier
// field.
const (
// ModifierShift identifies any 'shift' modifier.
ModifierShift Modifiers = 1 << iota
// ModifierMenu identifies any 'alt' modifier.
ModifierMenu
// ModifierControl identifies any 'ctrl' modifier.
ModifierControl
)
// modifiersToStr maps each of the known
// modifiers to its string representation.
var modifiersToStr = map[Modifiers]string{
ModifierShift: "Shift",
ModifierMenu: "Alt",
ModifierControl: "Ctrl",
}
// HasModifiers reports whether m contains the given modifiers.
func (m Modifiers) HasModifiers(modifiers Modifiers) bool {
return m&modifiers != 0
}
// RemoveModifiers returns a copy of m with the given modifiers removed.
func (m Modifiers) RemoveModifiers(modifiers Modifiers) Modifiers {
return m & ^modifiers
}
// String returns the string representation of m.
func (m Modifiers) String() string {
var mods []string
for mod, s := range modifiersToStr {
if m.HasModifiers(mod) {
mods = append(mods, s)
}
}
if len(mods) != bits.OnesCount(uint(m)) {
panic("unknown modifiers: " + m.toBinary())
}
return strings.Join(mods, " + ")
}
// toBinary returns the string representation of m in base 2.
func (m Modifiers) toBinary() string {
return strconv.FormatInt(int64(m), 2)
}