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
type code =
| Char of string
| Enter
| Escape
| Tab
| Up
| Down
| Left
| Right
| F1
| F2
| F3
| F4
| F5
| F6
| F7
| F8
| F9
| F10
| F11
| F12
| Next_page
| Prev_page
| Home
| End
| Insert
| Delete
| Backspace
let code_to_int=
let bin_to_int bin=
let len= String.length bin in
let rec calc s pos acc=
if pos < len then
calc s (pos+1) (acc + (int_of_char s.[pos]) lsl (pos * 8))
else
acc
in
calc bin 0 0
in
function
| Enter-> 1
| Escape-> 2
| Tab-> 3
| Up-> 4
| Down-> 5
| Left-> 6
| Right-> 7
| F1-> 8
| F2-> 9
| F3-> 10
| F4-> 11
| F5-> 12
| F6-> 13
| F7-> 14
| F8-> 15
| F9-> 16
| F10-> 17
| F11-> 18
| F12-> 19
| Next_page-> 20
| Prev_page-> 21
| Home-> 22
| End-> 23
| Insert-> 24
| Delete-> 25
| Backspace-> 26
| Char bin-> 27 + (bin_to_int bin)
let code_to_string= function
| Char bin-> Printf.sprintf "Char %s" bin
| Enter-> "Enter"
| Escape-> "Escape"
| Tab-> "Tab"
| Up-> "Up"
| Down-> "Down"
| Left-> "Left"
| Right-> "Right"
| F1-> "F1"
| F2-> "F2"
| F3-> "F3"
| F4-> "F4"
| F5-> "F5"
| F6-> "F6"
| F7-> "F7"
| F8-> "F8"
| F9-> "F9"
| F10-> "F10"
| F11-> "F11"
| F12-> "F12"
| Next_page-> "Next_page"
| Prev_page-> "Prev_page"
| Home-> "Home"
| End-> "End"
| Insert-> "Insert"
| Delete-> "Delete"
| Backspace-> "Backspace"
type t = {
control: bool;
meta: bool;
shift: bool;
code : code;
}
let t_to_int t=
let b= function true -> 1 | false -> 0 in
(code_to_int t.code, [b t.control; b t.meta; b t.shift])
let t_to_string t= Printf.sprintf "{ %s%s%s%s }"
(code_to_string t.code)
(if t.control then "; control" else "")
(if t.meta then "; meta" else "")
(if t.shift then "; shift" else "")
type modifier=
| Control
| Meta
| Shift
let compare_code t1 t2= compare t1.code t2.code
let compare_modifier t1 t2=
let c= compare t1.control t2.control in
if c <> 0 then c else
let c= compare t1.meta t2.meta in
if c <> 0 then c else
compare t1.shift t2.shift
module Modifiers = Set.Make(
struct
type t= modifier
let compare= compare
end)
type modifiers= Modifiers.t
let compare t1 t2=
let c= compare_code t1 t2 in
if c <> 0 then c
else compare_modifier t1 t2
let control key = key.control
let meta key = key.meta
let shift key = key.shift
let code key = key.code
let create ~code ~modifiers=
{ control= Modifiers.mem Control modifiers;
meta= Modifiers.mem Meta modifiers;
shift= Modifiers.mem Shift modifiers;
code
}
let create_modifiers= Modifiers.of_list
let modifiers t=
let l= [] in
let l= if t.control then Control::l else l in
let l= if t.meta then Meta::l else l in
let l= if t.shift then Shift::l else l in
Modifiers.of_list l
let modifier ~key ~modifier=
match modifier with
| Control-> key.control
| Meta-> key.meta
| Shift-> key.shift
let equal k1 k2= compare k1 k2 = 0
let hash k= let code, modifier= t_to_int k in
Mew.Key.hash code modifier
let to_string= t_to_string