keyboard

package
v0.3.2 Latest Latest
Warning

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

Go to latest
Published: Dec 30, 2025 License: GPL-3.0 Imports: 11 Imported by: 0

Documentation

Overview

Package keyboard provides a HID keyboard device implementation with full N-key rollover.

Index

Constants

View Source
const (
	ModLeftCtrl   = 0x01
	ModLeftShift  = 0x02
	ModLeftAlt    = 0x04
	ModLeftGUI    = 0x08 // Windows/Command key
	ModRightCtrl  = 0x10
	ModRightShift = 0x20
	ModRightAlt   = 0x40
	ModRightGUI   = 0x80
)

Modifier key bitmasks

View Source
const (
	LEDNumLock    = 0x01
	LEDCapsLock   = 0x02
	LEDScrollLock = 0x04
	LEDCompose    = 0x08
	LEDKana       = 0x10
)

LED bitmasks

View Source
const (
	// Letters A-Z
	KeyA = 0x04
	KeyB = 0x05
	KeyC = 0x06
	KeyD = 0x07
	KeyE = 0x08
	KeyF = 0x09
	KeyG = 0x0A
	KeyH = 0x0B
	KeyI = 0x0C
	KeyJ = 0x0D
	KeyK = 0x0E
	KeyL = 0x0F
	KeyM = 0x10
	KeyN = 0x11
	KeyO = 0x12
	KeyP = 0x13
	KeyQ = 0x14
	KeyR = 0x15
	KeyS = 0x16
	KeyT = 0x17
	KeyU = 0x18
	KeyV = 0x19
	KeyW = 0x1A
	KeyX = 0x1B
	KeyY = 0x1C
	KeyZ = 0x1D

	// Numbers 1-0 (top row)
	Key1 = 0x1E
	Key2 = 0x1F
	Key3 = 0x20
	Key4 = 0x21
	Key5 = 0x22
	Key6 = 0x23
	Key7 = 0x24
	Key8 = 0x25
	Key9 = 0x26
	Key0 = 0x27

	// Special keys
	KeyEnter      = 0x28
	KeyEscape     = 0x29
	KeyBackspace  = 0x2A
	KeyTab        = 0x2B
	KeySpace      = 0x2C
	KeyMinus      = 0x2D // - and _
	KeyEqual      = 0x2E // = and +
	KeyLeftBrace  = 0x2F // [ and {
	KeyRightBrace = 0x30 // ] and }
	KeyBackslash  = 0x31 // \ and |
	KeyNonUSHash  = 0x32 // Non-US # and ~
	KeySemicolon  = 0x33 // ; and :
	KeyApostrophe = 0x34 // ' and "
	KeyGrave      = 0x35 // ` and ~
	KeyComma      = 0x36 // , and <
	KeyPeriod     = 0x37 // . and >
	KeySlash      = 0x38 // / and ?
	KeyCapsLock   = 0x39

	// Function keys
	KeyF1  = 0x3A
	KeyF2  = 0x3B
	KeyF3  = 0x3C
	KeyF4  = 0x3D
	KeyF5  = 0x3E
	KeyF6  = 0x3F
	KeyF7  = 0x40
	KeyF8  = 0x41
	KeyF9  = 0x42
	KeyF10 = 0x43
	KeyF11 = 0x44
	KeyF12 = 0x45

	// Control keys
	KeyPrintScreen = 0x46
	KeyScrollLock  = 0x47
	KeyPause       = 0x48
	KeyInsert      = 0x49
	KeyHome        = 0x4A
	KeyPageUp      = 0x4B
	KeyDelete      = 0x4C
	KeyEnd         = 0x4D
	KeyPageDown    = 0x4E

	// Arrow keys
	KeyRight = 0x4F
	KeyLeft  = 0x50
	KeyDown  = 0x51
	KeyUp    = 0x52

	// Numpad
	KeyNumLock    = 0x53
	KeyKpSlash    = 0x54 // Keypad /
	KeyKpAsterisk = 0x55 // Keypad *
	KeyKpMinus    = 0x56 // Keypad -
	KeyKpPlus     = 0x57 // Keypad +
	KeyKpEnter    = 0x58 // Keypad Enter
	KeyKp1        = 0x59 // Keypad 1 and End
	KeyKp2        = 0x5A // Keypad 2 and Down
	KeyKp3        = 0x5B // Keypad 3 and PageDn
	KeyKp4        = 0x5C // Keypad 4 and Left
	KeyKp5        = 0x5D // Keypad 5
	KeyKp6        = 0x5E // Keypad 6 and Right
	KeyKp7        = 0x5F // Keypad 7 and Home
	KeyKp8        = 0x60 // Keypad 8 and Up
	KeyKp9        = 0x61 // Keypad 9 and PageUp
	KeyKp0        = 0x62 // Keypad 0 and Insert
	KeyKpDot      = 0x63 // Keypad . and Delete

	// Additional keys
	KeyNonUSBackslash = 0x64 // Non-US \ and |
	KeyApplication    = 0x65 // Application (Windows Menu key)
	KeyPower          = 0x66 // Power (not commonly used)
	KeyKpEqual        = 0x67 // Keypad =

	// Extended function keys
	KeyF13 = 0x68
	KeyF14 = 0x69
	KeyF15 = 0x6A
	KeyF16 = 0x6B
	KeyF17 = 0x6C
	KeyF18 = 0x6D
	KeyF19 = 0x6E
	KeyF20 = 0x6F
	KeyF21 = 0x70
	KeyF22 = 0x71
	KeyF23 = 0x72
	KeyF24 = 0x73

	// Execution keys
	KeyExecute    = 0x74
	KeyHelp       = 0x75
	KeyMenu       = 0x76
	KeySelect     = 0x77
	KeyStop       = 0x78
	KeyAgain      = 0x79 // Redo
	KeyUndo       = 0x7A
	KeyCut        = 0x7B
	KeyCopy       = 0x7C
	KeyPaste      = 0x7D
	KeyFind       = 0x7E
	KeyMute       = 0x7F
	KeyVolumeUp   = 0x80
	KeyVolumeDown = 0x81

	// Media control keys
	KeyMediaPlayPause = 0xE8 // Play/Pause
	KeyMediaStop      = 0xE9 // Stop
	KeyMediaNext      = 0xEB // Next Track
	KeyMediaPrevious  = 0xEC // Previous Track
)

