Skip to content

Commit a6a9fb9

Browse files
committed
Create label text with keyboard.
1 parent 3328050 commit a6a9fb9

File tree

3 files changed

+239
-101
lines changed

3 files changed

+239
-101
lines changed

gui/GtkGui.hs

+21-16
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import Control.Monad.IO.Class (MonadIO)
1919
import Data.GI.Base (AttrOp ((:=)), new)
2020
import Data.IORef (IORef, newIORef)
2121
import qualified Data.Map.Strict as Map
22-
import Data.Maybe (fromJust)
22+
import Data.Maybe (fromJust, fromMaybe)
2323
import Data.Text (Text)
2424
import Data.Time.Clock.System (getSystemTime)
2525
import Data.Word (Word16)
@@ -32,6 +32,7 @@ import GuiInternals
3232
( AppState,
3333
Inputs,
3434
KeyEvent (..),
35+
KeyInput (..),
3536
MouseButton (..),
3637
backgroundPress,
3738
emptyAppState,
@@ -119,15 +120,17 @@ backgroundPressCallback inputsRef stateRef eventButton = do
119120
decodeKey :: Maybe Text -> Word16 -> KeyEvent
120121
decodeKey mKeyStr keyCode =
121122
case mKeyStr of
122-
Nothing -> UnknownKey
123-
Just keyStr -> case Map.lookup keyStr keyStrings of
124-
Just e -> e
125-
Nothing -> case Map.lookup keyCode keyCodes of
126-
Just e -> e
127-
Nothing -> UnknownKey
128-
129-
keyPressCallback :: IORef Inputs -> IORef AppState -> Gdk.EventKey -> IO Bool
130-
keyPressCallback inputsRef stateRef eventKey = do
123+
Nothing -> OtherKey ""
124+
Just keyStr ->
125+
fromMaybe
126+
( fromMaybe
127+
(OtherKey keyStr)
128+
(Map.lookup keyCode keyCodes)
129+
)
130+
(Map.lookup keyStr keyStrings)
131+
132+
getKeyInput :: MonadIO m => Gdk.EventKey -> m KeyInput
133+
getKeyInput eventKey = do
131134
-- TODO May want to check that ctrl is pressed by checking that
132135
-- getEventKeyState is ModifierTypeControlMask. May also want to use
133136
-- Gdk.KEY_?.
@@ -141,20 +144,22 @@ keyPressCallback inputsRef stateRef eventKey = do
141144
keyCode <- Gdk.getEventKeyHardwareKeycode eventKey
142145
-- putStrLn $ "key: " <> show mKey
143146
-- putStrLn $ "keycode: " <> show keyCode
144-
let keyEvent = decodeKey mKey keyCode
147+
pure $ KeyInput (fromMaybe "" mKey) (decodeKey mKey keyCode)
148+
149+
keyPressCallback :: IORef Inputs -> IORef AppState -> Gdk.EventKey -> IO Bool
150+
keyPressCallback inputsRef stateRef eventKey = do
145151
-- print keyEvent
146-
keyPress inputsRef stateRef keyEvent
152+
keyInput <- getKeyInput eventKey
153+
keyPress inputsRef stateRef keyInput
147154
pure Gdk.EVENT_STOP
148155

149156
keyReleaseCallback :: IORef Inputs -> Gdk.EventKey -> IO Bool
150157
keyReleaseCallback inputsRef eventKey = do
151158
-- TODO May want to check that ctrl is pressed by checking that
152159
-- getEventKeyState is ModifierTypeControlMask. May also want to use
153160
-- Gdk.KEY_?.
154-
mKey <- Gdk.getEventKeyString eventKey
155-
keyCode <- Gdk.getEventKeyHardwareKeycode eventKey
156-
let keyEvent = decodeKey mKey keyCode
157-
keyRelease inputsRef keyEvent
161+
keyInput <- getKeyInput eventKey
162+
keyRelease inputsRef keyInput
158163
pure Gdk.EVENT_STOP
159164

160165
scrollCallback :: IORef Inputs -> Gdk.EventScroll -> IO Bool

0 commit comments

Comments
 (0)