module Main where
import Control.Monad.IO.Class
import Graphics.UI.Gtk
main :: IO ()
main = do
initGUI
window <- windowNew
canvas <- drawingAreaNew
widgetAddEvents canvas [Button1MotionMask]
canvas `on` motionNotifyEvent $ do
c <- eventCoordinates
liftIO $ print c
return False
containerAdd window canvas
widgetShowAll window
mainGUI
In the code above, I am trying to handle a 'mouse drag' on a GtkDrawingArea
, where the left mouse button is depressed. However, nothing is printed, indicating that the event is never firing. Even stranger, when I change Button1MotionMask
to PointerMotionMask
, the event fires when I move my mouse normally (as expected), but not when I move the mouse while depressing the left mouse button. What is going on here? I am using the gtk3-0.14.8
package on Windows 10.
EDIT: I should probably be a bit clearer about my problem. When I hold the left mouse button down while moving the mouse, motionNotifyEvent
does not fire. This does not depend on whether I have added PointerMotionMask
or Button1MotionMask
.
It doesn't seem to be mentioned in the documentation, but it turns out that you also need to enable ButtonPressMask
to recieve any event with the mouse button pressed - including motion events which occur when the button is pressed down. This results in the paradoxical behaviour where enabling only Button1MotionMask
results in no events being received unless you also enable ButtonPressMask
. I finally figured this out after discovering the gtk-demo sample application - it's very useful for learning GTK!