package virtual_dom

  1. Overview
  2. Docs
Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source

Source file keystroke.ml

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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
open Core
open Js_of_ocaml

module Keyboard_code = struct
  module T = struct
    type t = Dom_html.Keyboard_code.t =
      | Unidentified
      | KeyA
      | KeyB
      | KeyC
      | KeyD
      | KeyE
      | KeyF
      | KeyG
      | KeyH
      | KeyI
      | KeyJ
      | KeyK
      | KeyL
      | KeyM
      | KeyN
      | KeyO
      | KeyP
      | KeyQ
      | KeyR
      | KeyS
      | KeyT
      | KeyU
      | KeyV
      | KeyW
      | KeyX
      | KeyY
      | KeyZ
      | Digit0
      | Digit1
      | Digit2
      | Digit3
      | Digit4
      | Digit5
      | Digit6
      | Digit7
      | Digit8
      | Digit9
      | Minus
      | Equal
      | Tab
      | Enter
      | Space
      | Escape
      | Backspace
      | Insert
      | Delete
      | CapsLock
      | BracketLeft
      | BracketRight
      | Semicolon
      | Quote
      | Backquote
      | Backslash
      | Comma
      | Period
      | Slash
      | F1
      | F2
      | F3
      | F4
      | F5
      | F6
      | F7
      | F8
      | F9
      | F10
      | F11
      | F12
      | Numpad0
      | Numpad1
      | Numpad2
      | Numpad3
      | Numpad4
      | Numpad5
      | Numpad6
      | Numpad7
      | Numpad8
      | Numpad9
      | NumpadMultiply
      | NumpadSubtract
      | NumpadAdd
      | NumpadDecimal
      | NumpadEqual
      | NumpadEnter
      | NumpadDivide
      | NumLock
      | ControlLeft
      | ControlRight
      | MetaLeft
      | MetaRight
      | ShiftLeft
      | ShiftRight
      | AltLeft
      | AltRight
      | ArrowLeft
      | ArrowRight
      | ArrowUp
      | ArrowDown
      | PageUp
      | PageDown
      | Home
      | End
      | VolumeMute
      | VolumeDown
      | VolumeUp
      | MediaTrackPrevious
      | MediaTrackNext
      | MediaPlayPause
      | MediaStop
      | ContextMenu
      | BrowserSearch
      | BrowserHome
      | BrowserFavorites
      | BrowserRefresh
      | BrowserStop
      | BrowserForward
      | BrowserBack
      | OSLeft
      | OSRight
      | ScrollLock
      | PrintScreen
      | IntlBackslash
      | IntlYen
      | Pause
    [@@deriving sexp, compare, bin_io, hash, enumerate, equal, sexp_grammar]
  end

  include T
  include Sexpable.To_stringable (T)

  let unidentified = 100000

  let to_key_code_left = function
    | ShiftLeft -> 16
    | ControlLeft -> 17
    | AltLeft -> 18
    | MetaLeft -> 91
    | _ -> unidentified
  ;;

  let to_key_code_right = function
    | ShiftRight -> 16
    | ControlRight -> 17
    | AltRight -> 18
    | MetaRight -> 91
    | _ -> unidentified
  ;;

  let to_key_code_numpad = function
    | NumpadDecimal -> 46
    | Numpad0 -> 45
    | Numpad1 -> 35
    | Numpad2 -> 40
    | Numpad3 -> 34
    | Numpad4 -> 37
    | Numpad5 -> 12
    | Numpad6 -> 39
    | Numpad7 -> 36
    | Numpad8 -> 38
    | Numpad9 -> 33
    | NumpadEnter -> 13
    | NumpadDivide -> 111
    | NumpadAdd -> 107
    | NumpadSubtract -> 109
    | NumpadMultiply -> 106
    | _ -> unidentified
  ;;

  let to_key_code_normal = function
    | Escape -> 27
    | F1 -> 112
    | F2 -> 113
    | F3 -> 114
    | F4 -> 115
    | F5 -> 116
    | F6 -> 117
    | F7 -> 118
    | F8 -> 119
    | F9 -> 120
    | F10 -> 121
    | F11 -> 122
    | F12 -> 123
    | PrintScreen -> 42
    | ScrollLock -> 145
    | Pause -> 19
    | Backquote -> 192
    | Digit1 -> 49
    | Digit2 -> 50
    | Digit3 -> 51
    | Digit4 -> 52
    | Digit5 -> 53
    | Digit6 -> 54
    | Digit7 -> 55
    | Digit8 -> 56
    | Digit9 -> 57
    | Digit0 -> 48
    | Minus -> 189
    | Equal -> 187
    | Backspace -> 8
    | Tab -> 9
    | KeyQ -> 81
    | KeyW -> 87
    | KeyE -> 69
    | KeyR -> 82
    | KeyT -> 84
    | KeyY -> 89
    | KeyU -> 85
    | KeyI -> 73
    | KeyO -> 79
    | KeyP -> 80
    | BracketLeft -> 219
    | BracketRight -> 221
    | Backslash -> 220
    | CapsLock -> 20
    | KeyA -> 65
    | KeyS -> 83
    | KeyD -> 68
    | KeyF -> 70
    | KeyG -> 71
    | KeyH -> 72
    | KeyJ -> 74
    | KeyK -> 75
    | KeyL -> 76
    | Semicolon -> 186
    | Quote -> 222
    | Enter -> 13
    | KeyZ -> 90
    | KeyX -> 88
    | KeyC -> 67
    | KeyV -> 86
    | KeyB -> 66
    | KeyN -> 78
    | KeyM -> 77
    | Comma -> 188
    | Period -> 190
    | Slash -> 191
    | Space -> 32
    | ContextMenu -> 93
    | Insert -> 45
    | Home -> 36
    | PageUp -> 33
    | Delete -> 46
    | End -> 35
    | PageDown -> 34
    | ArrowLeft -> 37
    | ArrowDown -> 40
    | ArrowRight -> 39
    | ArrowUp -> 38
    | _ -> unidentified
  ;;

  let to_location (t : t) =
    match t with
    | Unidentified
    | KeyA
    | KeyB
    | KeyC
    | KeyD
    | KeyE
    | KeyF
    | KeyG
    | KeyH
    | KeyI
    | KeyJ
    | KeyK
    | KeyL
    | KeyM
    | KeyN
    | KeyO
    | KeyP
    | KeyQ
    | KeyR
    | KeyS
    | KeyT
    | KeyU
    | KeyV
    | KeyW
    | KeyX
    | KeyY
    | KeyZ -> `Other
    | Digit0
    | Digit1
    | Digit2
    | Digit3
    | Digit4
    | Digit5
    | Digit6
    | Digit7
    | Digit8
    | Digit9
    | Minus
    | Equal
    | Tab
    | Enter
    | Space
    | Escape
    | Backspace
    | Insert
    | Delete
    | CapsLock
    | BracketLeft
    | BracketRight
    | Semicolon
    | Quote
    | Backquote
    | Backslash
    | Comma
    | Period
    | Slash
    | F1
    | F2
    | F3
    | F4
    | F5
    | F6
    | F7
    | F8
    | F9
    | F10
    | F11
    | F12
    | NumLock
    | ArrowLeft
    | ArrowRight
    | ArrowUp
    | ArrowDown
    | PageUp
    | PageDown
    | Home
    | End
    | VolumeMute
    | VolumeDown
    | VolumeUp
    | MediaTrackPrevious
    | MediaTrackNext
    | MediaPlayPause
    | MediaStop
    | ContextMenu
    | BrowserSearch
    | BrowserHome
    | BrowserFavorites
    | BrowserRefresh
    | BrowserStop
    | BrowserForward
    | BrowserBack
    | OSLeft
    | OSRight
    | ScrollLock
    | PrintScreen
    | IntlBackslash
    | IntlYen
    | Pause -> `Other
    | Numpad0
    | Numpad1
    | Numpad2
    | Numpad3
    | Numpad4
    | Numpad5
    | Numpad6
    | Numpad7
    | Numpad8
    | Numpad9
    | NumpadMultiply
    | NumpadSubtract
    | NumpadAdd
    | NumpadDecimal
    | NumpadEqual
    | NumpadEnter
    | NumpadDivide -> `Numpad
    | ShiftLeft | MetaLeft | AltLeft | ControlLeft -> `Left
    | ControlRight | MetaRight | ShiftRight | AltRight -> `Right
  ;;

  let to_key_code t =
    match to_location t with
    | `Left -> to_key_code_left t
    | `Right -> to_key_code_right t
    | `Numpad -> to_key_code_numpad t
    | `Other -> to_key_code_normal t
  ;;

  let to_location t =
    match to_location t with
    | `Left -> 1
    | `Right -> 2
    | `Numpad -> 3
    | `Other -> 4
  ;;

  let of_event = Dom_html.Keyboard_code.of_event
end

module T = struct
  type t =
    { key : Keyboard_code.t
    ; ctrl : bool
    ; alt : bool
    ; shift : bool
    ; meta : bool
    }
  [@@deriving sexp, compare, bin_io, hash, fields ~getters, sexp_grammar]
end

include T
include Comparable.Make_binable (T)
include Hashable.Make_binable (T)

let create key ~ctrl ~alt ~shift ~meta = { key; ctrl; alt; shift; meta }

let create' ?ctrl ?alt ?shift ?meta key =
  { key
  ; ctrl = Option.is_some ctrl
  ; alt = Option.is_some alt
  ; shift = Option.is_some shift
  ; meta = Option.is_some meta
  }
;;

let of_event (ev : Keyboard_event.t) =
  let key = Keyboard_event.key ev in
  let ctrl = Keyboard_event.ctrl ev in
  let alt = Keyboard_event.alt ev in
  let shift = Keyboard_event.shift ev in
  let meta = Keyboard_event.meta ev in
  create key ~ctrl ~alt ~shift ~meta
;;

module With_prefix = struct
  type t =
    | Key of string
    | Digit of string
    | Numpad of string
    | Arrow of string
    | Other of string
  [@@deriving variants]

  let of_string str =
    List.find_map
      [ "Key", key; "Digit", digit; "Numpad", numpad; "Arrow", arrow ]
      ~f:(fun (prefix, f) ->
        if String.is_prefix str ~prefix
        then Some (f (String.drop_prefix str (String.length prefix)))
        else None)
    |> Option.value ~default:(Other str)
  ;;
end

let shift_combo_to_string (key : Keyboard_code.t) =
  match key with
  | Digit0 -> Some ")"
  | Digit1 -> Some "!"
  | Digit2 -> Some "@"
  | Digit3 -> Some "#"
  | Digit4 -> Some "$"
  | Digit5 -> Some "%"
  | Digit6 -> Some "^"
  | Digit7 -> Some "&"
  | Digit8 -> Some "*"
  | Digit9 -> Some "("
  | Minus -> Some "_"
  | Equal -> Some "+"
  | BracketLeft -> Some "{"
  | BracketRight -> Some "}"
  | Semicolon -> Some ":"
  | Quote -> Some "\""
  | Backquote -> Some "~"
  | Backslash -> Some "|"
  | Comma -> Some "<"
  | Period -> Some ">"
  | Slash -> Some "?"
  | _ -> None
;;

let keyboard_code_to_string (key : Keyboard_code.t) (key_with_prefix : With_prefix.t) =
  match key_with_prefix with
  | Key str -> String.lowercase str
  | Digit str | Arrow str -> str
  | Numpad str ->
    (match key with
     | NumpadMultiply -> "*"
     | NumpadSubtract -> "-"
     | NumpadAdd -> "+"
     | NumpadDecimal -> "."
     | NumpadEqual -> "="
     | NumpadDivide -> "/"
     | _ -> str)
  | Other str ->
    (match key with
     | Minus -> "-"
     | Equal -> "="
     | Escape -> "Esc"
     | Backspace -> "Bksp"
     | BracketLeft -> "["
     | BracketRight -> "]"
     | Semicolon -> ";"
     | Quote -> "'"
     | Backquote -> "`"
     | Backslash -> "\\"
     | Comma -> ","
     | Period -> "."
     | Slash -> "/"
     | _ -> str)
