keyboard/input/simulator.go

55 lines
1.1 KiB
Go

package input
import (
"fmt"
"runtime"
"strings"
"github.com/go-vgo/robotgo"
)
type KeySimulator interface {
PressKey(key string) error
}
type keySimulator struct{}
func NewKeySimulator() KeySimulator {
return &keySimulator{}
}
func (k *keySimulator) PressKey(key string) error {
switch runtime.GOOS {
case "linux":
return k.pressKeyLinux(key)
case "windows":
return k.pressKeyWindows(key)
default:
return fmt.Errorf("unsupported operating system: %s", runtime.GOOS)
}
}
func (k *keySimulator) pressKeyLinux(key string) error {
return k.pressKeyWithRobotGo(key)
}
func (k *keySimulator) pressKeyWindows(key string) error {
return k.pressKeyWithRobotGo(key)
}
func (k *keySimulator) pressKeyWithRobotGo(key string) error {
keyMap := map[string]string{
"0": "0", "1": "1", "2": "2", "3": "3", "4": "4",
"5": "5", "6": "6", "7": "7", "8": "8", "9": "9",
"*": "*", "+": "+", "-": "-", ".": ".", "/": "/",
"enter": "return", "backspace": "backspace", "escape": "escape",
}
mappedKey, exists := keyMap[strings.ToLower(key)]
if !exists {
mappedKey = key
}
robotgo.KeyTap(mappedKey)
return nil
}