kbd

package module
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Oct 4, 2019 License: Unlicense Imports: 4 Imported by: 0

README

Package kbd is a simple package to allow one to test key state: press or not-pressed. It works only for Linux systems and requires that executables using it are started with sudo privledges. This is because keyboard events are read directly from the device file in /dev/input/. This also means that key events are read from the entire system, not just the terminal in which the executable was run.

Example (obviously no error handling):

   kb, _ := kbd.Open("/dev/input/event0")
   defer kb.Close()
   
   kb.Start()
   for key := range kb.Event() {
   		switch key {
   		case kbd.KeyA:
   			if kb.IsDown(key) {
  				fmt.Println("A down")
  			} else {
  				fmt.Println("A up")
   			}
   		case kbd.KeyESC:
  			if kb.IsDown(key) {
  				fmt.Println("ESC")
  				kb.Stop()
  			}
   		}
   }
   fmt.Println("Error:", kb.Err())

Documentation

Overview

Package kbd is a simple package to allow one to test key state: press or not-pressed. It works only for Linux systems and requires that executables using it are started with `sudo` privledges. This is because keyboard events are read directly from the device file in `/dev/input/`. This also means that key events are read from the *entire system*, not just the terminal in which the executable was run.

Example (obviously no error handling):

 kb, _ := kbd.Open("/dev/input/event0")
 defer kb.Close()

 kb.Start()
 for key := range kb.Event() {
 		switch key {
 		case kbd.KeyA:
 			if kb.IsDown(key) {
				fmt.Println("A down")
			} else {
				fmt.Println("A up")
 			}

 		case kbd.KeyESC:
			if kb.IsDown(key) {
				fmt.Println("ESC")
				kb.Stop()
			}
 		}
 }
 fmt.Println("Error:", kb.Err())

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type KeyCode

type KeyCode uint16

KeyCode is a code from "input-event-codes.h"

const (
	KeyRESERVED   KeyCode = 0
	KeyESC        KeyCode = 1
	Key1          KeyCode = 2
	Key2          KeyCode = 3
	Key3          KeyCode = 4
	Key4          KeyCode = 5
	Key5          KeyCode = 6
	Key6          KeyCode = 7
	Key7          KeyCode = 8
	Key8          KeyCode = 9
	Key9          KeyCode = 10
	Key0          KeyCode = 11
	KeyMINUS      KeyCode = 12
	KeyEQUAL      KeyCode = 13
	KeyBACKSPACE  KeyCode = 14
	KeyTAB        KeyCode = 15
	KeyQ          KeyCode = 16
	KeyW          KeyCode = 17
	KeyE          KeyCode = 18
	KeyR          KeyCode = 19
	KeyT          KeyCode = 20
	KeyY          KeyCode = 21
	KeyU          KeyCode = 22
	KeyI          KeyCode = 23
	KeyO          KeyCode = 24
	KeyP          KeyCode = 25
	KeyLEFTBRACE  KeyCode = 26
	KeyRIGHTBRACE KeyCode = 27
	KeyENTER      KeyCode = 28
	KeyLEFTCTRL   KeyCode = 29
	KeyA          KeyCode = 30
	KeyS          KeyCode = 31
	KeyD          KeyCode = 32
	KeyF          KeyCode = 33
	KeyG          KeyCode = 34
	KeyH          KeyCode = 35
	KeyJ          KeyCode = 36
	KeyK          KeyCode = 37
	KeyL          KeyCode = 38
	KeySEMICOLON  KeyCode = 39
	KeyAPOSTROPHE KeyCode = 40
	KeyGRAVE      KeyCode = 41
	KeyLEFTSHIFT  KeyCode = 42
	KeyBACKSLASH  KeyCode = 43
	KeyZ          KeyCode = 44
	KeyX          KeyCode = 45
	KeyC          KeyCode = 46
	KeyV          KeyCode = 47
	KeyB          KeyCode = 48
	KeyN          KeyCode = 49
	KeyM          KeyCode = 50
	KeyCOMMA      KeyCode = 51
	KeyDOT        KeyCode = 52
	KeySLASH      KeyCode = 53
	KeyRIGHTSHIFT KeyCode = 54
	KeyKPASTERISK KeyCode = 55
	KeyLEFTALT    KeyCode = 56
	KeySPACE      KeyCode = 57
	KeyCAPSLOCK   KeyCode = 58
	KeyF1         KeyCode = 59
	KeyF2         KeyCode = 60
	KeyF3         KeyCode = 61
	KeyF4         KeyCode = 62
	KeyF5         KeyCode = 63
	KeyF6         KeyCode = 64
	KeyF7         KeyCode = 65
	KeyF8         KeyCode = 66
	KeyF9         KeyCode = 67
	KeyF10        KeyCode = 68
	KeyNUMLOCK    KeyCode = 69
	KeySCROLLLOCK KeyCode = 70
)

KeyCodes for many keys.

type Keyboard

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

Keyboard allows access to key states.

func Open

func Open(path string) (*Keyboard, error)

Open will attempt to open the device at path as well as the terminal at `/dev/tty`. An error is returned if either of these fails.

func (*Keyboard) Close

func (kb *Keyboard) Close() error

Close calls Stop() and also closes files used by the Keyboard.

func (*Keyboard) Err

func (kb *Keyboard) Err() error

Err reads the error that ended the keyboard event reading loop.

func (*Keyboard) Event

func (kb *Keyboard) Event() <-chan KeyCode

Event returns a channel from which the most recently read KeyCode can be obtained.

func (*Keyboard) IsDown

func (kb *Keyboard) IsDown(key KeyCode) bool

IsDown checks if the key is pressed or held (aka repeat).

func (*Keyboard) Start

func (kb *Keyboard) Start() error

Start puts the terminal in "cbreak" mode (to prevent key echo) and kicks off a gofunc to read keyboard events. An error is returned if the terminal can't be put into cbreak mode. Errors affecting (ending) the keyboard event reading loop can be examined with Err().

func (*Keyboard) Stop

func (kb *Keyboard) Stop() error

Stop restores the terminal state and stops reading keyboard events.

Jump to

Keyboard shortcuts

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