Debian Codegraph @

vt/event.go

raw symbols SCIP
1 // Copyright 2025 The TCell Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 package vt
16
17 // KeyEvent is a key event.
18 type KeyEvent struct {
19 Down bool // true if event is for key down event
20 Repeat int // if > 1, a repeat count
21 Key Key // Key symbol.
22 Base BaseKey // base key code (physical key, e.g 'a'), may be zero if same as code
23 VK WinVK // Windows virtual key. 0 for none, or if not known.
24 SC ScanCode // Windows scan code. 0 for none, or if not known.
25 Mod Modifier // modifiers
26 Utf string // if non-empty, the unicode content for this
27 }
28
29 type Modifier int
30
31 const (
32 ModNone = Modifier(0)
33 ModLShift = Modifier(1 << iota)
34 ModRShift
35 ModLCtrl
36 ModRCtrl
37 ModLAlt
38 ModRAlt
39 ModLMeta
40 ModRMeta
41 ModLHyper
42 ModRHyper
43 ModCapsLock
44 ModNumLock
45 )
46
47 func (m Modifier) IsShift() bool { return (m & (ModLShift | ModRShift)) != 0 }
48 func (m Modifier) IsCtrl() bool { return (m & (ModLCtrl | ModRCtrl)) != 0 }
49 func (m Modifier) IsAlt() bool { return (m & (ModLAlt | ModRAlt)) != 0 }
50 func (m Modifier) IsMeta() bool { return (m & (ModLMeta | ModRMeta)) != 0 }
51 func (m Modifier) IsHyper() bool { return (m & (ModLHyper | ModRHyper)) != 0 }
52 func (m Modifier) IsNumLock() bool { return m&ModNumLock != 0 }
53 func (m Modifier) IsCapsLock() bool { return m&ModCapsLock != 0 }
54 func (m Modifier) IsCapitals() bool { return m.IsCapsLock() != m.IsShift() }
55 func (m Modifier) IsAltGr() bool { return m.IsCtrl() && m.IsAlt() }
56
57 // Button is the mouse button pressed or released.
58 type Button int
59
60 const (
61 NoButton = Button(0) // No buttons are pressed.
62 Button1 = Button(1 << iota) // Usually left most button.
63 Button2 // Usually right most button.
64 Button3 // Usually middle button.
65 Button4
66 Button5
67 Button6
68 Button7
69 Button8
70 WheelUp // Wheel motion up/away from user.
71 WheelDown // Wheel motion down/towards user.
72 WheelLeft // Wheel motion to left.
73 WheelRight // Wheel motion to right.
74 )
75
76 // MouseEvent reports a single mouse event. Only a single button
77 // may be reported for a given event. The application will have
78 // to keep state. As buttons are never pressed exactly simultaneously,
79 // the backend will send chords as a series of presses followed by a series
80 // of releases.
81 type MouseEvent struct {
82 Position Coord // Location of pointer.
83 Button Button // Buttons pressed.
84 Down bool // True on press, false on release.
85 Motion bool // True if mouse moved at least once cell.
86 Mod Modifier // Modifiers (for modified click).
87 }
88
89 // encodeButton just encodes the XTerm style button details into a byte
90 func (ev MouseEvent) encodeButton() byte {
91 var btn byte
92 switch ev.Button {
93 case NoButton:
94 btn = 3
95 case Button1:
96 btn = 0
97 case Button2: // intentionally reversed with button 3
98 btn = 2
99 case Button3:
100 btn = 1
101 case WheelUp:
102 btn = 0x40
103 case WheelDown:
104 btn = 0x41
105 case WheelLeft:
106 btn = 0x42
107 case WheelRight:
108 btn = 0x43
109 case Button4:
110 btn = 0x80
111 case Button5:
112 btn = 0x81
113 case Button6:
114 btn = 0x82
115 case Button7:
116 btn = 0x83
117 default:
118 btn = 3
119 }
120 if ev.Motion {
121 btn += 0x20
122 }
123 if ev.Mod.IsShift() {
124 btn += 4
125 }
126 if ev.Mod.IsAlt() || ev.Mod.IsMeta() {
127 btn += 8
128 }
129 if ev.Mod.IsCtrl() {
130 btn += 16
131 }
132 return btn
133 }