Skip to content

Commit

Permalink
Support different actions for long-presses on keys
Browse files Browse the repository at this point in the history
  • Loading branch information
muesli committed Feb 4, 2020
1 parent 026442a commit 0fa23f9
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 13 deletions.
7 changes: 4 additions & 3 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,10 @@ type WidgetConfig struct {
}

type KeyConfig struct {
Index uint8 `toml:"index"`
Widget WidgetConfig `toml:"widget"`
Action *ActionConfig `toml:"action,omitempty"`
Index uint8 `toml:"index"`
Widget WidgetConfig `toml:"widget"`
Action *ActionConfig `toml:"action,omitempty"`
ActionHold *ActionConfig `toml:"action_hold,omitempty"`
}
type Keys []KeyConfig

Expand Down
15 changes: 11 additions & 4 deletions deck.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func LoadDeck(deck string) (*Deck, error) {
}

for _, k := range dc.Keys {
w := NewWidget(k.Index, k.Widget.ID, k.Action, k.Widget.Config)
w := NewWidget(k.Index, k.Widget.ID, k.Action, k.ActionHold, k.Widget.Config)
d.Widgets = append(d.Widgets, w)
}

Expand Down Expand Up @@ -81,12 +81,19 @@ func executeCommand(cmd string) {
}
}

func (d *Deck) triggerAction(index uint8) {
// triggerAction triggers an action
func (d *Deck) triggerAction(index uint8, hold bool) {
for _, w := range d.Widgets {
if w.Key() == index {
a := w.Action()
var a *ActionConfig
if hold {
a = w.ActionHold()
} else {
a = w.Action()
}

if a != nil {
fmt.Println("Executing overwritten action")
fmt.Println("Executing overloaded action")
if a.Deck != "" {
d, err := LoadDeck(a.Deck)
if err != nil {
Expand Down
33 changes: 31 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"log"
"os"
"sync"
"time"

"github.com/bendahl/uinput"
Expand Down Expand Up @@ -120,6 +121,9 @@ func main() {
defer keyboard.Close()
}

var keyStates sync.Map
keyTimestamps := make(map[uint8]time.Time)

kch, err := dev.ReadKeys()
if err != nil {
log.Fatal(err)
Expand All @@ -128,6 +132,7 @@ func main() {
select {
case <-time.After(500 * time.Millisecond):
deck.updateWidgets()

case k, ok := <-kch:
if !ok {
err = dev.Open()
Expand All @@ -138,9 +143,33 @@ func main() {
}
spew.Dump(k)

if k.Pressed {
deck.triggerAction(k.Index)
var state bool
if ks, ok := keyStates.Load(k.Index); ok {
state = ks.(bool)
}

if state && !k.Pressed {
// key was released
if time.Since(keyTimestamps[k.Index]) < 200*time.Millisecond {
deck.triggerAction(k.Index, false)
}
}
if !state && k.Pressed {
// key was pressed
go func() {
// launch timer to observe keystate
time.Sleep(200 * time.Millisecond)

if state, ok := keyStates.Load(k.Index); ok && state.(bool) {
// key still pressed
deck.triggerAction(k.Index, true)
}
}()
}

keyStates.Store(k.Index, k.Pressed)
keyTimestamps[k.Index] = time.Now()

case e := <-tch:
switch event := e.(type) {
case WindowClosedEvent:
Expand Down
14 changes: 10 additions & 4 deletions widget.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,14 @@ type Widget interface {
Key() uint8
Update(dev *streamdeck.Device)
Action() *ActionConfig
ActionHold() *ActionConfig
TriggerAction()
}

type BaseWidget struct {
key uint8
action *ActionConfig
key uint8
action *ActionConfig
actionHold *ActionConfig
}

func (w *BaseWidget) Key() uint8 {
Expand All @@ -44,11 +46,15 @@ func (w *BaseWidget) Action() *ActionConfig {
return w.action
}

func (w *BaseWidget) ActionHold() *ActionConfig {
return w.actionHold
}

func (w *BaseWidget) TriggerAction() {
}

func NewWidget(index uint8, id string, action *ActionConfig, config map[string]string) Widget {
bw := BaseWidget{index, action}
func NewWidget(index uint8, id string, action *ActionConfig, actionHold *ActionConfig, config map[string]string) Widget {
bw := BaseWidget{index, action, actionHold}

switch id {
case "button":
Expand Down

0 comments on commit 0fa23f9

Please sign in to comment.