sendkeys

package module
v0.0.0-...-edff02a Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 3, 2023 License: MIT Imports: 8 Imported by: 0

README

sendkeys

GoDoc Go Report Card

Summary

sendkeys is a cross-platform usability wrapper for the keybd_event Go library. It aims to to provide a faster, more reliable, and easier to use way for humans to access the functionality of keybd_event.

Use this library to turn full strings into simulated keyboard events with ease. This library was created after a noVNC instance I was using had a broken clipboard feature.

I have successfully used the example to send a very long password into a NoVNC instance that had all kinds of varying case alphanumeric characters along with many symbols.

Features

  • Optionally randomized delays between keypresses.

  • Optimized map lookups should provide very high performance.

  • Negative integer -> abs inversion to determine when to send the shift key event.

  • Only send one key at a time, and clear the state inbetween keys for reliable functionality.

Documentation

For simple usage, take a look at the example.
GoDoc
type KBOpt
type KBOpt uint8

KBOpt[s] are options for our wrapper

const (
	// Stubborn will cause our sequences to continue despite errors.
	// Otherwise, we will stop if our error count is over 0.
	Stubborn KBOpt = iota
	// Noisy will cause all errors to be printed to stdout.
	Noisy
	// Random will use random sleeps throughout the typing process.
	// Otherwise, a static 10 milliseconds will be used.
	Random
	// NoDelay will bypass the 2 second delay for linux, mostly for testing.
	NoDelay
)
type KBWrap
type KBWrap struct {
	// There are unexported fields
}

KBWrap is a wrapper for the keybd_event library for convenience

func NewKBWrapWithOptions
func NewKBWrapWithOptions(opts ...KBOpt) (kbw *KBWrap, err error)

NewKBWrapWithOptions creates a new keyboard wrapper with the given options. As of writing, those options include: Stubborn Noisy and Random. The defaults are all false.

func (*KBWrap) BackSpace
func (kb *KBWrap) BackSpace()

BackSpace presses the backspace key. All other keys will be cleared.

func (*KBWrap) Enter
func (kb *KBWrap) Enter()

Enter presses the enter key. All other keys will be cleared.

func (*KBWrap) Escape
func (kb *KBWrap) Escape()

Escape presses the escape key. All other keys will be cleared.

func (*KBWrap) Tab
func (kb *KBWrap) Tab()

Tab presses the tab key. All other keys will be cleared.

func (*KBWrap) Type
func (kb *KBWrap) Type(s string) error

Type types out a string by simulating keystrokes. Check the exported Symbol map for non-alphanumeric keys.

Status

sendkeys is in early development. tests pass on a real machine, but I'm done trying to make github actions work for this one.

Local Test Results
=== RUN   Test_strToKeys
    sendkeys_test.go:51: string: yeet, keys: []int{21, 18, 18, 20}
    sendkeys_test.go:51: string: YEET, keys: []int{-21, -18, -18, -20}
    sendkeys_test.go:51: string: YeeT, keys: []int{-21, 18, 18, -20}
--- PASS: Test_strToKeys (0.00s)
=== RUN   Test_NewKBWrapWithOptions
    sendkeys_test.go:72: [OPT] Noisy: true NoDelay: true Stubborn: true Random: true
    sendkeys_test.go:86: [OPT] Noisy: false NoDelay: false Stubborn: false Random: false
