diff --git a/examples/keypress/main.go b/examples/keypress/main.go index 3a2c8cc..98935a9 100644 --- a/examples/keypress/main.go +++ b/examples/keypress/main.go @@ -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) { diff --git a/keyboard.go b/keyboard.go index 964731b..dd06324 100644 --- a/keyboard.go +++ b/keyboard.go @@ -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 {