gostwriter

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

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

Go to latest
Published: Mar 14, 2023 License: GPL-3.0 Imports: 2 Imported by: 1

README

Travis GoDoc

What is it?

It's a simple virtual keyboard for Go that uses /dev/uinput to inject key events, as if from a real keyboard.

Why?

To programatically emulate user keystrokes, i.e. an emulated gamepad or other device that can map to keyboard inputs.

Limitations

It currently only does keyboard events, and only the ones I needed so far. Can be extended in the scope of uinput, i.e. mouse events, but I didn't do that yet. Feel free to fork!

Installation

Do the usual go get:

$> go get github.com/galaktor/gostwriter

Key codes

Since gostwriter uses uinput, you might have to regenerate the key codes it uses to match the kernel you are running on. The key codes are defined as constants in "key/codes.go", and are basically just integers which are sent to the kernel to tell it what key was pressed.

As keys are added and moved around between kernels, you might want to either add or remove keys to the codes.go file to suit your purposes. gostwriter comes with a handy script to auto-generate a complete set of keycodes from any given "linux/input.h" kernel header file.

In the simplest case, you will get gostwriter and it will just build and work. If the keycodes it comes with don't match your kernel, you will get something like this:

# github.com/galaktor/gostwriter/input
38: error: 'KEY_BRIGHTNESS_AUTO' undeclared (first use in this function)
38: error: 'KEY_JOURNAL' undeclared (first use in this function)
38: error: 'KEY_VOICECOMMAND' undeclared (first use in this function)
38: error: 'KEY_BRIGHTNESS_TOGGLE' undeclared (first use in this function)
39: error: 'KEY_CONTROLPANEL' undeclared (first use in this function)
39: error: 'KEY_APPSELECT' undeclared (first use in this function)
39: error: 'KEY_BRIGHTNESS_MIN' undeclared (first use in this function)
39: error: 'KEY_BRIGHTNESS_MAX' undeclared (first use in this function)
39: error: 'KEY_TASKMANAGER' undeclared (first use in this function)
39: error: 'KEY_SCREENSAVER' undeclared (first use in this function)
39: error: 'KEY_BUTTONCONFIG' undeclared (first use in this function)

You can either remove the offending codes from codes.go by hand, or run the provided script. It is located at "key/get_keycodes.sh" and by default will try to get your kernel's header file from "/usr/include/linux/input.h". It accepts an argument to point it to another "input.h" file (if say you want to cross-compile to another header version).

I recommend to do this for a simple and quick way to have good key codes, which will suit most cases I suppose:

$gostwriter>       cd key
$gostwriter/key>   ./get_keycodes.sh
$gostwriter/key>   cd ..
$gostwriter>       go build
$gostwriter/build> go install github.com/galaktor/gostwriter

Documentation

The source code and it's comments should be more than enough to get what's going on within.

godoc

It renders reasonably well in GoDoc:

http://godoc.org/github.com/galaktor/gostwriter

examples

I put a little demo together, it's very straight-forward. Find it at "demo/demo.go".

Licensed under the GPL v3. See LICENSE and COPYRIGHT files.

Documentation

Overview

Copyright 2014, Raphael Estrada Author email: <galaktor@gmx.de> Project home: <https://github.com/galaktor/gostwriter> Licensed under The GPL v3 License (see README and LICENSE files)

Copyright 2014, Raphael Estrada Author email: <galaktor@gmx.de> Project home: <https://github.com/galaktor/gostwriter> Licensed under The GPL v3 License (see README and LICENSE files)

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type K

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

represents a key on the virtual keyboard

func (*K) Code

func (k *K) Code() key.Code

getter which returns the keycode associated with the key

func (*K) IsPressed

func (k *K) IsPressed() bool

returns whether or not the key is currently pressed

func (*K) Press

func (k *K) Press() (err error)

changes the key state into PRESSED if it isn't already. you can repeatedly call this but the state will switch at most once .

func (*K) Push

func (k *K) Push() error

presses the key then subsequently releases it. for convenience when typical 'typing'-style key presses are desired. for key combinations, you probably should be using Press() and Release() directly.

func (*K) Release

func (k *K) Release() (err error)

changes the key state into NOT_PRESSED if it isn't already you can repeatedly call this but the state will switch at most once.

func (*K) State

func (k *K) State() State

getter which returns the state that the key is in

func (*K) Toggle

func (k *K) Toggle() (result State, err error)

switches the key from it's current state into the other. not sure what this would be good for, but as they say:

"we do what we must, because we can."
                -aperture science laboratories

type Keyboard

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

represents the virtual keyboard. holds a reference to a device on /dev/uinput where it's keys will send events to.

func New

func New(name string) (*Keyboard, error)

constructs and returns a new virtual keyboard. the name provided is used to name the underlying uinput device in the operating system. registers *all* available key codes with uinput for simplicity. only actually creates virtual keys on first request to save some memory.

func (*Keyboard) Destroy

func (kb *Keyboard) Destroy() error

destroys an existing virtual keyboard, i.e. it unregisters the uinput device on the kernel. the keyboard will not work after this function was called. make sure to call it before your application exits, e.g. using 'defer'

func (*Keyboard) Get

func (kb *Keyboard) Get(c key.Code) (*K, error)

gets a key on the keyboard. since the constructor will register all known key codes, you can use them all here. once a key is created the first time, it will be re-used in subsequent Get() calls.

type State

type State bool

represents the state a virtual key can be in. Bool because there are only two states.

const (
	NOT_PRESSED State = State(false) // the key is not pressed, aka released
	PRESSED           = State(true)  // the key is pressed, and not (yet) released
)

Directories

Path Synopsis
Copyright 2014, Raphael Estrada Author email: <galaktor@gmx.de> Project home: <https://github.com/galaktor/gostwriter> Licensed under The GPL v3 License (see README and LICENSE files)
Copyright 2014, Raphael Estrada Author email: <galaktor@gmx.de> Project home: <https://github.com/galaktor/gostwriter> Licensed under The GPL v3 License (see README and LICENSE files)
Copyright 2014, Raphael Estrada Author email: <galaktor@gmx.de> Project home: <https://github.com/galaktor/gostwriter> Licensed under The GPL v3 License (see README and LICENSE files)
Copyright 2014, Raphael Estrada Author email: <galaktor@gmx.de> Project home: <https://github.com/galaktor/gostwriter> Licensed under The GPL v3 License (see README and LICENSE files)
Copyright 2014, Raphael Estrada Author email: <galaktor@gmx.de> Project home: <https://github.com/galaktor/gostwriter> Licensed under The GPL v3 License (see README and LICENSE files)
Copyright 2014, Raphael Estrada Author email: <galaktor@gmx.de> Project home: <https://github.com/galaktor/gostwriter> Licensed under The GPL v3 License (see README and LICENSE files)

Jump to

Keyboard shortcuts

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