add keydelay and typedelay subcommands

This commit is contained in:
John Gebbie 2022-10-23 11:27:26 +01:00
parent ce63460c14
commit 825781901c

View file

@ -10,6 +10,7 @@ import (
"os" "os"
"strconv" "strconv"
"strings" "strings"
"time"
"unicode" "unicode"
) )
@ -162,6 +163,8 @@ The commands are:
scroll NUMBER (where NUMBER is the amount down/up if positive/negative) scroll NUMBER (where NUMBER is the amount down/up if positive/negative)
mouseto X Y (where X and Y are percentages between 0.0 and 1.0) mouseto X Y (where X and Y are percentages between 0.0 and 1.0)
mousemove X Y (where X and Y are the number of pixels to move) mousemove X Y (where X and Y are the number of pixels to move)
keydelay MILLISECONDS
typedelay MILLISECONDS
Example: echo "key h i shift+1" | dotool Example: echo "key h i shift+1" | dotool
@ -257,6 +260,9 @@ func main() {
} }
defer mouse.Close() defer mouse.Close()
keydelay := time.Duration(0)
typedelay := time.Duration(0)
sc := bufio.NewScanner(os.Stdin) sc := bufio.NewScanner(os.Stdin)
for sc.Scan() { for sc.Scan() {
text := strings.TrimLeftFunc(sc.Text(), unicode.IsSpace) text := strings.TrimLeftFunc(sc.Text(), unicode.IsSpace)
@ -265,6 +271,7 @@ func main() {
} }
if s, ok := cutCmd(text, "key"); ok { if s, ok := cutCmd(text, "key"); ok {
for _, field := range strings.Fields(s) { for _, field := range strings.Fields(s) {
time.Sleep(keydelay)
if chord, err := parseChord(field); err == nil { if chord, err := parseChord(field); err == nil {
chord.Press(keyboard) chord.Press(keyboard)
} else { } else {
@ -273,6 +280,7 @@ func main() {
} }
} else if s, ok := cutCmd(text, "keydown"); ok { } else if s, ok := cutCmd(text, "keydown"); ok {
for _, field := range strings.Fields(s) { for _, field := range strings.Fields(s) {
time.Sleep(keydelay)
if chord, err := parseChord(field); err == nil { if chord, err := parseChord(field); err == nil {
chord.KeyDown(keyboard) chord.KeyDown(keyboard)
} else { } else {
@ -281,14 +289,24 @@ func main() {
} }
} else if s, ok := cutCmd(text, "keyup"); ok { } else if s, ok := cutCmd(text, "keyup"); ok {
for _, field := range strings.Fields(s) { for _, field := range strings.Fields(s) {
time.Sleep(keydelay)
if chord, err := parseChord(field); err == nil { if chord, err := parseChord(field); err == nil {
chord.KeyUp(keyboard) chord.KeyUp(keyboard)
} else { } else {
warn(err.Error()) warn(err.Error())
} }
} }
} else if s, ok := cutCmd(text, "keydelay"); ok {
var d float64
_, err := fmt.Sscanf(s + "\n", "%f\n", &d)
if err == nil {
keydelay = time.Duration(d)*time.Millisecond
} else {
warn("invalid delay: " + sc.Text())
}
} else if s, ok := cutCmd(text, "type"); ok { } else if s, ok := cutCmd(text, "type"); ok {
for _, r := range s { for _, r := range s {
time.Sleep(typedelay)
if chord, ok := runeChords[unicode.ToLower(r)]; ok { if chord, ok := runeChords[unicode.ToLower(r)]; ok {
if unicode.IsUpper(r) { if unicode.IsUpper(r) {
chord.Shift = true chord.Shift = true
@ -296,6 +314,14 @@ func main() {
chord.Press(keyboard) chord.Press(keyboard)
} }
} }
} else if s, ok := cutCmd(text, "typedelay"); ok {
var d float64
_, err := fmt.Sscanf(s + "\n", "%f\n", &d)
if err == nil {
typedelay = time.Duration(d)*time.Millisecond
} else {
warn("invalid delay: " + sc.Text())
}
} else if s, ok := cutCmd(text, "click"); ok { } else if s, ok := cutCmd(text, "click"); ok {
for _, button := range strings.Fields(s) { for _, button := range strings.Fields(s) {
switch button { switch button {