Commit cd3e907a authored by qwelp's avatar qwelp

add escape sequences for special characters

parent 75d97c36
Package for Golang. Used to convert string to keyboard input.
Working on Windows.
Example of usage in example folder.
# string2keyboard
Emulate keyboard input from string
## Overview
Package is used to emulate keyboard input.
It is generates keystrokes on the keyboard and sends them to the active text input field.
So cursor should be in some text input field before calling *KeyboardWrite* method.
Escape sequences are available for some special characters ('Tab', 'Enter' etc.)
## Install
```golang
go get github.com/taglme/string2keyboard
```
## Import
```golang
import (
"github.com/taglme/string2keyboard"
)
```
## Usage
```golang
message := "Hello world!"
err := string2keyboard.KeyboardWrite(message)
```
## Special characters
It is possible to pass some special characters using escape sequence. Use backslash character to start escape sequence.
List of available special characters:
* \n - line feed or newline
* \b - backspace
* \t - horizontal tab
* \\\ - backslash
* \\" - double quote
## Basic example
Check usage in example folder.
## Special characters example
```golang
//Newline example
message := "123\n456"
err := string2keyboard.KeyboardWrite(message)
//Output
123
456
//Tab example
message := "123\t456"
err := string2keyboard.KeyboardWrite(message)
//Output
123 456
//Backslash example
message := "123\\456"
err := string2keyboard.KeyboardWrite(message)
//Output
123\456
```
\ No newline at end of file
package string2keyboard
import "github.com/micmonay/keybd_event"
import (
"github.com/micmonay/keybd_event"
)
type keySet struct {
code int
......@@ -112,19 +114,59 @@ var (
}
)
// Convert string to keyboard input in Windows
//KeyboardWrite emulate keyboard input from string
func KeyboardWrite(textInput string) error {
kb, err := keybd_event.NewKeyBonding()
if err != nil {
return err
}
for _, c := range textInput {
kb.SetKeys(names[string(c)].code)
kb.HasSHIFT(names[string(c)].shift)
err = kb.Launching()
if err != nil {
return err
//Should we skip next character in string
//Used if we found some escape sequence
skip := false
for i, c := range textInput {
if !skip {
if c != '\\' {
kb.SetKeys(names[string(c)].code)
kb.HasSHIFT(names[string(c)].shift)
} else {
//Found backslash escape character
//Check next character
switch textInput[i+1] {
case 'n':
//Found newline character sequence
kb.SetKeys(keybd_event.VK_ENTER)
skip = true
case '\\':
//Found backslash character sequence
kb.SetKeys(keybd_event.VK_OEM_5)
skip = true
case 'b':
//Found backspace character sequence
kb.SetKeys(keybd_event.VK_BACK)
skip = true
case 't':
//Found tab character sequence
kb.SetKeys(keybd_event.VK_TAB)
skip = true
case '"':
//Found double quote character sequence
kb.SetKeys(keybd_event.VK_OEM_7)
kb.HasSHIFT(true)
skip = true
default:
//Nothing special, jsut backslash output
kb.SetKeys(keybd_event.VK_OEM_5)
}
}
err = kb.Launching()
if err != nil {
return err
}
} else {
skip = false
}
}
return nil
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment