/
KeyboardEvent.purs
128 lines (103 loc) · 3.34 KB
/
KeyboardEvent.purs
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
-- | Functions that expose the KeyboardEvent API.
-- |
-- | https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent
-- |
-- | Note: The deprecated attributes `.keyCode`, `.charCode`, and
-- | `.which` are deliberately omitted. It is currently recommended to use
-- | `KeyboardEvent.key` instead.
-- |
-- | If browser support for `KeyboardEvent.key` is not yet widespread
-- | enough for your use case, consider using a polyfill
-- | (e.g. https://github.com/inexorabletash/polyfill#keyboard-events)
-- | or use the purescript FFI to access the deprecated attributes you
-- | want to work with.
-- |
module Web.UIEvent.KeyboardEvent
( KeyboardEvent
, fromUIEvent
, fromEvent
, toUIEvent
, toEvent
, key
, code
, locationIndex
, location
, KeyLocation(..)
, toEnumKeyLocation
, fromEnumKeyLocation
, ctrlKey
, shiftKey
, altKey
, metaKey
, repeat
, isComposing
, getModifierState
) where
import Prelude
import Data.Enum (class BoundedEnum, class Enum, Cardinality(..), defaultPred, defaultSucc, toEnum)
import Data.Maybe (Maybe(..), fromJust)
import Effect (Effect)
import Unsafe.Coerce (unsafeCoerce)
import Web.Event.Event (Event)
import Web.Internal.FFI (unsafeReadProtoTagged)
import Web.UIEvent.UIEvent (UIEvent)
foreign import data KeyboardEvent :: Type
fromUIEvent :: UIEvent -> Maybe KeyboardEvent
fromUIEvent = unsafeReadProtoTagged "KeyboardEvent"
fromEvent :: Event -> Maybe KeyboardEvent
fromEvent = unsafeReadProtoTagged "KeyboardEvent"
toUIEvent :: KeyboardEvent -> UIEvent
toUIEvent = unsafeCoerce
toEvent :: KeyboardEvent -> Event
toEvent = unsafeCoerce
-- | A non-empty Unicode character string containing the printable representation
-- | of the key, if available.
foreign import key :: KeyboardEvent -> String
-- | Returns a string representing a physical key on the keyboard. Not
-- | affected by keyboard layout or state of the modifier keys.
foreign import code :: KeyboardEvent -> String
foreign import locationIndex :: KeyboardEvent -> Int
location :: Partial => KeyboardEvent -> KeyLocation
location = fromJust <<< toEnum <<< locationIndex
data KeyLocation
= Standard
| Left
| Right
| Numpad
derive instance eqKeyLocation :: Eq KeyLocation
derive instance ordKeyLocation :: Ord KeyLocation
instance boundedKeyLocation :: Bounded KeyLocation where
bottom = Standard
top = Numpad
instance enumKeyLocation :: Enum KeyLocation where
succ = defaultSucc toEnumKeyLocation fromEnumKeyLocation
pred = defaultPred toEnumKeyLocation fromEnumKeyLocation
instance boundedEnumKeyLocation :: BoundedEnum KeyLocation where
cardinality = Cardinality 4
toEnum = toEnumKeyLocation
fromEnum = fromEnumKeyLocation
toEnumKeyLocation :: Int -> Maybe KeyLocation
toEnumKeyLocation =
case _ of
0 -> Just Standard
1 -> Just Left
2 -> Just Right
3 -> Just Numpad
_ -> Nothing
fromEnumKeyLocation :: KeyLocation -> Int
fromEnumKeyLocation =
case _ of
Standard -> 0
Left -> 1
Right -> 2
Numpad -> 3
foreign import ctrlKey :: KeyboardEvent -> Boolean
foreign import shiftKey :: KeyboardEvent -> Boolean
foreign import altKey :: KeyboardEvent -> Boolean
foreign import metaKey :: KeyboardEvent -> Boolean
foreign import repeat :: KeyboardEvent -> Boolean
foreign import isComposing :: KeyboardEvent -> Boolean
foreign import getModifierState
:: String
-> KeyboardEvent
-> Effect Boolean