api_common.go 4.3 KB
Newer Older
N
nsf 已提交
1
// termbox is a library for creating cross-platform text-based interfaces
2 3 4 5 6
package termbox

// public API, common OS agnostic part

type (
7
	InputMode  int
8
	OutputMode int
9 10 11 12
	EventType  uint8
	Modifier   uint8
	Key        uint16
	Attribute  uint16
13 14
)

15 16 17
// This type represents a termbox event. The 'Mod', 'Key' and 'Ch' fields are
// valid if 'Type' is EventKey. The 'Width' and 'Height' fields are valid if
// 'Type' is EventResize. The 'Err' field is valid if 'Type' is EventError.
18 19 20 21 22 23 24
type Event struct {
	Type   EventType // one of Event* constants
	Mod    Modifier  // one of Mod* constants or 0
	Key    Key       // one of Key* constants, invalid if 'Ch' is not 0
	Ch     rune      // a unicode character
	Width  int       // width of the screen
	Height int       // height of the screen
25
	Err    error     // error in case if input failed
T
taylorchu 已提交
26 27
	MouseX int       // x coord of mouse
	MouseY int       // y coord of mouse
N
nsf 已提交
28
	N      int       // number of bytes written when getting a raw event
29 30 31 32 33 34 35 36 37 38 39
}

// A cell, single conceptual entity on the screen. The screen is basically a 2d
// array of cells. 'Ch' is a unicode character, 'Fg' and 'Bg' are foreground
// and background attributes respectively.
type Cell struct {
	Ch rune
	Fg Attribute
	Bg Attribute
}

40 41 42 43 44
// To know if termbox has been initialized or not
var (
	IsInit bool = false
)

45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
// Key constants, see Event.Key field.
const (
	KeyF1 Key = 0xFFFF - iota
	KeyF2
	KeyF3
	KeyF4
	KeyF5
	KeyF6
	KeyF7
	KeyF8
	KeyF9
	KeyF10
	KeyF11
	KeyF12
	KeyInsert
	KeyDelete
	KeyHome
	KeyEnd
	KeyPgup
	KeyPgdn
	KeyArrowUp
	KeyArrowDown
	KeyArrowLeft
	KeyArrowRight
T
taylorchu 已提交
69
	key_min // see terminfo
T
taylorchu 已提交
70
	MouseLeft
71
	MouseMiddle
T
taylorchu 已提交
72
	MouseRight
73 74 75
	MouseRelease
	MouseWheelUp
	MouseWheelDown
76 77 78 79 80
)

const (
	KeyCtrlTilde      Key = 0x00
	KeyCtrl2          Key = 0x00
N
nsf 已提交
81
	KeyCtrlSpace      Key = 0x00
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
	KeyCtrlA          Key = 0x01
	KeyCtrlB          Key = 0x02
	KeyCtrlC          Key = 0x03
	KeyCtrlD          Key = 0x04
	KeyCtrlE          Key = 0x05
	KeyCtrlF          Key = 0x06
	KeyCtrlG          Key = 0x07
	KeyBackspace      Key = 0x08
	KeyCtrlH          Key = 0x08
	KeyTab            Key = 0x09
	KeyCtrlI          Key = 0x09
	KeyCtrlJ          Key = 0x0A
	KeyCtrlK          Key = 0x0B
	KeyCtrlL          Key = 0x0C
	KeyEnter          Key = 0x0D
	KeyCtrlM          Key = 0x0D
	KeyCtrlN          Key = 0x0E
	KeyCtrlO          Key = 0x0F
	KeyCtrlP          Key = 0x10
	KeyCtrlQ          Key = 0x11
	KeyCtrlR          Key = 0x12
	KeyCtrlS          Key = 0x13
	KeyCtrlT          Key = 0x14
	KeyCtrlU          Key = 0x15
	KeyCtrlV          Key = 0x16
	KeyCtrlW          Key = 0x17
	KeyCtrlX          Key = 0x18
	KeyCtrlY          Key = 0x19
	KeyCtrlZ          Key = 0x1A
	KeyEsc            Key = 0x1B
	KeyCtrlLsqBracket Key = 0x1B
	KeyCtrl3          Key = 0x1B
	KeyCtrl4          Key = 0x1C
	KeyCtrlBackslash  Key = 0x1C
	KeyCtrl5          Key = 0x1D
	KeyCtrlRsqBracket Key = 0x1D
	KeyCtrl6          Key = 0x1E
	KeyCtrl7          Key = 0x1F
	KeyCtrlSlash      Key = 0x1F
	KeyCtrlUnderscore Key = 0x1F
	KeySpace          Key = 0x20
	KeyBackspace2     Key = 0x7F
	KeyCtrl8          Key = 0x7F
)

// Alt modifier constant, see Event.Mod field and SetInputMode function.
const (
129 130
	ModAlt Modifier = 1 << iota
	ModMotion
131 132
)

133 134
// Cell colors, you can combine a color with multiple attributes using bitwise
// OR ('|').
135
const (
N
nsf 已提交
136 137
	ColorDefault Attribute = iota
	ColorBlack
138 139 140 141 142 143 144 145 146
	ColorRed
	ColorGreen
	ColorYellow
	ColorBlue
	ColorMagenta
	ColorCyan
	ColorWhite
)

147 148 149 150
// Cell attributes, it is possible to use multiple attributes by combining them
// using bitwise OR ('|'). Although, colors cannot be combined. But you can
// combine attributes and a single color.
//
J
Josh Soref 已提交
151
// It's worth mentioning that some platforms don't support certain attributes.
152 153 154
// For example windows console doesn't support AttrUnderline. And on some
// terminals applying AttrBold to background may result in blinking text. Use
// them with caution and test your code on various terminals.
155
const (
156
	AttrBold Attribute = 1 << (iota + 9)
N
nsf 已提交
157 158
	AttrUnderline
	AttrReverse
159 160 161 162
)

// Input mode. See SetInputMode function.
const (
163
	InputEsc InputMode = 1 << iota
164
	InputAlt
165 166
	InputMouse
	InputCurrent InputMode = 0
167 168
)

169 170 171 172 173 174 175 176 177
// Output mode. See SetOutputMode function.
const (
	OutputCurrent OutputMode = iota
	OutputNormal
	Output256
	Output216
	OutputGrayscale
)

178 179 180 181
// Event type. See Event.Type field.
const (
	EventKey EventType = iota
	EventResize
T
taylorchu 已提交
182
	EventMouse
183
	EventError
184
	EventInterrupt
N
nsf 已提交
185 186
	EventRaw
	EventNone
187
)