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)] cur, ok := k.CurrentKeymap()[string(r)]
if !ok { 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) ok, err = k.changeKeymap(r)
if !ok && k.ErrOnUnknownKey { if !ok { // rune does not have a mapping
if err != nil { if k.ErrOnUnknownKey {
return index, err 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 break press
} }
} }
@ -157,7 +161,7 @@ func (k *Keyboard) Write(p []byte) (int, error) {
if report[i-1] != 0 { if report[i-1] != 0 {
break press 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 flag |= mod
} }

3
go.mod Normal file
View File

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

32
main.go
View File

@ -6,19 +6,23 @@ import (
"io" "io"
"os" "os"
"path" "path"
"time"
hid "timmy.narnian.us/hid/ghid" hid "github.com/lordwelch/hid/ghid"
) )
func main() { func main() {
var ( var (
Shortcut string Shortcut string
filePath string filePath string
keymapPath string keymapPath string
err error ghidPath string
ghid0 *os.File pressDelay time.Duration
tmp *os.File releaseDelay time.Duration
keyboard *hid.Keyboard err error
ghid *os.File
tmp *os.File
keyboard *hid.Keyboard
) )
if _, exists := os.LookupEnv("XDG_CONFIG_HOME"); !exists { if _, exists := os.LookupEnv("XDG_CONFIG_HOME"); !exists {
_ = os.Setenv("XDG_CONFIG_HOME", path.Join(os.ExpandEnv("$HOME"), ".config")) _ = 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(&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, "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(&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() flag.Parse()
if flag.NArg() < 0 { if flag.NArg() < 0 {
flag.Usage() 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 { if err != nil {
panic(err) 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) _, err = io.Copy(keyboard, os.Stdin)