# IsMouseDown - drawing while button is pressed?

**URL:** <https://forum.xojo.com/t/ismousedown-drawing-while-button-is-pressed/59732>\
**Category:** General\
**Created:** [December 12, 2020, 12:04pm UTC](https://forum.xojo.com/t/ismousedown-drawing-while-button-is-pressed/59732 "2020-12-12T12:04:39Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jeff\_Tullin](https://forum.xojo.com/user_avatar/forum.xojo.com/jeff_tullin/32/693_2.png) [@Jeff\_Tullin](https://forum.xojo.com/u/Jeff_Tullin)\
**Post date:** [December 12, 2020, 12:04pm UTC](https://forum.xojo.com/t/ismousedown-drawing-while-button-is-pressed/59732/1 "2020-12-12T12:04:40Z")

</div>

In another post, I mentioned that a 64bit build of my WIndows app works when using 2015, but fails to invalidate the screen in compiled code from 2018  
Asked to produce a ‘simple example’, I have been forced to revisit some of the rubbish I had to go through years back to make ‘drawing under the control of the mouse’ happen at all.

You might imagine:

Click and drag on a canvas, drawing rectangles as you go

would be easy.  
But in practice, in the mousemove event, there is no ‘isMouseDown’ parameter.  
Keyboard.async… has nothing about it either

So you have to trap mouse down in the mousedown event, and remember.  
Then, in the mouse move event, only try to draw if a property like ‘mousewaspressed’ is true.

We can’t draw directly to the graphics of the canvas any more.  
So we create and draw on an offscreen picture, and draw that to the canvas in the paint event.

However, even when calling Invalidate, the invalidation/redrawing doesn’t happen WHILE THE MOUSE IS PRESSED.

The whole thing feels back to front to me.  
Stuff is different on Windows, but this is on Mac

1/ I should be able to detect if the mouse is down during a Mouse Move. _(it’s part of the WM\_MOUSEMOVE message handling on Windows - mouse events and keyboard CTRL/SHIFT states are parameters)_

2/ I should be able to make the canvas refresh the area under the mouse if the button is down while moving

---

<div class="post-metadata">

**Author:** ![DerkJ](https://forum.xojo.com/letter_avatar_proxy/v4/letter/d/e495f1/32.png) [@DerkJ](https://forum.xojo.com/u/DerkJ)\
**Post date:** [December 12, 2020, 1:08pm UTC](https://forum.xojo.com/t/ismousedown-drawing-while-button-is-pressed/59732/2 "2020-12-12T13:08:24Z")

</div>

Add a property MouseIsDown to your canvas.  
in MouseDown set it to true and return true

In mouseMove you could now do the drawing (set flags or whatever) ?

in MouseUp set MouseIsDown to false…

---

<div class="post-metadata">

**Author:** ![MarkusR](https://forum.xojo.com/user_avatar/forum.xojo.com/markusr/32/685_2.png) [@MarkusR](https://forum.xojo.com/u/MarkusR)\
**Post date:** [December 12, 2020, 1:36pm UTC](https://forum.xojo.com/t/ismousedown-drawing-while-button-is-pressed/59732/3 "2020-12-12T13:36:54Z")

</div>

there is also a System.MouseDown. for right button IsContextualClick

---

<div class="post-metadata">

**Author:** ![Sam\_Rowlands](https://forum.xojo.com/user_avatar/forum.xojo.com/sam_rowlands/32/265_2.png) [@Sam\_Rowlands](https://forum.xojo.com/u/Sam_Rowlands)\
**Post date:** [December 12, 2020, 1:39pm UTC](https://forum.xojo.com/t/ismousedown-drawing-while-button-is-pressed/59732/4 "2020-12-12T13:39:11Z")

</div>

What about the mouseDrag event of a canvas?  
[https://documentation.xojo.com/MouseDrag\_event](https://documentation.xojo.com/MouseDrag_event)

---

<div class="post-metadata">

**Author:** ![Jeff\_Tullin](https://forum.xojo.com/user_avatar/forum.xojo.com/jeff_tullin/32/693_2.png) [@Jeff\_Tullin](https://forum.xojo.com/u/Jeff_Tullin)\
**Post date:** [December 12, 2020, 1:41pm UTC](https://forum.xojo.com/t/ismousedown-drawing-while-button-is-pressed/59732/5 "2020-12-12T13:41:33Z")

</div>

> Add a property MouseIsDown to your canvas.  
> in MouseDown set it to true and return true
> 
> In mouseMove you could now do the drawing (set flags or whatever) ?
> 
> in MouseUp set MouseIsDown to false…

Yeah,I know.  
Doesn’t work.  
The canvas does not update while the mouse button is down and the mouse is moved.

A window with a canvas, a window property of type picture (theStaticBit, initialised in Open), and a property of bDrawing (boolean)

```
Canvas1 Mousedown:
bDrawing = True

```

If I add return = false or omit it, Mouseup never fires.  
Nothing is drawn while mouse is down, but stuff does draw while mouse is moved but not pressed

If Return = true, nothing happens in Mousemove at all, but mouseup does fire

```
Canvas1 Mousemove:
If bdrawing Then
  theStaticBit.Graphics.fillrect x-20,y-20,40,40
  canvas1.invalidate  
End If

```

This is the mouseup

```
MouseUP:
bdrawing = false

```

The paint event…just draws what the offscreen picture holds

```
Canvas1 Paint:

g.DrawPicture theStaticBit,0,0

```

---

<div class="post-metadata">

**Author:** ![MarkusR](https://forum.xojo.com/user_avatar/forum.xojo.com/markusr/32/685_2.png) [@MarkusR](https://forum.xojo.com/u/MarkusR)\
**Post date:** [December 12, 2020, 1:42pm UTC](https://forum.xojo.com/t/ismousedown-drawing-while-button-is-pressed/59732/6 "2020-12-12T13:42:55Z")

</div>

MouseMove event not come at windows if you hold the mouse button.  
Add  
System.DebugLog CurrentMethodName

---

<div class="post-metadata">

**Author:** ![Jeff\_Tullin](https://forum.xojo.com/user_avatar/forum.xojo.com/jeff_tullin/32/693_2.png) [@Jeff\_Tullin](https://forum.xojo.com/u/Jeff_Tullin)\
**Post date:** [December 12, 2020, 1:45pm UTC](https://forum.xojo.com/t/ismousedown-drawing-while-button-is-pressed/59732/7 "2020-12-12T13:45:57Z")

</div>

> [@MarkusR](#):
>
> MouseMove event not come at windows if you hold the mouse button.

Exactly.  
Bear in mind I’m ultimately trying to create a _simple_ example of why things don’t work in a 64bit windows build.  
These are problems I ‘solved’ years ago, but faced all over again when trying from a ‘bare bones’ project.

Mousedrag works if I return true in mousedown - (lthough to be fair, I dont consider that Im _dragging_ anything…)

On windows, I then need (ed?) to subclass the window in order to recreate the mousedrag stuff.

Gonna mark this as solved, in an attempt to close it. Its gone off track a bit, but the forum doesnt let me hide it or close it .

---

<div class="post-metadata">

**Author:** ![MarkusR](https://forum.xojo.com/user_avatar/forum.xojo.com/markusr/32/685_2.png) [@MarkusR](https://forum.xojo.com/u/MarkusR)\
**Post date:** [December 12, 2020, 1:56pm UTC](https://forum.xojo.com/t/ismousedown-drawing-while-button-is-pressed/59732/8 "2020-12-12T13:56:42Z")

</div>

i agree that realtime graphics in xojo with mouse interactions is very difficult.  
you can make life easier if you use your own mouse class.

if me not know when events come i put System.DebugLog in each.
