Enable go module

Set go version to 1.18
Add arguments to set press delay and release delay
Add better handling for unknown characters
This commit is contained in:
lordwelch 2022-05-13 09:44:04 -07:00
parent 0bbf455bc7
commit 0206ba448e
3 changed files with 35 additions and 18 deletions

View File

@ -103,15 +103,19 @@ func (k *Keyboard) Write(p []byte) (int, error) {
}
cur, ok := k.CurrentKeymap()[string(r)]
if !ok {
if i == 3 { // can't press two keys from different keymaps
if i == 2 { // We can change the keymap if we are on the first key
ok, err = k.changeKeymap(r)
if !ok && k.ErrOnUnknownKey {
if !ok { // rune does not have a mapping
if k.ErrOnUnknownKey {
if err != nil {
return index, err
}
return index, fmt.Errorf("rune not in keymap: %c", r)
}
} else {
index += s
break press
}
} else { // rune does not have a mapping in this keymaps
break press
}
}
@ -157,7 +161,7 @@ func (k *Keyboard) Write(p []byte) (int, error) {
if report[i-1] != 0 {
break press
}
// Add the modifier of the current key eg 'D' adds shift 'd' does not
// Add the modifier of the current key eg 'D' adds shift; 'd' does not
flag |= mod
}

3
go.mod Normal file
View File

@ -0,0 +1,3 @@
module github.com/lordwelch/hid
go 1.18

20
main.go
View File

@ -6,8 +6,9 @@ import (
"io"
"os"
"path"
"time"
hid "timmy.narnian.us/hid/ghid"
hid "github.com/lordwelch/hid/ghid"
)
func main() {
@ -15,8 +16,11 @@ func main() {
Shortcut string
filePath string
keymapPath string
ghidPath string
pressDelay time.Duration
releaseDelay time.Duration
err error
ghid0 *os.File
ghid *os.File
tmp *os.File
keyboard *hid.Keyboard
)
@ -29,6 +33,10 @@ func main() {
flag.StringVar(&keymapPath, "p", path.Join(os.ExpandEnv("$XDG_CONFIG_HOME"), "hid"), "Path to config dir default: $XDG_CONFIG_HOME")
flag.StringVar(&filePath, "f", "-", "The file to read content from. Defaults to stdin")
flag.StringVar(&filePath, "file", "-", "The file to read content from. Defaults to stdin")
flag.StringVar(&ghidPath, "g", "/dev/hidg0", "The device to send key presses to. Defaults to /dev/hidg0")
flag.StringVar(&ghidPath, "ghid", "/dev/hidg0", "The device to send key presses to. Defaults to /dev/hidg0")
flag.DurationVar(&pressDelay, "press", 0, "sets the default delay between presses of individual keys")
flag.DurationVar(&releaseDelay, "press", 0, "sets the default delay between sending the press of an individual key and sending the release")
flag.Parse()
if flag.NArg() < 0 {
flag.Usage()
@ -44,13 +52,15 @@ func main() {
}
}
ghid0, err = os.OpenFile("/dev/hidg0", os.O_APPEND|os.O_WRONLY, 0600)
ghid, err = os.OpenFile(ghidPath, os.O_APPEND|os.O_WRONLY, 0600)
if err != nil {
panic(err)
}
defer ghid0.Close()
defer ghid.Close()
keyboard = hid.NewKeyboard(hid.Modifiers, flag.Args(), keymapPath, ghid0)
keyboard = hid.NewKeyboard(hid.Modifiers, flag.Args(), keymapPath, ghid)
keyboard.PressDelay = pressDelay
keyboard.ReleaseDelay = releaseDelay
_, err = io.Copy(keyboard, os.Stdin)