;;

let shift_string_and_keyboard_code_string (t : t) =
  let shift_str, shift_combo_str =
    match t.shift with
    | false -> "", None
    | true ->
      (match shift_combo_to_string t.key with
       | None -> "Shift+", None
       | Some str -> "", Some str)
  in
  let keyboard_code_str =
    match shift_combo_str with
    | Some str -> str
    | None ->
      let key_with_prefix =
        t.key |> Keyboard_code.sexp_of_t |> Sexp.to_string |> With_prefix.of_string
      in
      keyboard_code_to_string t.key key_with_prefix
  in
  shift_str, keyboard_code_str
;;

let to_string_hum t =
  let open Core in
  let ctrl_str = if t.ctrl then "Ctrl+" else "" in
  let alt_str = if t.alt then "Alt+" else "" in
  let meta_str = if t.meta then "Meta+" else "" in
  let shift_str, keyboard_code_str = shift_string_and_keyboard_code_string t in
  String.concat [ ctrl_str; alt_str; shift_str; meta_str; keyboard_code_str ]
;;

let%expect_test "" =
  let print ?ctrl ?alt ?shift ?meta key =
    printf !"%{to_string_hum}\n" (create' ?ctrl ?alt ?shift ?meta key)
  in
  print KeyA;
  [%expect {| a |}];
  print ~shift:() KeyA;
  [%expect {| Shift+a |}];
  print ~ctrl:() ~alt:() ~shift:() ~meta:() KeyA;
  [%expect {| Ctrl+Alt+Shift+Meta+a |}];
  print Digit1;
  [%expect {| 1 |}];
  print ~ctrl:() Digit1;
  [%expect {| Ctrl+1 |}];
  print Numpad1;
  [%expect {| 1 |}];
  print ~ctrl:() Numpad1;
  [%expect {| Ctrl+1 |}];
  print ~ctrl:() Numpad1;
  [%expect {| Ctrl+1 |}];
  print Comma;
  [%expect {| , |}];
  print ~shift:() Comma;
  [%expect {| < |}];
  print ~ctrl:() Comma;
  [%expect {| Ctrl+, |}];
  print ~ctrl:() ~shift:() Comma;
  [%expect {| Ctrl+< |}]
;;
OCaml

Innovation. Community. Security.