module System.Win32.Automation.Input
( module System.Win32.Automation.Input
, module System.Win32.Automation.Input.Key
, module System.Win32.Automation.Input.Mouse
) where
import Data.Bits ( (.|.) )
import Foreign.Ptr ( Ptr )
import Foreign.Storable ( Storable(..) )
import Foreign.Marshal.Array ( withArrayLen )
import Foreign.C.Types ( CIntPtr(..) )
import Graphics.Win32.Key ( VKey, c_MapVirtualKey )
import System.Win32.Automation.Input.Key
import System.Win32.Automation.Input.Mouse ( MOUSEINPUT )
import System.Win32.Automation.Input.Mouse hiding ( MOUSEINPUT(..) )
import System.Win32.Types ( UINT, LPARAM, failIfZero )
import System.Win32.Word ( DWORD, WORD )
#include "windows_cconv.h"
sendInput :: [INPUT] -> IO UINT
sendInput input
= withArrayLen input $ \len c_input ->
sendInputPtr len c_input
sendInputPtr :: Int -> Ptr INPUT -> IO UINT
sendInputPtr len c_input
= failIfZero "SendInput" $
c_SendInput (fromIntegral len) c_input $ sizeOf (undefined :: INPUT)
foreign import WINDOWS_CCONV unsafe "windows.h SendInput"
c_SendInput :: UINT -> LPINPUT -> Int -> IO UINT
makeKeyboardInput :: VKey -> Maybe DWORD -> IO INPUT
makeKeyboardInput vkey flag = do
let flag' = maybe kEYEVENTF_EXTENDEDKEY (kEYEVENTF_EXTENDEDKEY .|.) flag
scan <- c_MapVirtualKey vkey 0
dwExtraInfo' <- getMessageExtraInfo
return $ Keyboard
$ KEYBDINPUT {
wVk = fromIntegral vkey
, wScan = fromIntegral scan
, dwFlags = flag'
, time = 0
, dwExtraInfo = fromIntegral $ dwExtraInfo'
}
type PINPUT = Ptr INPUT
type LPINPUT = Ptr INPUT
data INPUT = Mouse MOUSEINPUT | Keyboard KEYBDINPUT | OtherHardware HARDWAREINPUT
deriving Show
instance Storable INPUT where
sizeOf = const (40)
alignment _ = 8
poke buf (Mouse mouse) = do
((\hsc_ptr -> pokeByteOff hsc_ptr 0)) buf (0:: DWORD)
((\hsc_ptr -> pokeByteOff hsc_ptr 8)) buf mouse
poke buf (Keyboard key) = do
((\hsc_ptr -> pokeByteOff hsc_ptr 0)) buf (1 :: DWORD)
((\hsc_ptr -> pokeByteOff hsc_ptr 8)) buf key
poke buf (OtherHardware hard) = do
((\hsc_ptr -> pokeByteOff hsc_ptr 0)) buf (2 :: DWORD)
((\hsc_ptr -> pokeByteOff hsc_ptr 8)) buf hard
peek buf = do
type' <- ((\hsc_ptr -> peekByteOff hsc_ptr 0)) buf :: IO DWORD
case type' of
0 ->
Mouse `fmap` ((\hsc_ptr -> peekByteOff hsc_ptr 8)) buf
1 ->
Keyboard `fmap` ((\hsc_ptr -> peekByteOff hsc_ptr 8)) buf
_ -> OtherHardware `fmap` ((\hsc_ptr -> peekByteOff hsc_ptr 8)) buf
type PHARDWAREINPUT = Ptr HARDWAREINPUT
data HARDWAREINPUT = HARDWAREINPUT
{ uMsg :: DWORD
, wParamL :: WORD
, wParamH :: WORD
} deriving Show
instance Storable HARDWAREINPUT where
sizeOf = const (8)
alignment _ = 4
poke buf input = do
((\hsc_ptr -> pokeByteOff hsc_ptr 0)) buf (uMsg input)
((\hsc_ptr -> pokeByteOff hsc_ptr 4)) buf (wParamL input)
((\hsc_ptr -> pokeByteOff hsc_ptr 6)) buf (wParamH input)
peek buf = do
uMsg' <- ((\hsc_ptr -> peekByteOff hsc_ptr 0)) buf
wParamL' <- ((\hsc_ptr -> peekByteOff hsc_ptr 4)) buf
wParamH' <- ((\hsc_ptr -> peekByteOff hsc_ptr 6)) buf
return $ HARDWAREINPUT uMsg' wParamL' wParamH'
foreign import WINDOWS_CCONV unsafe "windows.h GetMessageExtraInfo"
getMessageExtraInfo :: IO LPARAM
foreign import WINDOWS_CCONV unsafe "windows.h SetMessageExtraInfo"
setMessageExtraInfo :: LPARAM -> IO LPARAM