keyboard

package
v0.12.0 Latest Latest
Warning

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

Go to latest
Published: Jan 24, 2022 License: Apache-2.0 Imports: 3 Imported by: 0

Documentation

Overview

Package keyboard adds support for keyboard input.

You can start using keyboard by creating Keyboard instance:

keys := keyboard.New(window)
for {
    keys.Update() // This is needed each frame to update the state of keys
    if keys.Pressed(keyboard.A) {
        ...
    }
})

Index

Constants

This section is empty.

Variables

View Source
var (
	// Space Key " "
	Space = newKey(" ")
	// Apostrophe Key "'"
	Apostrophe = newKey("'")
	// Comma Key ","
	Comma = newKey(",")
	// Minus Key "-"
	Minus = newKey("-")
	// Period Key "."
	Period = newKey(".")
	// Slash Key "/"
	Slash = newKey("/")
	// Zero Key "0"
	Zero = newKey("0")
	// One Key "1"
	One = newKey("1")
	// Two Key "2"
	Two = newKey("2")
	// Three Key "3"
	Three = newKey("3")
	// Four Key "4"
	Four = newKey("4")
	// Five Key "5"
	Five = newKey("5")
	// Six Key "6"
	Six = newKey("6")
	// Seven Key "7"
	Seven = newKey("7")
	// Eight Key "8"
	Eight = newKey("8")
	// Nine Key "9"
	Nine = newKey("9")
	// Semicolon Key ";"
	Semicolon = newKey(";")
	// Equal Key "="
	Equal = newKey("=")
	// A key
	A = newKey("A")
	// B key
	B = newKey("B")
	// C Key
	C = newKey("C")
	// D Key
	D = newKey("D")
	// E Key
	E = newKey("E")
	// F Key
	F = newKey("F")
	// G Key
	G = newKey("G")
	// H Key
	H = newKey("H")
	// I Key
	I = newKey("I")
	// J Key
	J = newKey("J")
	// K Key
	K = newKey("K")
	// L Key
	L = newKey("L")
	// M Key
	M = newKey("M")
	// N Key
	N = newKey("N")
	// O Key
	O = newKey("O")
	// P Key
	P = newKey("P")
	// Q Key
	Q = newKey("Q")
	// R Key
	R = newKey("R")
	// S Key
	S = newKey("S")
	// T Key
	T = newKey("T")
	// U Key
	U = newKey("U")
	// V Key
	V = newKey("V")
	// W Key
	W = newKey("W")
	// X Key
	X = newKey("X")
	// Y Key
	Y = newKey("Y")
	// Z Key
	Z = newKey("Z")
	// LeftBracket Key "["
	LeftBracket = newKey("[")
	// Backslash Key "\"
	Backslash = newKey("\\")
	// RightBracket Key "]"
	RightBracket = newKey("]")
	// GraveAccent Key "`"
	GraveAccent = newKey("`")
	// World1 Key
	World1 = newKey("World 1")
	// World2 Key
	World2 = newKey("World 2")
	// Esc Key
	Esc = newKey("Esc")
	// Enter Key
	Enter = newKey("Enter")
	// Tab Key
	Tab = newKey("Tab")
	// Backspace Key
	Backspace = newKey("Backspace")
	// Insert Key
	Insert = newKey("Insert")
	// Delete Key
	Delete = newKey("Delete")
	// Right Key
	Right = newKey("Right")
	// Left Key
	Left = newKey("Left")
	// Down Key
	Down = newKey("Down")
	// Up Key
	Up = newKey("Up")
	// PageUp Key
	PageUp = newKey("PageUp")
	// PageDown Key
	PageDown = newKey("PageDown")
	// Home Key
	Home = newKey("Home")
	// End Key
	End = newKey("End")
	// CapsLock Key
	CapsLock = newKey("CapsLock")
	// ScrollLock Key
	ScrollLock = newKey("ScrollLock")
	// NumLock Key
	NumLock = newKey("NumLock")
	// PrintScreen Key
	PrintScreen = newKey("PrintScreen")
	// Pause Key
	Pause = newKey("Pause")
	// F1 Key
	F1 = newKey("F1")
	// F2 Key
	F2 = newKey("F2")
	// F3 Key
	F3 = newKey("F3")
	// F4 Key
	F4 = newKey("F4")
	// F5 Key
	F5 = newKey("F5")
	// F6 Key
	F6 = newKey("F6")
	// F7 Key
	F7 = newKey("F7")
	// F8 Key
	F8 = newKey("F8")
	// F9 Key
	F9 = newKey("F9")
	// F10 Key
	F10 = newKey("F10")
	// F11 Key
	F11 = newKey("F11")
	// F12 Key
	F12 = newKey("F12")
	// F13 Key
	F13 = newKey("F13")
	// F14 Key
	F14 = newKey("F14")
	// F15 Key
	F15 = newKey("F15")
	// F16 Key
	F16 = newKey("F16")
	// F17 Key
	F17 = newKey("F17")
	// F18 Key
	F18 = newKey("F18")
	// F19 Key
	F19 = newKey("F19")
	// F20 Key
	F20 = newKey("F20")
	// F21 Key
	F21 = newKey("F21")
	// F22 Key
	F22 = newKey("F22")
	// F23 Key
	F23 = newKey("F23")
	// F24 Key
	F24 = newKey("F24")
	// F25 Key
	F25 = newKey("F25")
	// Keypad0 Key
	Keypad0 = newKey("Keypad 0")
	// Keypad1 Key
	Keypad1 = newKey("Keypad 1")
	// Keypad2 Key
	Keypad2 = newKey("Keypad 2")
	// Keypad3 Key
	Keypad3 = newKey("Keypad 3")
	// Keypad4 Key
	Keypad4 = newKey("Keypad 4")
	// Keypad5 Key
	Keypad5 = newKey("Keypad 5")
	// Keypad6 Key
	Keypad6 = newKey("Keypad 6")
	// Keypad7 Key
	Keypad7 = newKey("Keypad 7")
	// Keypad8 Key
	Keypad8 = newKey("Keypad 8")
	// Keypad9 Key
	Keypad9 = newKey("Keypad 9")
	// KeypadDecimal Key
	KeypadDecimal = newKey("Keypad .")
	// KeypadDivide Key
	KeypadDivide = newKey("Keypad /")
	// KeypadMultiply Key
	KeypadMultiply = newKey("Keypad *")
	// KeypadSubtract Key
	KeypadSubtract = newKey("Keypad -")
	// KeypadAdd Key
	KeypadAdd = newKey("Keypad +")
	// KeypadEnter Key
	KeypadEnter = newKey("Keypad Enter")
	// KeypadEqual Key
	KeypadEqual = newKey("Keypad =")
	// LeftShift Key
	LeftShift = newKey("Left Shift")
	// LeftControl Key
	LeftControl = newKey("Left Control")
	// LeftAlt Key
	LeftAlt = newKey("Left Alt")
	// LeftSuper Key
	LeftSuper = newKey("Left Super")
	// RightShift Key
	RightShift = newKey("Right Shift")
	// RightControl Key
	RightControl = newKey("Right Control")
	// RightAlt Key
	RightAlt = newKey("Right Alt")
	// RightSuper Key
	RightSuper = newKey("Right Super")
	// Menu Key
	Menu = newKey("Menu")

	// AllKeys returns all defined keys
	AllKeys = []Key{}/* 120 elements not displayed */

)
View Source
var EmptyEvent = Event{}

