tomo/input/input.go

187 lines
4.2 KiB
Go

// Package input defines keyboard and mouse code constants.
package input
import "unicode"
// Key represents a keyboard key.
type Key int
const (
KeyNone Key = 0
KeyInsert Key = 1
KeyMenu Key = 2
KeyPrintScreen Key = 3
KeyPause Key = 4
KeyCapsLock Key = 5
KeyScrollLock Key = 6
KeyNumLock Key = 7
KeyBackspace Key = 8
KeyTab Key = 9
KeyEnter Key = 10
KeyEscape Key = 11
KeyUp Key = 12
KeyDown Key = 13
KeyLeft Key = 14
KeyRight Key = 15
KeyPageUp Key = 16
KeyPageDown Key = 17
KeyHome Key = 18
KeyEnd Key = 19
KeyLeftShift Key = 20
KeyRightShift Key = 21
KeyLeftControl Key = 22
KeyRightControl Key = 23
KeyLeftAlt Key = 24
KeyRightAlt Key = 25
KeyLeftMeta Key = 26
KeyRightMeta Key = 27
KeyLeftSuper Key = 28
KeyRightSuper Key = 29
KeyLeftHyper Key = 30
KeyRightHyper Key = 31
KeyDelete Key = 127
KeyDead Key = 128
KeyF1 Key = 129
KeyF2 Key = 130
KeyF3 Key = 131
KeyF4 Key = 132
KeyF5 Key = 133
KeyF6 Key = 134
KeyF7 Key = 135
KeyF8 Key = 136
KeyF9 Key = 137
KeyF10 Key = 138
KeyF11 Key = 139
KeyF12 Key = 140
KeyF13 Key = 141
KeyF14 Key = 142
KeyF15 Key = 143
KeyF16 Key = 144
KeyF17 Key = 145
KeyF18 Key = 146
KeyF19 Key = 147
KeyF20 Key = 148
KeyF21 Key = 149
KeyF22 Key = 150
KeyF23 Key = 151
KeyF24 Key = 152
)
// Button represents a mouse button.
type Button int
const (
ButtonNone Button = iota
Button1
Button2
Button3
Button4
Button5
Button6
Button7
Button8
Button9
ButtonLeft Button = Button1
ButtonMiddle Button = Button2
ButtonRight Button = Button3
ButtonBack Button = Button8
ButtonForward Button = Button9
)
// Printable returns whether or not the key's character could show up on screen.
// If this function returns true, the key can be cast to a rune and used as
// such.
func (key Key) Printable () (printable bool) {
printable = unicode.IsPrint(rune(key))
return
}
// Modifiers lists what modifier keys are being pressed. These should be used
// instead of attempting to track the state of the modifier keys, because there
// is no guarantee that one press event will be coupled with one release event.
type Modifiers uint; const (
ModNone Modifiers = 0
ModShift Modifiers = 1 << iota
ModControl
ModAlt
ModMeta
ModSuper
ModHyper
)
// Shift returns whether the list of modifiers includes the shift key.
func (modifiers Modifiers) Shift () bool {
return modifiers & ModShift != ModNone
}
// Control returns whether the list of modifiers includes the control key.
func (modifiers Modifiers) Control () bool {
return modifiers & ModControl != ModNone
}
// Alt returns whether the list of modifiers includes the alt key.
func (modifiers Modifiers) Alt () bool {
return modifiers & ModAlt != ModNone
}
// Meta returns whether the list of modifiers includes the meta key.
func (modifiers Modifiers) Meta () bool {
return modifiers & ModAlt != ModNone
}
// Super returns whether the list of modifiers includes the super key.
func (modifiers Modifiers) Super () bool {
return modifiers & ModSuper != ModNone
}
// Hyper returns whether the list of modifiers includes the hyper key.
func (modifiers Modifiers) Hyper () bool {
return modifiers & ModHyper != ModNone
}
// KeyChord combines a keyboard key with Modifiers.
type KeyChord struct {
Key Key
Modifiers Modifiers
}
// KC is a convenience constructor for a KeyChord.
func KC (key Key, modifiers Modifiers) KeyChord {
return KeyChord {
Key: key,
Modifiers: modifiers,
}
}
// Pressed returns true if the given Key Modifiers match the KeyChord.
func (chord KeyChord) Pressed (key Key, modifiers Modifiers) bool {
return chord == KC(key, modifiers)
}
// ButtonChord combines a mouse button with a number of modifiers.
type ButtonChord struct {
Button Button
Modifiers Modifiers
}
// BC is a convenience constructor for a ButtonChord.
func BC (button Button, modifiers Modifiers) ButtonChord {
return ButtonChord {
Button: button,
Modifiers: modifiers,
}
}
// Pressed returns true if the given Button and Modifiers match the ButtonChord.
func (chord ButtonChord) Pressed (button Button, modifiers Modifiers) bool {
return chord == BC(button, modifiers)
}