HID Usage codes for keyboard keys (USB HID Keyboard/Keypad usage page)

Variables

View Source
var CharToKey = map[byte]uint8{

	'a': KeyA, 'b': KeyB, 'c': KeyC, 'd': KeyD, 'e': KeyE, 'f': KeyF, 'g': KeyG,
	'h': KeyH, 'i': KeyI, 'j': KeyJ, 'k': KeyK, 'l': KeyL, 'm': KeyM, 'n': KeyN,
	'o': KeyO, 'p': KeyP, 'q': KeyQ, 'r': KeyR, 's': KeyS, 't': KeyT, 'u': KeyU,
	'v': KeyV, 'w': KeyW, 'x': KeyX, 'y': KeyY, 'z': KeyZ,

	'A': KeyA, 'B': KeyB, 'C': KeyC, 'D': KeyD, 'E': KeyE, 'F': KeyF, 'G': KeyG,
	'H': KeyH, 'I': KeyI, 'J': KeyJ, 'K': KeyK, 'L': KeyL, 'M': KeyM, 'N': KeyN,
	'O': KeyO, 'P': KeyP, 'Q': KeyQ, 'R': KeyR, 'S': KeyS, 'T': KeyT, 'U': KeyU,
	'V': KeyV, 'W': KeyW, 'X': KeyX, 'Y': KeyY, 'Z': KeyZ,

	'1': Key1, '2': Key2, '3': Key3, '4': Key4, '5': Key5,
	'6': Key6, '7': Key7, '8': Key8, '9': Key9, '0': Key0,

	'!': Key1, '@': Key2, '#': Key3, '$': Key4, '%': Key5,
	'^': Key6, '&': Key7, '*': Key8, '(': Key9, ')': Key0,

	'-':  KeyMinus,
	'=':  KeyEqual,
	'[':  KeyLeftBrace,
	']':  KeyRightBrace,
	'\\': KeyBackslash,
	';':  KeySemicolon,
	'\'': KeyApostrophe,
	'`':  KeyGrave,
	',':  KeyComma,
	'.':  KeyPeriod,
	'/':  KeySlash,

	'_': KeyMinus,
	'+': KeyEqual,
	'{': KeyLeftBrace,
	'}': KeyRightBrace,
	'|': KeyBackslash,
	':': KeySemicolon,
	'"': KeyApostrophe,
	'~': KeyGrave,
	'<': KeyComma,
	'>': KeyPeriod,
	'?': KeySlash,

	' ':  KeySpace,
	'\n': KeyEnter,
	'\r': KeyEnter,
	'\t': KeyTab,
}

CharToKey maps ASCII characters to their corresponding HID usage codes. For shifted characters (uppercase, symbols), use with NeedsShift().

View Source
var KeyName = map[uint8]string{}/* 115 elements not displayed */

KeyName maps HID usage codes to human-readable key names.

View Source
var ShiftChars = map[byte]bool{

	'A': true, 'B': true, 'C': true, 'D': true, 'E': true, 'F': true, 'G': true,
	'H': true, 'I': true, 'J': true, 'K': true, 'L': true, 'M': true, 'N': true,
	'O': true, 'P': true, 'Q': true, 'R': true, 'S': true, 'T': true, 'U': true,
	'V': true, 'W': true, 'X': true, 'Y': true, 'Z': true,

	'!': true, '@': true, '#': true, '$': true, '%': true,
	'^': true, '&': true, '*': true, '(': true, ')': true,

	'_': true, '+': true, '{': true, '}': true, '|': true,
	':': true, '"': true, '~': true, '<': true, '>': true, '?': true,
}