EmptyEvent should be returned by EventSource when it does not have more events.

Functions

This section is empty.

Types

type Event

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

Event describes what happened with the key. Whether it was pressed or released.

Event can be constructed using NewXXXEvent function

func NewPressedEvent

func NewPressedEvent(key Key) Event

NewPressedEvent returns new instance of Event when key was pressed.

func NewReleasedEvent

func NewReleasedEvent(key Key) Event

NewReleasedEvent returns new instance of Event when key was released.

type EventBuffer

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

EventBuffer is a capped collection of accumulated events which can be used by libraries or in unit tests as a fake implementation of EventSource. The order of added events is preserved. EventBuffer is an EventSource and can be directly consumed by Keyboard.

func NewEventBuffer

func NewEventBuffer(size int) *EventBuffer

NewEventBuffer creates EventBuffer of given size. The minimum size of buffer is 1. Size smaller than 1 is constrained to 1.

func (*EventBuffer) Add

func (q *EventBuffer) Add(event Event)

Add adds event to the buffer. If there is not enough space the oldest event will be replaced.

func (*EventBuffer) Poll

func (q *EventBuffer) Poll() (Event, bool)

Poll retrieves and removes event from the buffer. If there are no available events EmptyEvent and false is returned.

type EventSource

type EventSource interface {
	// PollKeyboardEvent retrieves and removes next keyboard Event. If there are no more
	// events false is returned.
	PollKeyboardEvent() (Event, bool)
}

EventSource is a source of keyboard Events. On each Update() Keyboard polls the EventSource by executing PollKeyboardEvent method multiple times - until PollKeyboardEvent() returns false. In other words Keyboard#Update drains the EventSource.

type Key

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

Key identifies the pressed or released key.

func Deserialize

func Deserialize(s string) (Key, error)

Deserialize unmarshalls the key from string which can be used for loading action keymap.

func NewUnknownKey

func NewUnknownKey(scanCode int) Key

NewUnknownKey creates key with platform-specific scanCode.

func (Key) IsUnknown

func (k Key) IsUnknown() bool

IsUnknown returns true if mapping has not been found. ScanCode should be used instead.

func (Key) ScanCode

func (k Key) ScanCode() int

ScanCode returns the platform-specific code.

func (Key) Serialize

func (k Key) Serialize() string

Serialize marshals the key to string which can be used for saving action keymap.

func (Key) String

func (k Key) String() string

String returns the string representation of the Key for debugging purposes. Do not use this method for serialization. Use Key.Serialize instead.

type Keyboard

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

Keyboard provides a read-only information about the current state of the keyboard, such as what keys are currently pressed. Please note that updating the Keyboard state retrieves and removes events from EventSource. Therefore only one Keyboard instance can be created for specific EventSource.

func New

func New(source EventSource) *Keyboard

New creates Keyboard instance. It will consume all events from EventSource each time Update method is called. For this reason you can't have two Keyboard instances for the same EventSource.

func (*Keyboard) JustPressed

func (k *Keyboard) JustPressed(key Key) bool

JustPressed returns true if the key was pressed between two last keyboard.Update calls. If it was pressed and released at the same time between these calls this method return true.

func (*Keyboard) JustReleased

func (k *Keyboard) JustReleased(key Key) bool

JustReleased returns true if the key was released between two last keyboard.Update calls. If it was released and pressed at the same time between these calls this method return true.

func (*Keyboard) Pressed

func (k *Keyboard) Pressed(key Key) bool

Pressed returns true if given key is currently pressed. If between two last keyboard.Update calls the key was pressed and released then the this method returns false.

func (*Keyboard) PressedKeys

func (k *Keyboard) PressedKeys() []Key

PressedKeys returns a slice of all currently pressed keys. It may be empty aka nil. This function can be used to get a key mapping for a given action in the game. If between two last keyboard.Update calls the key was pressed and released then the key is not returned.

func (*Keyboard) Update

func (k *Keyboard) Update()

Update updates the state of the keyboard by polling events queued since last time the function was executed.

Jump to

Keyboard shortcuts

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