From 0206ba448ed7b4a2b7a2c27c59d155d36d06a6ca Mon Sep 17 00:00:00 2001 From: lordwelch Date: Fri, 13 May 2022 09:44:04 -0700 Subject: [PATCH] Enable go module Set go version to 1.18 Add arguments to set press delay and release delay Add better handling for unknown characters --- ghid/keyboard.go | 18 +++++++++++------- go.mod | 3 +++ main.go | 32 +++++++++++++++++++++----------- 3 files changed, 35 insertions(+), 18 deletions(-) create mode 100644 go.mod diff --git a/ghid/keyboard.go b/ghid/keyboard.go index bbfba6b..426393e 100644 --- a/ghid/keyboard.go +++ b/ghid/keyboard.go @@ -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 err != nil { - return index, err + 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) } - return index, fmt.Errorf("rune not in keymap: %c", r) + index += s + break press } - } else { + } 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 } diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..607935c --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module github.com/lordwelch/hid + +go 1.18 diff --git a/main.go b/main.go index 73f3c7b..46ae0c8 100644 --- a/main.go +++ b/main.go @@ -6,19 +6,23 @@ import ( "io" "os" "path" + "time" - hid "timmy.narnian.us/hid/ghid" + hid "github.com/lordwelch/hid/ghid" ) func main() { var ( - Shortcut string - filePath string - keymapPath string - err error - ghid0 *os.File - tmp *os.File - keyboard *hid.Keyboard + Shortcut string + filePath string + keymapPath string + ghidPath string + pressDelay time.Duration + releaseDelay time.Duration + err error + ghid *os.File + tmp *os.File + keyboard *hid.Keyboard ) if _, exists := os.LookupEnv("XDG_CONFIG_HOME"); !exists { _ = os.Setenv("XDG_CONFIG_HOME", path.Join(os.ExpandEnv("$HOME"), ".config")) @@ -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)