-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkeyboard.v
184 lines (168 loc) · 4.35 KB
/
keyboard.v
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
module cdv
import x.json2 as json
import time
pub struct Keyboard {
pub mut:
modifiers int
pressed_keys []string
page &Page
}
pub fn (mut page Page) use_keyboard() &Keyboard {
return page.keyboard or {
kb := &Keyboard{
page: page
}
page.keyboard = kb
return kb
}
}
@[params]
pub struct KeyEventParams {
pub:
typ string @[json: 'type']
modifiers ?int
timestamp ?f64
text ?string
unmodified_text ?string @[json: 'unmodifiedText']
key_identifier ?string @[json: 'keyIdentifier']
code ?string @[json: 'code']
key ?string @[json: 'key']
windows_virtual_key_code ?int @[json: 'windowsVirtualKeyCode']
native_virtual_key_code ?int @[json: 'nativeVirtualKeyCode']
auto_repeat ?bool @[json: 'autoRepeat']
is_keypad ?bool @[json: 'isKeypad']
is_system_key ?bool @[json: 'isSystemKey']
location ?int
commands ?[]string
}
pub fn (mut kb Keyboard) dispatch_key_event(opts KeyEventParams) ! {
params := struct_to_json_any(opts)!.as_map()
kb.page.send_or_noop('Input.dispatchKeyEvent', params: params)
}
fn (mut kb Keyboard) get_modifiers(key string) int {
return match key {
'Alt' { 1 }
'Control' { 2 }
'Meta' { 4 }
'Shift' { 8 }
else { 0 }
}
}
@[params]
pub struct OptionsDown {
pub:
text ?string
commands []string
delay ?i64
}
pub fn (mut kb Keyboard) down(key string, opts OptionsDown) {
mut params := map[string]json.Any{}
mut desc := kb.get_desc(key)
kb.modifiers |= kb.get_modifiers(desc.key)
mut text := opts.text or { desc.text }
auto_repeat := kb.pressed_keys.contains(desc.code)
if !auto_repeat {
kb.pressed_keys << desc.code
}
params['commands'] = opts.commands.map(json.Any(it))
params['text'] = text
params['type'] = if text != '' { 'keyDown' } else { 'rawKeyDown' }
params['modifiers'] = kb.modifiers
params['key'] = desc.key
params['windowsVirtualKeyCode'] = desc.key_code
params['code'] = desc.code
params['unmodifiedText'] = text
params['autoRepeat'] = auto_repeat
params['location'] = desc.location
params['isKeypad'] = desc.location == 3
kb.page.send_or_noop('Input.dispatchKeyEvent', params: params)
}
pub fn (mut kb Keyboard) up(key string) {
mut params := map[string]json.Any{}
mut desc := kb.get_desc(key)
kb.modifiers &= ~kb.get_modifiers(desc.key)
idx := kb.pressed_keys.index(desc.code)
kb.pressed_keys.delete(idx)
params['type'] = 'keyUp'
params['modifiers'] = kb.modifiers
params['key'] = desc.key
params['windowsVirtualKeyCode'] = desc.key_code
params['code'] = desc.code
params['location'] = desc.location
kb.page.send_or_noop('Input.dispatchKeyEvent', params: params)
}
pub fn (mut kb Keyboard) press(key string, opts OptionsDown) {
kb.down(key, opts)
if delay := opts.delay {
time.sleep(delay)
}
kb.up(key)
}
pub fn (mut kb Keyboard) send_character(text string) {
kb.page.send_or_noop('Input.insertText',
params: {
'text': text
}
)
}
pub fn (mut kb Keyboard) type(text string, opts OptionsDown) {
for rn in text.runes() {
chr := rn.str()
if chr in key_map {
kb.press(chr, delay: opts.delay)
} else {
if delay := opts.delay {
time.sleep(delay)
}
kb.send_character(chr)
}
}
}
struct KeyDesc {
mut:
key string
key_code int
code string
text string
location int
}
fn (mut kb Keyboard) get_desc(key string) KeyDesc {
if key !in key_map {
eprintln('unknown key ${key}')
return KeyDesc{}
}
is_shift := (kb.modifiers & 8) != 0
mut def := key_map[key].clone()
mut desc := KeyDesc{}
if d_key := def['key'] {
desc.key = d_key.str()
}
if is_shift && def['shiftKey'] or { '' }.str() != '' {
desc.key = def['shiftKey'] or { '' }.str()
}
if key_code := def['keyCode'] {
desc.key_code = key_code.int()
}
if is_shift && def['shiftKeyCode'] or { json.Any(-1) }.int() != -1 {
desc.key_code = def['shiftKeyCode'] or { json.Any(0) }.int()
}
if code := def['code'] {
desc.code = code.str()
}
if location := def['location'] {
desc.location = location.int()
}
if desc.key.len == 1 {
desc.text = desc.key
}
if text := def['text'] {
desc.text = text.str()
}
if is_shift && def['shiftText'] or { '' }.str() != '' {
desc.text = def['shiftText'] or { '' }.str()
}
if (kb.modifiers & ~8) > 0 {
desc.text = ''
}
return desc
}