kbd.anubis
6.18 KB
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
*Project* The Anubis Project
*Title* Testing mouse and keyboard events in a host window.
*Copyright* Copyright (c) Alain Prouté 2005.
*Author* Alain Prouté
define Int32 length( List($T) l) =
if l is { [] then 0, [h . t] then 1 + length(t)}.
*** Formating functions.
define String
Int32 n + String s
=
integer_to_string(n) + s.
define String
format
(
KeyboardState ks
) =
if ks is
{
kbdstate(s,c,a,l,m,r) then
(if s then "s" else "-") +
(if c then "c" else "-") +
(if a then "a" else "-") +
(if l then "l" else "-") +
(if m then "m" else "-") +
(if r then "r" else "-")
}.
define String
format
(
KeyboardSpecialKey k
) =
if k is
{
escape then "escape",
f1 then "f1",
f2 then "f2",
f3 then "f3",
f4 then "f4",
f5 then "f5",
f6 then "f6",
f7 then "f7",
f8 then "f8",
f9 then "f9",
f10 then "f10",
backspace then "backspace",
home then "home",
end then "end",
tab then "tab",
enter then "enter",
insert then "insert",
del then "del",
page_up then "page_up",
page_down then "page_down",
up then "up",
down then "down",
left then "left",
right then "right"
}.
define String
format
(
MouseClick mc
) =
if mc is
{
left_down then "left_down",
left_up then "left_up",
middle_down then "middle_down",
middle_up then "middle_up",
right_down then "right_down",
right_up then "right_up"
}.
define String
format
(
Rectangle r
) =
if r is rect(x,y,u,v) then "["+x+","+y+","+u+","+v+"]".
define String
format
(
List(Rectangle) l
) =
if l is
{
[ ] then "",
[h . t] then format(h) + if t is [] then "" else ","+format(t)
}.
define String
format
(
KeyboardKey kk
) =
if kk is
{
unknown then "unknown",
char(c) then "char("+int8_to_int32(c)+")",
unicode(a,b) then "unicode("+int8_to_int32(a)+","+int8_to_int32(b)+")",
special(s) then "special("+format(s)+")"
}.
define String
format
(
HostWindowEvent(One) e
) =
if e is
{
quit then "quit",
expose then "expose",
pointer_entering then "pointer_entering",
pointer_leaving then "pointer_leaving",
key_down(ks,kk) then "key_down("+format(ks)+","+format(kk)+")",
mouse_move(ks,x,y) then "mouse_move("+format(ks)+","+x+","+y+")",
mouse_click(ks,mc,x,y) then "mouse_click("+format(ks)+","+format(mc)+","+x+","+y+")",
mouse_wheel(ks,d,x,y) then "mouse_wheel("+format(ks)+","+d+","+x+","+y+")",
tick then "tick",
repaint(l) then "repaint("+format(l)+")",
specific(u) then "specific(unique)"
}.
*** Redrawing the host window.
define (HostWindow,List(Rectangle)) -> One
redraw_window
(
HostImage buffer,
) =
(HostWindow hw, List(Rectangle) l) |->
unique;
if size(buffer) is (w,h) then
paint_image(hw,rect(0,0,w,h),0,0,buffer).
*** Event handler.
define One
draw_h_scale
(
HostImage buffer,
Int32 w,
Int32 x
) =
if x >= w then unique else
draw_rectangle(buffer,rect(x,0,x+1,8),rgb(0,0,0));
draw_h_scale(buffer,w,x+10).
define One
draw_v_scale
(
HostImage buffer,
Int32 h,
Int32 y
) =
if y >= h then unique else
draw_rectangle(buffer,rect(0,y,8,y+1),rgb(0,0,0));
draw_v_scale(buffer,h,y+10).
define One
draw_scales
(
HostImage buffer,
Int32 w,
Int32 h
) =
draw_rectangle(buffer,rect(0,0,w,h),rgb(0,255,0));
draw_h_scale(buffer,w,0);
draw_v_scale(buffer,h,0).
define One
drawchar
(
KeyboardKey kk,
HostImage buffer,
SystemFont font
) =
if size(buffer) is (w,h) then
if kk is char(c)
then forget(draw_system_character(buffer,rect(0,0,w,h),30,50,font,int8_to_int32(c),rgb(0,0,0)))
else unique.
define (HostWindow, HostWindowEvent(One)) -> List(Rectangle)
event_handler
(
HostImage buffer,
SystemFont font,
) =
(HostWindow hw, HostWindowEvent(One) e) |->
if size(buffer) is (w,h) then
if e is tick then [] else
(
if e is key_down(ks,kk)
then (draw_scales(buffer,w,h); drawchar(kk,buffer,font); [rect(0,0,w,h)])
else []
).
;
(if e is key_down(ks,kk) then drawchar(kk,buffer,font) else unique);
(if e is tick then unique else print(format(e)+"\n"));
[rect(0,0,w,h)].
global define One
kbd
(
List(String) args
) =
if load_system_font("helvetica_bold_r_24") is
{
failure then print("cannot load system font.\n"),
success(font) then
with total_width = (Int32)300,
total_height = (Int32)200,
buffer = to_host_image(
create_rgba_image(
total_width,total_height,rgba(0,0,255,255)),1),
if open_host_window
(
rect(500,500,500+total_width,500+total_height),
"kbd test",
managed,
redraw_window(buffer),
event_handler(buffer,font),
(List(HostWindowEvent(One)) l) |-> l) is
{
failure then print("Cannot open host window.\n"),
success(hw) then show(hw)
}
}.