Documentation
¶
Overview ¶
Package keys provides constants for keyboard inputs (X11 KeySyms as used by the RFB protocol per RFC 6143 §7.5.4). The constants in this package mirror ASCII/Latin-1 where possible:
- Printable ASCII U+0020..U+007E are sequential and map directly; for a printable rune r in that range, Key(r) equals the corresponding constant (e.g., Key('A') == A, Key('a') == SmallA, Key('0') == Digit0, Key('-') == Minus).
- Extended Latin-1 U+0080..U+00FF are also represented in the low 8 bits for convenience.
- Special and control keys live in the 0xFFxx range (e.g., BackSpace 0xFF08, Tab 0xFF09, Return 0xFF0D, Escape 0xFF1B), matching X11 KeySym values.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Key ¶
type Key uint32
Key represents a VNC key press (an X11 KeySym value on the wire).
Example ¶
ExampleKey demonstrates direct Key constant usage for special keys.
package main
import (
"fmt"
"github.com/kward/go-vnc/keys"
)
func main() {
// Special keys are available as constants
fmt.Printf("Enter key: %s (0x%x)\n", keys.Return, uint32(keys.Return))
fmt.Printf("Escape key: %s (0x%x)\n", keys.Escape, uint32(keys.Escape))
fmt.Printf("Tab key: %s (0x%x)\n", keys.Tab, uint32(keys.Tab))
// Function keys
fmt.Printf("F1 key: %s (0x%x)\n", keys.F1, uint32(keys.F1))
// Modifier keys
fmt.Printf("Left Shift: %s (0x%x)\n", keys.ShiftLeft, uint32(keys.ShiftLeft))
fmt.Printf("Left Control: %s (0x%x)\n", keys.ControlLeft, uint32(keys.ControlLeft))
}
Output: Enter key: Return (0xff0d) Escape key: Escape (0xff1b) Tab key: Tab (0xff09) F1 key: F1 (0xffbe) Left Shift: ShiftLeft (0xffe1) Left Control: ControlLeft (0xffe3)
const ( Space Key = iota + 0x0020 Exclaim // exclamation mark QuoteDbl NumberSign Dollar Percent Ampersand Apostrophe ParenLeft ParenRight Asterisk Plus Comma Minus Period Slash Digit0 Digit1 Digit2 Digit3 Digit4 Digit5 Digit6 Digit7 Digit8 Digit9 Colon Semicolon Less Equal Greater Question At A B C D E F G H I J K L M N O P Q R S T U V W X Y Z BracketLeft Backslash BracketRight AsciiCircum Underscore Grave SmallA SmallB SmallC SmallD SmallE SmallF SmallG SmallH SmallI SmallJ SmallK SmallL SmallM SmallN SmallO SmallP SmallQ SmallR SmallS SmallT SmallU SmallV SmallW SmallX SmallY SmallZ BraceLeft Bar BraceRight AsciiTilde )
Latin 1 (byte 3 = 0) ISO/IEC 8859-1 = Unicode U+0020..U+00FF
const ( KeypadF1 Key = iota + 0xff91 KeypadF2 KeypadF3 KeypadF4 KeypadHome KeypadLeft KeypadUp KeypadRight KeypadDown KeypadPrior KeypadPageUp KeypadNext KeypadPageDown KeypadEnd KeypadBegin KeypadInsert KeypadDelete KeypadMultiply KeypadAdd KeypadSeparator KeypadSubtract KeypadDecimal KeypadDivide Keypad0 Keypad1 Keypad2 Keypad3 Keypad4 Keypad5 Keypad6 Keypad7 Keypad8 Keypad9 KeypadEqual Key = 0xffbd )
func FromRune ¶
FromRune converts a rune to a Key. It handles printable ASCII (U+0020..U+007E), extended Latin-1 (U+0080..U+00FF), and common control characters (\n, \t, \b, \r). Returns (Key, true) on success or (0, false) for unsupported runes.
Example ¶
ExampleFromRune demonstrates converting individual runes to Key values. FromRune handles printable ASCII, extended Latin-1, and common control characters.
package main
import (
"fmt"
"github.com/kward/go-vnc/keys"
)
func main() {
// Printable ASCII characters
k, ok := keys.FromRune('A')
if ok {
fmt.Printf("'A' -> %s (0x%x)\n", k, uint32(k))
}
// Control character
k, ok = keys.FromRune('\n')
if ok {
fmt.Printf("'\\n' -> %s (0x%x)\n", k, uint32(k))
}
// Unsupported character
_, ok = keys.FromRune('😀')
fmt.Printf("'😀' supported: %v\n", ok)
}
Output: 'A' -> A (0x41) '\n' -> Linefeed (0xff0a) '😀' supported: false
type Keys ¶
type Keys []Key
Keys is a convenience slice of Key values.
func IntToKeys ¶
IntToKeys returns Keys that represent the key presses required to type an int using ASCII digits and an optional leading minus sign. Digits and minus map directly since they're in the printable ASCII range.
Example ¶
ExampleIntToKeys demonstrates converting an integer to Key values. This is useful for typing numbers via VNC.
package main
import (
"fmt"
"github.com/kward/go-vnc/keys"
)
func main() {
// Positive number
ks := keys.IntToKeys(123)
fmt.Printf("123 -> %d keys:", len(ks))
for _, k := range ks {
fmt.Printf(" %s", k)
}
fmt.Println()
// Negative number (includes minus sign)
ks = keys.IntToKeys(-42)
fmt.Printf("-42 -> %d keys:", len(ks))
for _, k := range ks {
fmt.Printf(" %s", k)
}
fmt.Println()
// Zero
ks = keys.IntToKeys(0)
fmt.Printf("0 -> %d keys: %s\n", len(ks), ks[0])
}
Output: 123 -> 3 keys: Digit1 Digit2 Digit3 -42 -> 3 keys: Minus Digit4 Digit2 0 -> 1 keys: Digit0
func TextToKeys ¶
TextToKeys converts a string to Keys by mapping each rune via FromRune. Returns an error if any rune is unsupported.
Example ¶
ExampleTextToKeys demonstrates converting a string to a slice of Key values. This is useful for simulating typing text via VNC.
package main
import (
"fmt"
"github.com/kward/go-vnc/keys"
)
func main() {
// Convert a simple string to keys
ks, err := keys.TextToKeys("Hello")
if err != nil {
fmt.Printf("Error: %v\n", err)
return
}
fmt.Printf("'Hello' -> %d keys\n", len(ks))
fmt.Printf("First key: %s\n", ks[0])
// String with control characters
ks, err = keys.TextToKeys("line1\nline2")
if err != nil {
fmt.Printf("Error: %v\n", err)
return
}
fmt.Printf("'line1\\nline2' -> %d keys (includes Linefeed)\n", len(ks))
// Unsupported characters produce an error
_, err = keys.TextToKeys("test😀")
fmt.Printf("Error with emoji: %v\n", err != nil)
}
Output: 'Hello' -> 5 keys First key: H 'line1\nline2' -> 11 keys (includes Linefeed) Error with emoji: true