Skip to content

Commit

Permalink
Consecutive key presses (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
pheerai authored May 18, 2021
1 parent 0a0b293 commit a85afb6
Showing 1 changed file with 24 additions and 2 deletions.
26 changes: 24 additions & 2 deletions deck.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"os/exec"
"strconv"
"strings"
"time"

"github.com/atotto/clipboard"
"github.com/godbus/dbus"
Expand All @@ -32,6 +33,27 @@ func LoadDeck(deck string) (*Deck, error) {
return &d, nil
}

// handles keypress with delay.
func emulateKeyPressWithDelay(keys string) {
kd := strings.Split(keys, "+")
emulateKeyPress(kd[0])
if len(kd) == 1 {
return
}

// optional delay
if delay, err := strconv.Atoi(strings.TrimSpace(kd[1])); err == nil {
time.Sleep(time.Duration(delay) * time.Millisecond)
}
}

// emulates a range of key presses.
func emulateKeyPresses(keys string) {
for _, kp := range strings.Split(keys, "/") {
emulateKeyPressWithDelay(kp)
}
}

// emulates a (multi-)key press.
func emulateKeyPress(keys string) {
if keyboard == nil {
Expand All @@ -41,7 +63,7 @@ func emulateKeyPress(keys string) {

kk := strings.Split(keys, "-")
for i, k := range kk {
kc, err := strconv.Atoi(k)
kc, err := strconv.Atoi(strings.TrimSpace(k))
if err != nil {
log.Fatalf("%s is not a valid keycode: %s", k, err)
}
Expand Down Expand Up @@ -115,7 +137,7 @@ func (d *Deck) triggerAction(index uint8, hold bool) {
deck.updateWidgets()
}
if a.Keycode != "" {
emulateKeyPress(a.Keycode)
emulateKeyPresses(a.Keycode)
}
if a.Paste != "" {
emulateClipboard(a.Paste)
Expand Down

0 comments on commit a85afb6

Please sign in to comment.