ShiftChars defines which characters require the Shift modifier.

Functions

func CharToHID

func CharToHID(c byte) uint8

CharToHID converts an ASCII character to its HID usage code. Returns 0 if the character is not supported.

func NeedsShift

func NeedsShift(c byte) bool

NeedsShift returns true if the character requires the Shift modifier.

Types

type InputState

type InputState struct {
	Modifiers uint8     // bit 0-7: LCtrl, LShift, LAlt, LGui, RCtrl, RShift, RAlt, RGui
	KeyBitmap [32]uint8 // 256 bits for HID usage codes 0x00-0xFF
}

InputState represents the keyboard state used to build a report. Internally uses a 256-bit bitmap for N-key rollover support. viiper:wire keyboard c2s modifiers:u8 count:u8 keys:u8*count

func PressKey

func PressKey(keys ...uint8) InputState

PressKey creates an InputState with the specified keys pressed. No modifiers are set.

Example:

state := PressKey(KeyA, KeyB) // Press A and B simultaneously

func PressKeyWithMod

func PressKeyWithMod(modifiers uint8, keys ...uint8) InputState

PressKeyWithMod creates an InputState with modifiers and keys pressed.

Example:

state := PressKeyWithMod(ModLeftCtrl, KeyC) // Ctrl+C
state := PressKeyWithMod(ModLeftShift, KeyA) // Shift+A

func Release

func Release() InputState

Release creates an empty InputState with all keys released.

func TypeChar

func TypeChar(c byte) (press, release InputState)

TypeChar converts a single character to a press/release InputState pair. Automatically adds Shift modifier if needed.

func TypeString

func TypeString(s string) []InputState

TypeString converts a string into a sequence of InputState press/release pairs. Automatically handles shift modifiers for uppercase letters and symbols. Returns a slice of states alternating between press and release.

Example:

states := TypeString("Hi!")
// Returns: [Press Shift+H, Release, Press i, Release, Press Shift+1, Release]

func (*InputState) BuildReport

func (kb *InputState) BuildReport() []byte

BuildReport encodes an InputState into the 34-byte HID keyboard report.

Report layout (34 bytes):

Byte 0: Modifiers (8 bits)
Byte 1: Reserved (0x00)
Bytes 2-33: Key bitmap (256 bits, 32 bytes)

func (*InputState) MarshalBinary

func (kb *InputState) MarshalBinary() ([]byte, error)

MarshalBinary encodes InputState to variable-length wire format.

Wire format:

Byte 0: Modifiers
Byte 1: Key count
Bytes 2+: Key codes (HID usage codes of pressed keys)

func (*InputState) UnmarshalBinary

func (kb *InputState) UnmarshalBinary(data []byte) error

UnmarshalBinary decodes variable-length wire format into InputState.

Wire format:

Byte 0: Modifiers
Byte 1: Key count
Bytes 2+: Key codes (HID usage codes of pressed keys)

type Keyboard

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

Keyboard implements the Device interface for a full HID keyboard with LED support.

func New

func New(o *device.CreateOptions) *Keyboard

New returns a new Keyboard device.

func (*Keyboard) GetDescriptor

func (k *Keyboard) GetDescriptor() *usb.Descriptor

func (*Keyboard) GetLEDState

func (k *Keyboard) GetLEDState() LEDState

GetLEDState returns the current LED state from the host.

func (*Keyboard) HandleTransfer

func (k *Keyboard) HandleTransfer(ep uint32, dir uint32, out []byte) []byte

HandleTransfer implements interrupt IN/OUT for Keyboard.

func (*Keyboard) SetLEDCallback

func (k *Keyboard) SetLEDCallback(f func(LEDState))

SetLEDCallback sets a callback that will be invoked when LED state changes.

func (*Keyboard) UpdateInputState

func (k *Keyboard) UpdateInputState(state InputState)

UpdateInputState updates the device's current input state (thread-safe).

type LEDState

type LEDState struct {
	NumLock    bool
	CapsLock   bool
	ScrollLock bool
	Compose    bool
	Kana       bool
}

LEDState represents the state of keyboard LEDs controlled by the host. viiper:wire keyboard s2c leds:u8

func (*LEDState) UnmarshalBinary

func (ls *LEDState) UnmarshalBinary(data []byte) error

UnmarshalBinary decodes a 1-byte LED bitmask into LEDState. Bits are defined by LEDNumLock, LEDCapsLock, LEDScrollLock, LEDCompose, LEDKana.

Jump to

Keyboard shortcuts

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