diff --git a/src/Puppeteer.Keyboard.purs b/src/Puppeteer.Keyboard.purs index 89e588c..0ccc3fc 100644 --- a/src/Puppeteer.Keyboard.purs +++ b/src/Puppeteer.Keyboard.purs @@ -1,11 +1,16 @@ -module Puppeteer.Keyboard (module X, KeyMod(..), Key(..), prepareKey, up, down, doType, press) where +module Puppeteer.Keyboard (module X, KeyMod(..), Key(..), prepareKey, keyModToString, keyModFromString, keyToString, keyFromString, up, down, doType, press) where import Prelude import Control.Promise (Promise) import Control.Promise as Promise +import Data.Bounded.Generic (genericBottom, genericTop) +import Data.Enum (class BoundedEnum, class Enum, upFrom) +import Data.Enum.Generic (genericCardinality, genericFromEnum, genericPred, genericSucc, genericToEnum) import Data.Eq.Generic (genericEq) +import Data.Foldable (find) import Data.Generic.Rep (class Generic) +import Data.Maybe (Maybe) import Data.Nullable (Nullable) import Data.Nullable as Nullable import Data.Ord.Generic (genericCompare) @@ -55,11 +60,25 @@ data KeyMod | KeyModControl | KeyModAlt -derive instance genericKeyMod :: Generic KeyMod _ -instance ordKeyMod :: Ord KeyMod where +derive instance Generic KeyMod _ + +instance Enum KeyMod where + pred = genericPred + succ = genericSucc + +instance Bounded KeyMod where + top = genericTop + bottom = genericBottom + +instance BoundedEnum KeyMod where + cardinality = genericCardinality + toEnum = genericToEnum + fromEnum = genericFromEnum + +instance Ord KeyMod where compare = genericCompare -instance eqKeyMod :: Eq KeyMod where +instance Eq KeyMod where eq = genericEq data Key @@ -164,128 +183,148 @@ data Key | KeyNumpad9 | KeyNumpadDecimal -derive instance genericKey :: Generic Key _ -instance ordKey :: Ord Key where +derive instance Generic Key _ + +instance Enum Key where + pred = genericPred + succ = genericSucc + +instance Bounded Key where + top = genericTop + bottom = genericBottom + +instance BoundedEnum Key where + cardinality = genericCardinality + toEnum = genericToEnum + fromEnum = genericFromEnum + +instance Ord Key where compare = genericCompare -instance eqKey :: Eq Key where +instance Eq Key where eq = genericEq prepareKey :: Key -> Foreign -prepareKey = writeImpl <<< prepareKey_ +prepareKey = writeImpl <<< keyToString -prepareKeyMod_ :: KeyMod -> String -prepareKeyMod_ KeyModMetaLeft = "MetaLeft" -prepareKeyMod_ KeyModMetaRight = "MetaRight" -prepareKeyMod_ KeyModMeta = "Meta" -prepareKeyMod_ KeyModShiftLeft = "ShiftLeft" -prepareKeyMod_ KeyModShiftRight = "ShiftRight" -prepareKeyMod_ KeyModControlLeft = "ControlLeft" -prepareKeyMod_ KeyModControlRight = "ControlRight" -prepareKeyMod_ KeyModAltLeft = "AltLeft" -prepareKeyMod_ KeyModAltRight = "AltRight" -prepareKeyMod_ KeyModShift = "Shift" -prepareKeyMod_ KeyModControl = "Control" -prepareKeyMod_ KeyModAlt = "Alt" +keyModFromString :: String -> Maybe KeyMod +keyModFromString s = find (eq s <<< keyModToString) (upFrom bottom :: Array KeyMod) -prepareKey_ :: Key -> String -prepareKey_ (KeyChar c) = String.fromCodePointArray [ CodePoint.codePointFromChar c ] -prepareKey_ (KeyMod mod) = prepareKeyMod_ mod -prepareKey_ KeyPower = "Power" -prepareKey_ KeyEject = "Eject" -prepareKey_ KeyAbort = "Abort" -prepareKey_ KeyHelp = "Help" -prepareKey_ KeyBackspace = "Backspace" -prepareKey_ KeyTab = "Tab" -prepareKey_ KeyEnter = "Enter" -prepareKey_ KeyCarriageReturn = "CarriageReturn" -prepareKey_ KeyNewline = "Newline" -prepareKey_ KeyPause = "Pause" -prepareKey_ KeyCapsLock = "CapsLock" -prepareKey_ KeyEscape = "Escape" -prepareKey_ KeyConvert = "Convert" -prepareKey_ KeyNonConvert = "NonConvert" -prepareKey_ KeySpace = "Space" -prepareKey_ KeyPageUp = "PageUp" -prepareKey_ KeyPageDown = "PageDown" -prepareKey_ KeyEnd = "End" -prepareKey_ KeyHome = "Home" -prepareKey_ KeyArrowLeft = "ArrowLeft" -prepareKey_ KeyArrowUp = "ArrowUp" -prepareKey_ KeyArrowRight = "ArrowRight" -prepareKey_ KeyArrowDown = "ArrowDown" -prepareKey_ KeySelect = "Select" -prepareKey_ KeyOpen = "Open" -prepareKey_ KeyPrintScreen = "PrintScreen" -prepareKey_ KeyInsert = "Insert" -prepareKey_ KeyDelete = "Delete" -prepareKey_ KeyContextMenu = "ContextMenu" -prepareKey_ KeyF1 = "F1" -prepareKey_ KeyF2 = "F2" -prepareKey_ KeyF3 = "F3" -prepareKey_ KeyF4 = "F4" -prepareKey_ KeyF5 = "F5" -prepareKey_ KeyF6 = "F6" -prepareKey_ KeyF7 = "F7" -prepareKey_ KeyF8 = "F8" -prepareKey_ KeyF9 = "F9" -prepareKey_ KeyF10 = "F10" -prepareKey_ KeyF11 = "F11" -prepareKey_ KeyF12 = "F12" -prepareKey_ KeyF13 = "F13" -prepareKey_ KeyF14 = "F14" -prepareKey_ KeyF15 = "F15" -prepareKey_ KeyF16 = "F16" -prepareKey_ KeyF17 = "F17" -prepareKey_ KeyF18 = "F18" -prepareKey_ KeyF19 = "F19" -prepareKey_ KeyF20 = "F20" -prepareKey_ KeyF21 = "F21" -prepareKey_ KeyF22 = "F22" -prepareKey_ KeyF23 = "F23" -prepareKey_ KeyF24 = "F24" -prepareKey_ KeyNumLock = "NumLock" -prepareKey_ KeyScrollLock = "ScrollLock" -prepareKey_ KeyAudioVolumeMute = "AudioVolumeMute" -prepareKey_ KeyAudioVolumeDown = "AudioVolumeDown" -prepareKey_ KeyAudioVolumeUp = "AudioVolumeUp" -prepareKey_ KeyMediaTrackNext = "MediaTrackNext" -prepareKey_ KeyMediaTrackPrevious = "MediaTrackPrevious" -prepareKey_ KeyMediaStop = "MediaStop" -prepareKey_ KeyMediaPlayPause = "MediaPlayPause" -prepareKey_ KeyAltGraph = "AltGraph" -prepareKey_ KeyProps = "Props" -prepareKey_ KeyCancel = "Cancel" -prepareKey_ KeyClear = "Clear" -prepareKey_ KeyAccept = "Accept" -prepareKey_ KeyModeChange = "ModeChange" -prepareKey_ KeyAttn = "Attn" -prepareKey_ KeyCrSel = "CrSel" -prepareKey_ KeyExSel = "ExSel" -prepareKey_ KeyEraseEof = "EraseEof" -prepareKey_ KeyPlay = "Play" -prepareKey_ KeyZoomOut = "ZoomOut" -prepareKey_ KeySoftLeft = "SoftLeft" -prepareKey_ KeySoftRight = "SoftRight" -prepareKey_ KeyCamera = "Camera" -prepareKey_ KeyCall = "Call" -prepareKey_ KeyEndCall = "EndCall" -prepareKey_ KeyVolumeDown = "VolumeDown" -prepareKey_ KeyVolumeUp = "VolumeUp" -prepareKey_ KeyNumpadEnter = "NumpadEnter" -prepareKey_ KeyNumpadMultiply = "NumpadMultiply" -prepareKey_ KeyNumpadAdd = "NumpadAdd" -prepareKey_ KeyNumpadSubtract = "NumpadSubtract" -prepareKey_ KeyNumpadDivide = "NumpadDivide" -prepareKey_ KeyNumpadEqual = "NumpadEqual" -prepareKey_ KeyNumpad0 = "Numpad0" -prepareKey_ KeyNumpad1 = "Numpad1" -prepareKey_ KeyNumpad2 = "Numpad2" -prepareKey_ KeyNumpad3 = "Numpad3" -prepareKey_ KeyNumpad4 = "Numpad4" -prepareKey_ KeyNumpad5 = "Numpad5" -prepareKey_ KeyNumpad6 = "Numpad6" -prepareKey_ KeyNumpad7 = "Numpad7" -prepareKey_ KeyNumpad8 = "Numpad8" -prepareKey_ KeyNumpad9 = "Numpad9" -prepareKey_ KeyNumpadDecimal = "NumpadDecimal" +keyFromString :: String -> Maybe Key +keyFromString s = find (eq s <<< keyToString) (upFrom bottom :: Array Key) + +keyModToString :: KeyMod -> String +keyModToString KeyModMetaLeft = "MetaLeft" +keyModToString KeyModMetaRight = "MetaRight" +keyModToString KeyModMeta = "Meta" +keyModToString KeyModShiftLeft = "ShiftLeft" +keyModToString KeyModShiftRight = "ShiftRight" +keyModToString KeyModControlLeft = "ControlLeft" +keyModToString KeyModControlRight = "ControlRight" +keyModToString KeyModAltLeft = "AltLeft" +keyModToString KeyModAltRight = "AltRight" +keyModToString KeyModShift = "Shift" +keyModToString KeyModControl = "Control" +keyModToString KeyModAlt = "Alt" + +keyToString :: Key -> String +keyToString (KeyChar c) = String.fromCodePointArray [ CodePoint.codePointFromChar c ] +keyToString (KeyMod mod) = keyModToString mod +keyToString KeyPower = "Power" +keyToString KeyEject = "Eject" +keyToString KeyAbort = "Abort" +keyToString KeyHelp = "Help" +keyToString KeyBackspace = "Backspace" +keyToString KeyTab = "Tab" +keyToString KeyEnter = "Enter" +keyToString KeyCarriageReturn = "CarriageReturn" +keyToString KeyNewline = "Newline" +keyToString KeyPause = "Pause" +keyToString KeyCapsLock = "CapsLock" +keyToString KeyEscape = "Escape" +keyToString KeyConvert = "Convert" +keyToString KeyNonConvert = "NonConvert" +keyToString KeySpace = "Space" +keyToString KeyPageUp = "PageUp" +keyToString KeyPageDown = "PageDown" +keyToString KeyEnd = "End" +keyToString KeyHome = "Home" +keyToString KeyArrowLeft = "ArrowLeft" +keyToString KeyArrowUp = "ArrowUp" +keyToString KeyArrowRight = "ArrowRight" +keyToString KeyArrowDown = "ArrowDown" +keyToString KeySelect = "Select" +keyToString KeyOpen = "Open" +keyToString KeyPrintScreen = "PrintScreen" +keyToString KeyInsert = "Insert" +keyToString KeyDelete = "Delete" +keyToString KeyContextMenu = "ContextMenu" +keyToString KeyF1 = "F1" +keyToString KeyF2 = "F2" +keyToString KeyF3 = "F3" +keyToString KeyF4 = "F4" +keyToString KeyF5 = "F5" +keyToString KeyF6 = "F6" +keyToString KeyF7 = "F7" +keyToString KeyF8 = "F8" +keyToString KeyF9 = "F9" +keyToString KeyF10 = "F10" +keyToString KeyF11 = "F11" +keyToString KeyF12 = "F12" +keyToString KeyF13 = "F13" +keyToString KeyF14 = "F14" +keyToString KeyF15 = "F15" +keyToString KeyF16 = "F16" +keyToString KeyF17 = "F17" +keyToString KeyF18 = "F18" +keyToString KeyF19 = "F19" +keyToString KeyF20 = "F20" +keyToString KeyF21 = "F21" +keyToString KeyF22 = "F22" +keyToString KeyF23 = "F23" +keyToString KeyF24 = "F24" +keyToString KeyNumLock = "NumLock" +keyToString KeyScrollLock = "ScrollLock" +keyToString KeyAudioVolumeMute = "AudioVolumeMute" +keyToString KeyAudioVolumeDown = "AudioVolumeDown" +keyToString KeyAudioVolumeUp = "AudioVolumeUp" +keyToString KeyMediaTrackNext = "MediaTrackNext" +keyToString KeyMediaTrackPrevious = "MediaTrackPrevious" +keyToString KeyMediaStop = "MediaStop" +keyToString KeyMediaPlayPause = "MediaPlayPause" +keyToString KeyAltGraph = "AltGraph" +keyToString KeyProps = "Props" +keyToString KeyCancel = "Cancel" +keyToString KeyClear = "Clear" +keyToString KeyAccept = "Accept" +keyToString KeyModeChange = "ModeChange" +keyToString KeyAttn = "Attn" +keyToString KeyCrSel = "CrSel" +keyToString KeyExSel = "ExSel" +keyToString KeyEraseEof = "EraseEof" +keyToString KeyPlay = "Play" +keyToString KeyZoomOut = "ZoomOut" +keyToString KeySoftLeft = "SoftLeft" +keyToString KeySoftRight = "SoftRight" +keyToString KeyCamera = "Camera" +keyToString KeyCall = "Call" +keyToString KeyEndCall = "EndCall" +keyToString KeyVolumeDown = "VolumeDown" +keyToString KeyVolumeUp = "VolumeUp" +keyToString KeyNumpadEnter = "NumpadEnter" +keyToString KeyNumpadMultiply = "NumpadMultiply" +keyToString KeyNumpadAdd = "NumpadAdd" +keyToString KeyNumpadSubtract = "NumpadSubtract" +keyToString KeyNumpadDivide = "NumpadDivide" +keyToString KeyNumpadEqual = "NumpadEqual" +keyToString KeyNumpad0 = "Numpad0" +keyToString KeyNumpad1 = "Numpad1" +keyToString KeyNumpad2 = "Numpad2" +keyToString KeyNumpad3 = "Numpad3" +keyToString KeyNumpad4 = "Numpad4" +keyToString KeyNumpad5 = "Numpad5" +keyToString KeyNumpad6 = "Numpad6" +keyToString KeyNumpad7 = "Numpad7" +keyToString KeyNumpad8 = "Numpad8" +keyToString KeyNumpad9 = "Numpad9" +keyToString KeyNumpadDecimal = "NumpadDecimal"