--- PASS: Test_NewKBWrapWithOptions (2.00s)
=== RUN   Test_sendkeys
    sendkeys_test.go:27: Key pressed: y
    sendkeys_test.go:27: Key pressed: e
    sendkeys_test.go:27: Key pressed: e
    sendkeys_test.go:27: Key pressed: t
    sendkeys_test.go:171: got 4 characters: yeet
    sendkeys_test.go:27: Key pressed: Y
    sendkeys_test.go:27: Key pressed: e
    sendkeys_test.go:27: Key pressed: e
    sendkeys_test.go:27: Key pressed: T
    sendkeys_test.go:171: got 4 characters: YeeT
    sendkeys_test.go:27: Key pressed: Y
    sendkeys_test.go:27: Key pressed: e
    sendkeys_test.go:27: Key pressed: e
    sendkeys_test.go:27: Key pressed: t
    sendkeys_test.go:27: Key pressed: !
    sendkeys_test.go:171: got 5 characters: Yeet!
    sendkeys_test.go:27: Key pressed: \
    sendkeys_test.go:27: Key pressed: '
    sendkeys_test.go:27: Key pressed: `
    sendkeys_test.go:27: Key pressed: /
    sendkeys_test.go:27: Key pressed: 3
    sendkeys_test.go:27: Key pressed: 3
    sendkeys_test.go:27: Key pressed: 7
    sendkeys_test.go:27: Key pressed: !
    sendkeys_test.go:27: Key pressed: '
    sendkeys_test.go:27: Key pressed: \
    sendkeys_test.go:171: got 10 characters: \'`/337!'\
    sendkeys_test.go:27: Key pressed: W
    sendkeys_test.go:27: Key pressed: e
    sendkeys_test.go:27: Key pressed: l
    sendkeys_test.go:27: Key pressed: c
    sendkeys_test.go:27: Key pressed: o
    sendkeys_test.go:27: Key pressed: m
    sendkeys_test.go:27: Key pressed: e
    sendkeys_test.go:27: Key pressed:
    sendkeys_test.go:32: spacebar detected
    sendkeys_test.go:27: Key pressed: t
    sendkeys_test.go:27: Key pressed: o
    sendkeys_test.go:27: Key pressed:
    sendkeys_test.go:32: spacebar detected
    sendkeys_test.go:27: Key pressed: y
    sendkeys_test.go:27: Key pressed: e
    sendkeys_test.go:27: Key pressed: e
    sendkeys_test.go:27: Key pressed: t
    sendkeys_test.go:27: Key pressed:
    sendkeys_test.go:32: spacebar detected
    sendkeys_test.go:27: Key pressed: t
    sendkeys_test.go:27: Key pressed: o
    sendkeys_test.go:27: Key pressed: w
    sendkeys_test.go:27: Key pressed: n
    sendkeys_test.go:27: Key pressed: ,
    sendkeys_test.go:27: Key pressed:
    sendkeys_test.go:32: spacebar detected
    sendkeys_test.go:27: Key pressed: b
    sendkeys_test.go:27: Key pressed: u
    sendkeys_test.go:27: Key pressed: d
    sendkeys_test.go:27: Key pressed: d
    sendkeys_test.go:27: Key pressed: y
    sendkeys_test.go:27: Key pressed: !
    sendkeys_test.go:27: Key pressed:
    sendkeys_test.go:32: spacebar detected
    sendkeys_test.go:171: got 28 characters: Welcome to yeet town, buddy!
    sendkeys_test.go:27: Key pressed: `
    sendkeys_test.go:27: Key pressed: ~
    sendkeys_test.go:27: Key pressed: !
    sendkeys_test.go:27: Key pressed: @
    sendkeys_test.go:27: Key pressed: #
    sendkeys_test.go:27: Key pressed: $
    sendkeys_test.go:27: Key pressed: %
    sendkeys_test.go:27: Key pressed: ^
    sendkeys_test.go:27: Key pressed: &
    sendkeys_test.go:27: Key pressed: *
    sendkeys_test.go:27: Key pressed: (
    sendkeys_test.go:27: Key pressed: )
    sendkeys_test.go:27: Key pressed: -
    sendkeys_test.go:27: Key pressed: _
    sendkeys_test.go:27: Key pressed: =
    sendkeys_test.go:27: Key pressed: +
    sendkeys_test.go:27: Key pressed: '
    sendkeys_test.go:27: Key pressed: ;
    sendkeys_test.go:27: Key pressed: :
    sendkeys_test.go:27: Key pressed: <
    sendkeys_test.go:27: Key pressed: >
    sendkeys_test.go:27: Key pressed: /
    sendkeys_test.go:27: Key pressed: \
    sendkeys_test.go:27: Key pressed: ,
    sendkeys_test.go:27: Key pressed: .
    sendkeys_test.go:27: Key pressed: |
    sendkeys_test.go:27: Key pressed: {
    sendkeys_test.go:27: Key pressed: }
    sendkeys_test.go:27: Key pressed: [
    sendkeys_test.go:27: Key pressed: ]
    sendkeys_test.go:27: Key pressed: `
    sendkeys_test.go:27: Key pressed: ~
    sendkeys_test.go:27: Key pressed: ,
    sendkeys_test.go:27: Key pressed:
    sendkeys_test.go:32: spacebar detected
    sendkeys_test.go:27: Key pressed: y
    sendkeys_test.go:27: Key pressed: o
    sendkeys_test.go:27: Key pressed: u
    sendkeys_test.go:27: Key pressed:
    sendkeys_test.go:32: spacebar detected
    sendkeys_test.go:27: Key pressed: f
    sendkeys_test.go:27: Key pressed: e
    sendkeys_test.go:27: Key pressed: e
    sendkeys_test.go:27: Key pressed: l
    sendkeys_test.go:27: Key pressed:
    sendkeys_test.go:32: spacebar detected
    sendkeys_test.go:27: Key pressed: m
    sendkeys_test.go:27: Key pressed: e
    sendkeys_test.go:27: Key pressed:
    sendkeys_test.go:32: spacebar detected
    sendkeys_test.go:27: Key pressed: d
    sendkeys_test.go:27: Key pressed: a
    sendkeys_test.go:27: Key pressed: w
    sendkeys_test.go:27: Key pressed: g
    sendkeys_test.go:27: Key pressed: ?
    sendkeys_test.go:27: Key pressed:
    sendkeys_test.go:32: spacebar detected
    sendkeys_test.go:171: got 52 characters: `~!@#$%^&*()-_=+';:<>/\,.|{}[]`~, you feel me dawg?
