|
| 1 | +module DOM.Event.KeyboardEvent |
| 2 | + ( module T |
| 3 | + , eventToKeyboardEvent |
| 4 | + , key |
| 5 | + , code |
| 6 | + , locationIndex |
| 7 | + , location |
| 8 | + , KeyLocation(..) |
| 9 | + , toEnumKeyLocation |
| 10 | + , fromEnumKeyLocation |
| 11 | + , ctrlKey |
| 12 | + , shiftKey |
| 13 | + , altKey |
| 14 | + , metaKey |
| 15 | + , repeat |
| 16 | + , isComposing |
| 17 | + , getModifierState |
| 18 | + ) where |
| 19 | + |
| 20 | +import Prelude |
| 21 | +import Control.Monad.Eff (Eff) |
| 22 | +import Data.Enum (class BoundedEnum, class Enum, Cardinality(..), defaultPred, defaultSucc, toEnum) |
| 23 | +import Data.Foreign (F, toForeign) |
| 24 | +import Data.Maybe (Maybe(..), fromJust) |
| 25 | +import DOM (DOM) |
| 26 | +import DOM.Event.Types (Event, KeyboardEvent, readKeyboardEvent) |
| 27 | +import DOM.Event.Types (KeyboardEvent, keyboardEventToEvent, readKeyboardEvent) as T |
| 28 | + |
| 29 | +eventToKeyboardEvent :: Event -> F KeyboardEvent |
| 30 | +eventToKeyboardEvent = readKeyboardEvent <<< toForeign |
| 31 | + |
| 32 | +foreign import key :: KeyboardEvent -> String |
| 33 | + |
| 34 | +foreign import code :: KeyboardEvent -> String |
| 35 | + |
| 36 | +foreign import locationIndex :: KeyboardEvent -> Int |
| 37 | + |
| 38 | +location :: Partial => KeyboardEvent -> KeyLocation |
| 39 | +location = fromJust <<< toEnum <<< locationIndex |
| 40 | + |
| 41 | +data KeyLocation |
| 42 | + = Standard |
| 43 | + | Left |
| 44 | + | Right |
| 45 | + | Numpad |
| 46 | + |
| 47 | +derive instance eqKeyLocation :: Eq KeyLocation |
| 48 | +derive instance ordKeyLocation :: Ord KeyLocation |
| 49 | + |
| 50 | +instance boundedKeyLocation :: Bounded KeyLocation where |
| 51 | + bottom = Standard |
| 52 | + top = Numpad |
| 53 | + |
| 54 | +instance enumKeyLocation :: Enum KeyLocation where |
| 55 | + succ = defaultSucc toEnumKeyLocation fromEnumKeyLocation |
| 56 | + pred = defaultPred toEnumKeyLocation fromEnumKeyLocation |
| 57 | + |
| 58 | +instance boundedEnumKeyLocation :: BoundedEnum KeyLocation where |
| 59 | + cardinality = Cardinality 4 |
| 60 | + toEnum = toEnumKeyLocation |
| 61 | + fromEnum = fromEnumKeyLocation |
| 62 | + |
| 63 | +toEnumKeyLocation :: Int -> Maybe KeyLocation |
| 64 | +toEnumKeyLocation = |
| 65 | + case _ of |
| 66 | + 0 -> Just Standard |
| 67 | + 1 -> Just Left |
| 68 | + 2 -> Just Right |
| 69 | + 3 -> Just Numpad |
| 70 | + _ -> Nothing |
| 71 | + |
| 72 | +fromEnumKeyLocation :: KeyLocation -> Int |
| 73 | +fromEnumKeyLocation = |
| 74 | + case _ of |
| 75 | + Standard -> 0 |
| 76 | + Left -> 1 |
| 77 | + Right -> 2 |
| 78 | + Numpad -> 3 |
| 79 | + |
| 80 | +foreign import ctrlKey :: KeyboardEvent -> Boolean |
| 81 | + |
| 82 | +foreign import shiftKey :: KeyboardEvent -> Boolean |
| 83 | + |
| 84 | +foreign import altKey :: KeyboardEvent -> Boolean |
| 85 | + |
| 86 | +foreign import metaKey :: KeyboardEvent -> Boolean |
| 87 | + |
| 88 | +foreign import repeat :: KeyboardEvent -> Boolean |
| 89 | + |
| 90 | +foreign import isComposing :: KeyboardEvent -> Boolean |
| 91 | + |
| 92 | +foreign import getModifierState |
| 93 | + :: forall eff |
| 94 | + . String |
| 95 | + -> KeyboardEvent |
| 96 | + -> Eff (dom :: DOM | eff) Boolean |
0 commit comments