module Hbro.Keys (
stringify,
keyToString,
manageSequentialKeys
) where
import Hbro.Core
import Hbro.Types
import Hbro.Util
import Control.Monad hiding(forM_)
import Data.IORef
import Graphics.UI.Gtk.Abstract.Widget
import Graphics.UI.Gtk.Gdk.EventM
import Graphics.UI.Gtk.Gdk.Keys
import Prelude hiding(mapM_)
manageSequentialKeys :: (String -> K (String, Bool)) -> String -> K (String, Bool)
manageSequentialKeys handler keystroke = do
keysRef <- getState "Hbro.Keys.manageSequentialKeys" ""
keys <- io $ manageSequentialKeys' keysRef keystroke
(keys', result) <- handler keys
case result of
True -> (io . modifyIORef keysRef $ const []) >> return ([], result)
_ -> return (keys', result)
manageSequentialKeys' :: IORef String -> String -> IO String
manageSequentialKeys' previousKeys "<Escape>" = do
writeIORef previousKeys []
return []
manageSequentialKeys' previousKeys keystroke = do
modifyIORef previousKeys (++ keystroke)
return =<< readIORef previousKeys
keyToString :: KeyVal -> Maybe String
keyToString keyVal = case keyToChar keyVal of
Just ' ' -> Just "<Space>"
Just char -> Just [char]
_ -> case keyName keyVal of
"Caps_Lock" -> Nothing
"Shift_L" -> Nothing
"Shift_R" -> Nothing
"Control_L" -> Nothing
"Control_R" -> Nothing
"Alt_L" -> Nothing
"Alt_R" -> Nothing
"Super_L" -> Nothing
"Super_R" -> Nothing
"Menu" -> Nothing
"ISO_Level3_Shift" -> Nothing
"dead_circumflex" -> Just "^"
"dead_diaeresis" -> Just "ยจ"
x -> Just ('<':x ++ ">")
stringify :: Modifier -> String
stringify Control = "C-"
stringify Alt = "M-"
stringify _ = []