--- PASS: Test_sendkeys (6.09s)
PASS
ok  	git.tcp.direct/kayos/sendkeys	8.139s

Compatibility

sendkeys has only been tested in Linux so far

the underlying library seemingly has support for all Go platforms. This should be cross platform.

Recently briefly tested in windows, I'm not sure that the shift key trigger is working or not. Needs to be tested further.

Credits

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrKeyMappingNotFound = errors.New("failed to map key: ")

ErrKeyMappingNotFound is an error returned when we don't know how to handle the given character.

View Source
var Symbol = map[string]int{
	"-": 12, "_": -12, "=": 13, "+": -13, "[": 26,
	"{": -26, "]": 27, "}": -27, "'": 40, "\"": -40,
	"`": 41, "~": -41, "\\": 43, "|": -43, ",": 51,
	"<": -51, ".": 52, ">": -52, "/": 53, "?": -53,
	" ": 57, "!": -2, "@": -3, "#": -4, "$": -5,
	"%": -6, "^": -7, "&": -8, "*": -9, "(": -10,
	")": -11, ";": 39, ":": -39,
}

Symbol is a map of translations for sending non-alphanumeric key events.

Functions

This section is empty.

Types

type KBOpt

type KBOpt uint8

KBOpt are options for our wrapper

const (
	// Stubborn will cause our sequences to continue despite errors.
	// Otherwise, we will stop if our error count is over 0.
	Stubborn KBOpt = iota
	// Noisy will cause all errors to be printed to stdout.
	Noisy
	// Random will use random sleeps throughout the typing process.
	// Otherwise, a static 10 milliseconds will be used.
	Random
	// NoDelay will bypass the 2 second delay for linux, mostly for testing.
	NoDelay
)

type KBWrap

type KBWrap struct {
	// contains filtered or unexported fields
}

KBWrap is a wrapper for the keybd_event library for convenience

func NewKBWrapWithOptions

func NewKBWrapWithOptions(opts ...KBOpt) (kbw *KBWrap, err error)

NewKBWrapWithOptions creates a new keyboard wrapper with the given options. As of writing, those options include: Stubborn Noisy and Random. The defaults are all false.

func (*KBWrap) BackSpace

func (kb *KBWrap) BackSpace()

BackSpace presses the backspace key. All other keys will be cleared.

func (*KBWrap) Enter

func (kb *KBWrap) Enter()

Enter presses the enter key. All other keys will be cleared.

func (*KBWrap) Escape

func (kb *KBWrap) Escape()

Escape presses the escape key. All other keys will be cleared.

func (*KBWrap) KeyDown

func (kb *KBWrap) KeyDown(k string) error

func (*KBWrap) KeyUp

func (kb *KBWrap) KeyUp(k string) error

func (*KBWrap) Tab

func (kb *KBWrap) Tab()

Tab presses the tab key. All other keys will be cleared.

func (*KBWrap) Type

func (kb *KBWrap) Type(s string) error

Type types out a string by simulating keystrokes. Check the exported Symbol map for non-alphanumeric keys.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL