{-# LANGUAGE CPP #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE OverloadedStrings #-}
module Glazier.React.Event.Keyboard
( KeyboardEvent(..)
, toKeyboardEvent
)
where
import Control.DeepSeq
import qualified GHC.Generics as G
import qualified GHCJS.Types as J
import Glazier.React.Notice.Internal
import qualified JavaScript.Extras as JE
import Prelude hiding (repeat)
data KeyboardEvent = KeyboardEvent
{ altKey :: Bool
, charCode ::Int
, ctrlKey :: Bool
, getModifierState :: J.JSString -> Bool
, key :: J.JSString
, keyCode :: Int
, locale :: J.JSString
, location ::Int
, metaKey :: Bool
, repeat :: Bool
, shiftkey :: Bool
, which :: Int
}
deriving (G.Generic)
instance NFData KeyboardEvent
toKeyboardEvent :: Notice -> Maybe KeyboardEvent
toKeyboardEvent (Notice (JE.JSRep evt)) | js_isKeyboardEvent (unsafeGetProperty evt "nativeEvent") = Just $
KeyboardEvent
{ altKey = unsafeGetProperty evt "altKey"
, charCode = unsafeGetProperty evt "charCode"
, ctrlKey = unsafeGetProperty evt "ctrlKey"
, getModifierState = unsafeGetModifierState evt
, key = unsafeGetProperty evt "key"
, keyCode = unsafeGetProperty evt "keyCode"
, locale = unsafeGetProperty evt "locale"
, location = unsafeGetProperty evt "location"
, metaKey = unsafeGetProperty evt "metaKey"
, repeat = unsafeGetProperty evt "repeat"
, shiftkey = unsafeGetProperty evt "shiftkey"
, which = unsafeGetProperty evt "which"
}
toKeyboardEvent _ | otherwise = Nothing
#ifdef __GHCJS__
foreign import javascript unsafe
"($1 instanceof KeyboardEvent)"
js_isKeyboardEvent :: J.JSVal -> Bool
#else
js_isKeyboardEvent :: J.JSVal -> Bool
js_isKeyboardEvent _ = False
#endif