Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consecutive key presses #20

Merged
merged 7 commits into from
May 18, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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