add keyhold and typehold

This commit is contained in:
John Gebbie 2023-01-04 22:14:21 +00:00
parent 68987774fd
commit 29da15e1d4

View file

@ -88,34 +88,6 @@ func parseChord(chord string) (Chord, error) {
return c, nil return c, nil
} }
func (c *Chord) Press(kb uinput.Keyboard) {
if c.Super {
log(kb.KeyDown(uinput.KeyLeftmeta))
}
if c.Ctrl {
log(kb.KeyDown(uinput.KeyLeftctrl))
}
if c.Alt {
log(kb.KeyDown(uinput.KeyLeftalt))
}
if c.Shift {
log(kb.KeyDown(uinput.KeyLeftshift))
}
log(kb.KeyPress(c.Key))
if c.Super {
log(kb.KeyUp(uinput.KeyLeftmeta))
}
if c.Ctrl {
log(kb.KeyUp(uinput.KeyLeftctrl))
}
if c.Alt {
log(kb.KeyUp(uinput.KeyLeftalt))
}
if c.Shift {
log(kb.KeyUp(uinput.KeyLeftshift))
}
}
func (c *Chord) KeyDown(kb uinput.Keyboard) { func (c *Chord) KeyDown(kb uinput.Keyboard) {
if c.Super { if c.Super {
log(kb.KeyDown(uinput.KeyLeftmeta)) log(kb.KeyDown(uinput.KeyLeftmeta))
@ -164,7 +136,9 @@ The commands are:
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 keydelay MILLISECONDS
keyhold MILLISECONDS
typedelay MILLISECONDS typedelay MILLISECONDS
typehold MILLISECONDS
Example: echo "key h i shift+1" | dotool Example: echo "key h i shift+1" | dotool
@ -272,7 +246,9 @@ func main() {
defer mouse.Close() defer mouse.Close()
keydelay := time.Duration(0) keydelay := time.Duration(0)
keyhold := time.Duration(0)
typedelay := time.Duration(0) typedelay := time.Duration(0)
typehold := time.Duration(0)
sc := bufio.NewScanner(os.Stdin) sc := bufio.NewScanner(os.Stdin)
for sc.Scan() { for sc.Scan() {
@ -283,7 +259,9 @@ 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) {
if chord, err := parseChord(field); err == nil { if chord, err := parseChord(field); err == nil {
chord.Press(keyboard) chord.KeyDown(keyboard)
time.Sleep(keyhold)
chord.KeyUp(keyboard)
} else { } else {
warn(err.Error()) warn(err.Error())
} }
@ -315,13 +293,23 @@ func main() {
} else { } else {
warn("invalid delay: " + sc.Text()) warn("invalid delay: " + sc.Text())
} }
} else if s, ok := cutCmd(text, "keyhold"); ok {
var d float64
_, err := fmt.Sscanf(s + "\n", "%f\n", &d)
if err == nil {
keyhold = time.Duration(d)*time.Millisecond
} else {
warn("invalid hold time: " + 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 {
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
} }
chord.Press(keyboard) chord.KeyDown(keyboard)
time.Sleep(typehold)
chord.KeyUp(keyboard)
} }
time.Sleep(typedelay) time.Sleep(typedelay)
} }
@ -333,6 +321,14 @@ func main() {
} else { } else {
warn("invalid delay: " + sc.Text()) warn("invalid delay: " + sc.Text())
} }
} else if s, ok := cutCmd(text, "typehold"); ok {
var d float64
_, err := fmt.Sscanf(s + "\n", "%f\n", &d)
if err == nil {
typehold = time.Duration(d)*time.Millisecond
} else {
warn("invalid hold time: " + 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 {