I’m using haskell and gtk2hs.
Is there any way to get keydown/keyup events (keystate is just as good; keypress event is not) with those?
I’m using haskell and gtk2hs.
Is there any way to get keydown/keyup events (keystate is just as good; keypress event is not) with those?
these imports will be necessary
import Graphics.UI.Gtk.Abstract.Widget
import Graphics.UI.Gtk.Gdk.Events
then this is how to respond to keyRelease Events (some simple example):
let handler event = do
case event of
(Key _ _ _ _ _ _ _ _ _ (Just char)) -> case char of
'a' -> doSomething
_ -> return () --or whatever type the other cases have
_ -> return ()
return True
onKeyRelease widget handler
the many ignore patterns would contain information that is not essential for simple event handlers.
Here’s how the state of a key can be tracked:
isPressedA <- newIORef False
let handler event = do
case event of
(Key released _ _ _ _ _ _ _ _ (Just char)) -> case char of
'a' -> writeIORef isPressedA (not released)
_ -> return ()
_ -> return ()
return True
onKeyPress window handler
onKeyRelease window handler
The released contains whether the key was released or pressed, so the same handler can work for both events.
I made this code respond to the window’s key events, rather than some widgets’, to avoid the keyPress and keyRelease registering on different widgets (it’s still possible though when switching windows between the events)
getting a keyDown event can be done by tracking whether there was a keyRelease since the last keyPress.