Mouse Events: Mouse Clicked?

54 views
Skip to first unread message

Will

unread,
Nov 19, 2010, 10:20:37 AM11/19/10
to NodeBox for OpenGL
Hello All!

It seems that this would be something that is simple to resolve, but I
was wondering if there is a simple way to create a "mouse clicked"
event in the canvas. I have been crawling through the nodebox/
graphics/context.py EventHandler class in order to figure out how
mouse events are currently handled, and at current (it seems) that
there are "pressed" and "dragged", and ways to access specific
buttons.

I would like to be able to register a single click (i.e. mouse down,
mouse release) -- I must be overlooking something really obvious here,
no?

Thanks _w

Tom De Smedt

unread,
Nov 19, 2010, 12:28:38 PM11/19/10
to NodeBox for OpenGL
You can use the on_mouse_release() event handler.
This event will fire after on_mouse_press(), if the mouse is still
inside the canvas/layer, when the mouse button is released. For layers
(e.g. a button), you may want to add an additional check to see if the
user has been dragging the layer (which is not a click event). The
example below illustrates this:

from nodebox.graphics import *

class Button(Layer):

color = Color(0,0,0,1)

def draw(self):
fill(self.color)
rect(0, 0, self.width, self.height)

def on_mouse_release(self, mouse):
if not self.dragged:
self.color = Color(random(), random(), random(), 1)
print "clicked", self.name

b1 = Button(name="b1", width=100, height=100)
b2 = Button(name="b2", width=100, height=100, x=100, y=100)
canvas.append(b1)
canvas.append(b2)
canvas.run()

A doubleclick is a bit more complicated (it requires a timer between
the two clicks). The Control class in gui/control.py has an
on_mouse_doubleclick() event which you can examine.

Will

unread,
Nov 19, 2010, 11:38:20 PM11/19/10
to NodeBox for OpenGL
Thanks Tom -- This was very helpful!
Reply all
Reply to author
Forward
0 new messages