pikey

package
v0.35.0 Latest Latest
Warning

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

Go to latest
Published: Jul 25, 2025 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package pikey provides functionality for a virtual keyboard.

It has the following layout:

Esc   F1 F2 F3 F4 F5 F6 F7 F8 F9 F10 F11 F12
`      1  2  3  4  5  6  7  8  9  0  -  =  Backspace
Tab     Q  W  E  R  T  Y  U  I  O  P  [  ]  \
CapsLock  A  S  D  F  G  H  J  K  L  ;  '  Enter
ShiftLeft  Z  X  C  V  B  N  M   ,  .  /    ShiftRight          Up
CtrlLeft AltLeft                    AltRight  CtrlRight   Left Down Right

The pikey package supports up to 119 keys but defines only 75 public constants. This is intentional, encouraging you to design your game to use no more than 75 buttons. Why? Because this ensures compatibility with nearly any modern keyboard. Of course, you can allow users to map additional keys if they want — for example, by capturing the key name from an Event and saving it in a settings file. Just remember that some keyboards have even more than 119 keys, but Pi does not support them.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DebugTarget

func DebugTarget() pievent.Target[Event]

events are published all the time - even when game is paused.

func Duration

func Duration(k Key) int

Duration returns the number of frames the key k has been held down.

func Target

func Target() pievent.Target[Event]

Types

type Event

type Event struct {
	Type EventType
	Key  Key
}

Event is published when the player presses or releases a key.

It may be published more than once during a single game tick.

type EventType

type EventType string
const (
	EventUp   EventType = "up"
	EventDown EventType = "down"
)

type Key

type Key string
const (
	A            Key = "A"
	B            Key = "B"
	C            Key = "C"
	D            Key = "D"
	E            Key = "E"
	F            Key = "F"
	G            Key = "G"
	H            Key = "H"
	I            Key = "I"
	J            Key = "J"
	K            Key = "K"
	L            Key = "L"
	M            Key = "M"
	N            Key = "N"
	O            Key = "O"
	P            Key = "P"
	Q            Key = "Q"
	R            Key = "R"
	S            Key = "S"
	T            Key = "T"
	U            Key = "U"
	V            Key = "V"
	W            Key = "W"
	X            Key = "X"
	Y            Key = "Y"
	Z            Key = "Z"
	AltLeft      Key = "AltLeft"
	AltRight     Key = "AltRight"
	Down         Key = "Down"
	Left         Key = "Left"
	Right        Key = "Right"
	Up           Key = "Up"
	Backquote    Key = "`"
	Backslash    Key = `\`
	Backspace    Key = "Backspace"
	BracketLeft  Key = "BracketLeft"
	BracketRight Key = "BracketRight"
	CapsLock     Key = "CapsLock"
	Comma        Key = ","
	CtrlLeft     Key = "CtrlLeft"
	CtrlRight    Key = "CtrlRight"
	Digit0       Key = "0"
	Digit1       Key = "1"
	Digit2       Key = "2"
	Digit3       Key = "3"
	Digit4       Key = "4"
	Digit5       Key = "5"
	Digit6       Key = "6"
	Digit7       Key = "7"
	Digit8       Key = "8"
	Digit9       Key = "9"
	Enter        Key = "Enter"
	Equal        Key = "="
	Esc          Key = "Esc"
	F1           Key = "F1"
	F2           Key = "F2"
	F3           Key = "F3"
	F4           Key = "F4"
	F5           Key = "F5"
	F6           Key = "F6"
	F7           Key = "F7"
	F8           Key = "F8"
	F9           Key = "F9"
	F10          Key = "F10"
	F11          Key = "F11"
	F12          Key = "F12"
	Minus        Key = "Minus"
	Period       Key = "."
	Quote        Key = `'`
	Semicolon    Key = ";"
	ShiftLeft    Key = "ShiftLeft"
	ShiftRight   Key = "ShiftRight"
	Slash        Key = "/"
	Space        Key = " "
	Tab          Key = "Tab"
	Alt          Key = "Alt"     // alt left or right
	Control      Key = "Control" // control left or right
	Shift        Key = "Shift"   // shift left or right
)

type Shortcut

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

func RegisterShortcut

func RegisterShortcut(onEvent func(), keys ...Key) *Shortcut

RegisterShortcut registers a new keyboard shortcut.

The function takes a callback onEvent, which will be called every time the user presses all the keys specified in `keys` simultaneously. The order of keys in the `keys` argument does not matter.

It returns a *Shortcut object, which can later be used to unregister the shortcut.

Example:

RegisterShortcut(func() {
	log.Println("Pressed Ctrl + S")
}, KeyCtrl, KeyS)

Notes:

  • If the same key combination is registered multiple times, all corresponding callbacks will be invoked.

func (*Shortcut) Unregister

func (s *Shortcut) Unregister()

Jump to

Keyboard shortcuts

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