Compare commits

...

2 Commits

Author SHA1 Message Date
Kevin F
576e70a989 Better handle special characters 2023-01-22 19:31:21 -06:00
Kevin F
027a62571f Client now automatically specifies xdotool if keys not on qwerty are present 2023-01-22 19:23:52 -06:00
2 changed files with 68 additions and 23 deletions

View File

@ -38,6 +38,7 @@ The base64 authentication token is loaded from the environment variable `KEYBOAR
@{load connection URL from second CLI argument}
@{get authTokenInput from environment}
@{add xdotool if non qwerty function}
if !authTokenInputExists {
fmt.Print("Enter authentication token: ")
@ -94,11 +95,15 @@ if !strings.HasPrefix(connectionURL, "ws://") && !strings.HasPrefix(connectionUR
--- start client with fifo
var inputString string
for {
input, err := ioutil.ReadFile(clientFifoInputFile)
if err != nil {
log.Fatal(err)
}
inputString = addXDoToolIfNonQWERTY(string(input))
input = []byte(inputString)
if len(input) > 0 {
fmt.Println("send" + strings.Replace(string(input), " ", "space", 10))
err = client.WriteMessage(websocket.TextMessage, input)
@ -129,7 +134,7 @@ for {
rune, _, err := reader.ReadRune() //:= fmt.Scan(&key)
key = string(rune)
key = addXDoToolIfNonQWERTY(string(rune))
if err != nil {
if err == io.EOF {
@ -147,3 +152,26 @@ for {
---
```
# Handling unicode outside of the ASCII set
``` go
--- add xdotool if non qwerty function --- noWeave
addXDoToolIfNonQWERTY := func(message string)(string) {
if strings.HasPrefix(message, "{kb_cmd:xdotool}:") {
return message
}
for _, char := range message {
if char < 32 || char > 126 {
if char != 8 && char != 9 && char != 10 {
return "{kb_cmd:xdotool}:" + message
}
}
}
return message
}
---
```

View File

@ -14,8 +14,6 @@ To specify xdotool usage, the client should send a message with the format `{kb_
func clientConnected(w http.ResponseWriter, r *http.Request) {
keyboard, err := sendkeys.NewKBWrapWithOptions(sendkeys.Noisy)
// regex if string has characters we need to convert to key presses
characterRegex, _ := regexp.Compile(`[^\x08]\x08|\t|\n`)
if err != nil {
panic(err)
@ -63,14 +61,23 @@ func clientConnected(w http.ResponseWriter, r *http.Request) {
# Sending the keys
Sending the keys is a bit tricky as we need to manually convert backspace, tab, and enter.
Sending the keys is a bit tricky as we need to manually convert backspace, tab, enter and modifier keys.
``` go
--- send keys to system
// regex if string has characters we need to convert to key presses
characterRegex, _ := regexp.Compile(`[^\x08]\x08|\t|\n`)
doXDoTool := func(command string, keys string)(err error) {
cmd := exec.Command("xdotool", command, keys)
var cmd *exec.Cmd
if command == "type" {
cmd = exec.Command("xdotool", command, "--delay", "25", keys)
} else {
cmd = exec.Command("xdotool", command, keys)
}
return cmd.Run()
}
@ -94,15 +101,18 @@ if strings.HasPrefix(message_string, "{kb_cmd:xdotool}:") {
doXDoTool("type", charString)
continue
}
// key is required for special characters
err = doXDoTool("key", charString)
}
continue
}
continue
} else {
doXDoTool("type", message_string)
}
continue
}
if characterRegex.MatchString(message_string) {
for _, character := range message_string {
charString := string(character)
if charString == "\n" {
@ -121,6 +131,13 @@ for _, character := range message_string {
if err != nil {
log.Println("type:", err)
}
continue
}
continue
}
err = keyboard.Type(message_string)
if err != nil {
log.Println("type:", err)
}
---
```