From d68422f759733bb0345bd6bd97bcee1b0fe1ad87 Mon Sep 17 00:00:00 2001 From: Knut Ahlers Date: Sat, 6 Jun 2020 17:05:21 +0200 Subject: [PATCH] Add support for raw key codes Signed-off-by: Knut Ahlers --- cmd/streamdeck/action_keyPress.go | 59 +++++++++++++++++++------------ 1 file changed, 37 insertions(+), 22 deletions(-) diff --git a/cmd/streamdeck/action_keyPress.go b/cmd/streamdeck/action_keyPress.go index 4ac1219..1791a3c 100644 --- a/cmd/streamdeck/action_keyPress.go +++ b/cmd/streamdeck/action_keyPress.go @@ -15,37 +15,52 @@ func init() { type actionKeyPress struct{} func (actionKeyPress) Execute(attributes map[string]interface{}) error { - keys, ok := attributes["keys"].([]interface{}) - if !ok { - return errors.New("No keys array present") - } - var ( - delay time.Duration - keyCodes []uint16 + delay time.Duration + execCodes []uint16 + ok bool + + keyNames []interface{} + keyCodes []interface{} + useKeyNames bool ) + keyCodes, ok = attributes["key_codes"].([]interface{}) + if !ok { + keyNames, ok = attributes["keys"].([]interface{}) + if !ok { + return errors.New("No key_codes or keys array present") + } + useKeyNames = true + } + if v, ok := attributes["delay"].(string); ok { if d, err := time.ParseDuration(v); err == nil { delay = d } } - for _, k := range keys { - // Convert misdetections into strings - switch k.(type) { - case int: - k = strconv.Itoa(k.(int)) - } - - if kv, ok := k.(string); ok { - if kc, ok := uinputKeyMapping[kv]; ok { - keyCodes = append(keyCodes, kc) - } else { - return errors.Errorf("Unknown key %q", kv) + if useKeyNames { + for _, k := range keyNames { + // Convert misdetections into strings + switch k.(type) { + case int: + k = strconv.Itoa(k.(int)) } - } else { - return errors.New("Unknown key type detected") + + if kv, ok := k.(string); ok { + if kc, ok := uinputKeyMapping[kv]; ok { + execCodes = append(execCodes, kc) + } else { + return errors.Errorf("Unknown key %q", kv) + } + } else { + return errors.New("Unknown key type detected") + } + } + } else { + for _, k := range keyCodes { + execCodes = append(execCodes, uint16(k.(int))) } } @@ -70,7 +85,7 @@ func (actionKeyPress) Execute(attributes map[string]interface{}) error { defer kbd.KeyUp(uinput.KeyLeftCtrl) } - for _, kc := range keyCodes { + for _, kc := range execCodes { if err := kbd.KeyPress(kc); err != nil { return errors.Wrap(err, "Unable to press key") }