goldwarden/autofill/uinput/uinput.go
Bernd Schoolmann 30237e79b2
Initial commit
2023-07-17 03:23:26 +02:00

171 lines
3.4 KiB
Go

package uinput
import (
"errors"
"fmt"
"time"
"github.com/bendahl/uinput"
)
type Layout interface {
TypeKey(key Key, keyboard uinput.Keyboard) error
}
type Key string
const (
KeyA Key = "a"
KeyB Key = "b"
KeyC Key = "c"
KeyD Key = "d"
KeyE Key = "e"
KeyF Key = "f"
KeyG Key = "g"
KeyH Key = "h"
KeyI Key = "i"
KeyJ Key = "j"
KeyK Key = "k"
KeyL Key = "l"
KeyM Key = "m"
KeyN Key = "n"
KeyO Key = "o"
KeyP Key = "p"
KeyQ Key = "q"
KeyR Key = "r"
KeyS Key = "s"
KeyT Key = "t"
KeyU Key = "u"
KeyV Key = "v"
KeyW Key = "w"
KeyX Key = "x"
KeyY Key = "y"
KeyZ Key = "z"
KeyAUpper Key = "A"
KeyBUpper Key = "B"
KeyCUpper Key = "C"
KeyDUpper Key = "D"
KeyEUpper Key = "E"
KeyFUpper Key = "F"
KeyGUpper Key = "G"
KeyHUpper Key = "H"
KeyIUpper Key = "I"
KeyJUpper Key = "J"
KeyKUpper Key = "K"
KeyLUpper Key = "L"
KeyMUpper Key = "M"
KeyNUpper Key = "N"
KeyOUpper Key = "O"
KeyPUpper Key = "P"
KeyQUpper Key = "Q"
KeyRUpper Key = "R"
KeySUpper Key = "S"
KeyTUpper Key = "T"
KeyUUpper Key = "U"
KeyVUpper Key = "V"
KeyWUpper Key = "W"
KeyXUpper Key = "X"
KeyYUpper Key = "Y"
KeyZUpper Key = "Z"
Key0 Key = "0"
Key1 Key = "1"
Key2 Key = "2"
Key3 Key = "3"
Key4 Key = "4"
Key5 Key = "5"
Key6 Key = "6"
Key7 Key = "7"
Key8 Key = "8"
Key9 Key = "9"
KeyHyphen Key = "-"
KeyAtSign Key = "@"
KeySpace Key = " "
KeyExclamationMark Key = "!"
KeyDollar Key = "$"
KeyEqual Key = "="
KeySemicolon Key = ";"
KeyColon Key = ":"
KeyComma Key = ","
KeyPeriod Key = "."
KeySlash Key = "/"
KeyBackslash Key = "\\"
KeyPound Key = "#"
KeyPercent Key = "%"
KeyCaret Key = "^"
KeyAmpersand Key = "&"
KeyAsterisk Key = "*"
KeyPlus Key = "+"
KeyEquals Key = "="
KeyUnderscore Key = "_"
KeyTab Key = "\t"
)
type LayoutRegistry struct {
layouts map[string]Layout
}
func NewLayoutRegistry() *LayoutRegistry {
return &LayoutRegistry{
layouts: make(map[string]Layout),
}
}
var DefaultLayoutRegistry = NewLayoutRegistry()
func (r *LayoutRegistry) Register(name string, layout Layout) {
r.layouts[name] = layout
}
func TypeString(text string, layout string) error {
if layout == "" {
layout = "qwerty"
}
if _, ok := DefaultLayoutRegistry.layouts[layout]; !ok {
return errors.New("layout not found")
}
keyboard, err := uinput.CreateKeyboard("/dev/uinput", []byte("testkeyboard"))
if err != nil {
return err
}
for _, c := range text {
key := Key(string(c))
err := DefaultLayoutRegistry.layouts[layout].TypeKey(key, keyboard)
if err != nil {
fmt.Println(err)
}
}
err = keyboard.Close()
if err != nil {
return err
}
return nil
}
func Paste(layout string) error {
if layout == "" {
layout = "qwerty"
}
if _, ok := DefaultLayoutRegistry.layouts[layout]; !ok {
return errors.New("layout not found")
}
keyboard, err := uinput.CreateKeyboard("/dev/uinput", []byte("Goldwarden Autotype"))
if err != nil {
return err
}
keyboard.KeyDown(uinput.KeyLeftctrl)
time.Sleep(100 * time.Millisecond)
DefaultLayoutRegistry.layouts[layout].TypeKey(KeyV, keyboard)
time.Sleep(100 * time.Millisecond)
keyboard.KeyUp(uinput.KeyLeftctrl)
return nil
}