1
0
Fork 0
mirror of https://github.com/Luzifer/streamdeck.git synced 2024-12-20 17:51:21 +00:00

Add support for raw key codes

Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
Knut Ahlers 2020-06-06 17:05:21 +02:00
parent c8a6c79369
commit d68422f759
Signed by: luzifer
GPG key ID: DC2729FDD34BE99E

View file

@ -15,37 +15,52 @@ func init() {
type actionKeyPress struct{} type actionKeyPress struct{}
func (actionKeyPress) Execute(attributes map[string]interface{}) error { func (actionKeyPress) Execute(attributes map[string]interface{}) error {
keys, ok := attributes["keys"].([]interface{})
if !ok {
return errors.New("No keys array present")
}
var ( var (
delay time.Duration delay time.Duration
keyCodes []uint16 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 v, ok := attributes["delay"].(string); ok {
if d, err := time.ParseDuration(v); err == nil { if d, err := time.ParseDuration(v); err == nil {
delay = d delay = d
} }
} }
for _, k := range keys { if useKeyNames {
// Convert misdetections into strings for _, k := range keyNames {
switch k.(type) { // Convert misdetections into strings
case int: switch k.(type) {
k = strconv.Itoa(k.(int)) 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)
} }
} 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) defer kbd.KeyUp(uinput.KeyLeftCtrl)
} }
for _, kc := range keyCodes { for _, kc := range execCodes {
if err := kbd.KeyPress(kc); err != nil { if err := kbd.KeyPress(kc); err != nil {
return errors.Wrap(err, "Unable to press key") return errors.Wrap(err, "Unable to press key")
} }