Added String method to Modifiers

This commit is contained in:
Sasha Koshka 2023-06-08 00:08:38 -04:00
parent e114c6d700
commit aa46cada1f
2 changed files with 22 additions and 2 deletions

View File

@ -67,10 +67,11 @@ func main() {
xevent.KeyPressFun(
func(X *xgbutil.XUtil, e xevent.KeyPressEvent) {
symbol, character := xgbkb.KeycodeToKeysym( e.Detail, e.State)
modifiers := xgbkb.StateToModifiers(e.State)
if unicode.IsPrint(character) {
log.Printf("0x%04X | '%s'\n", symbol, string(character))
log.Printf("0x%04X | '%s' %v\n", symbol, string(character), modifiers)
} else {
log.Printf("0x%04X |\n", symbol)
log.Printf("0x%04X | %v\n", symbol, modifiers)
}
if keybind.KeyMatch(X, "Escape", e.State, e.Detail) {

View File

@ -61,6 +61,25 @@ type Modifiers struct {
Hyper bool
}
// String returns a human-readable comma-separated list of all active modifiers.
func (modifiers Modifiers) String () (out string) {
add := func (name string) {
if out != "" {
out += ", "
}
out += name
}
if modifiers.Hyper { add("Hyper") }
if modifiers.Super { add("Super") }
if modifiers.Meta { add("Meta") }
if modifiers.Alt { add("Alt") }
if modifiers.ModeSwitch { add("ModeSwitch") }
if modifiers.NumLock { add("NumLock") }
if modifiers.ShiftLock { add("ShiftLock") }
if modifiers.CapsLock { add("CapsLock") }
return
}
// StateToModifiers converts a modifier state given by a keyboard or mouse event
// to a Modifiers struct.
func StateToModifiers (state uint16) Modifiers {