Distinguish between single and double click

I’ve searched the forum, but can not find a solution.

Is it possible to have a single click action and a double click action on the same object?
I can get both to work but cant stop the single click event firing on a double click.

Check the event.clickCount property in your mouse event handler.

https://docs.inductiveautomation.com/display/DOC79/Component+Events

This is what i have:

if event.button == event.BUTTON1:
	if event.clickCount == 1:
		system.nav.openWindowInstance('Controls', {'title' : 'Device 1'});
	if event.clickCount == 2:
		system.nav.openWindowInstance('Setpoints', {'title' : 'Device 1'});
1 Like

Edit your post and put the code inside a preformatted text box by putting three back-ticks on a line before and a line after code. Or use this button:
image
This is important to make your code readable as indentation matters in Python.

Done. Thanks

1 Like

I believe you’ll need to do something along the lines of starting a timer on single click and executing the single click action when the timer finishes without a double-click occurring first:

if event.button == event.BUTTON1:
	if event.clickCount == 1:
		event.source.putClientProperty('singleClick', True)
		# Define single click action (replace system.gui.messageBox line with your action).
		def actionSingleClick():
			# Execute single click action only if double-click hasn't cleared single click flag.
			if event.source.getClientProperty('singleClick'):
				system.gui.messageBox('Single click')
		# Run actionSingleClick code in 300 ms to allow for double click to clear single click flag first.
		system.util.invokeLater(actionSingleClick, 300)
	if event.clickCount == 2:
		# Clear single click flag to prevent single click action.
		event.source.putClientProperty('singleClick', False)
		# Double click action (replace system.gui.messageBox line with your action).
		system.gui.messageBox('Double click')
3 Likes

IA already do this for you, as Phil said

Am I missing something? I attached the code I’m using in post #3 which is using event.clickCount
But when I perform a double click I get the single click action and the double click action

The code I posted above distinguishes between single and double-clicks, executing only one or the other. I’m also curious if there’s a simpler way.

Thanks. I’ll try your solution

1 Like

You don’t need the event = event in there btw: def actionSingleClick(event = event):

1 Like

True; it’s not needed as it’s available when the function is defined (would be needed if function was defined in a library instead). I’ll edit that out of the example above. Thanks!

1 Like

This might be too simplistic, but if you get both events firing, then maybe just putting the “if event.clickCount == 2” statement first would work.

1 Like

That’d be simpler, but unfortunately it doesn’t work as each double click generates two events:

  1. Event with clickCount of 1 for the first click of double click
  2. Event with clickCount of 2 for second click of double click

So the first event with clickCount of 1 will trigger the single click action regardless of code order and whether another event comes right after it with a clickCount of 2 or not.

I’m still waiting for enlightenment if there’s a simpler way to distinguish between the single and double clicks when you need to support both on the same item without firing the single click action for double clicks, but the code I posted above does the trick.

True. But it should be easy to pick and choose using an if . . . elif . . . else.

So I decided to test it out for myself. I created a “test string” and used the mouse clicked event on a button. This worked fine.

numClicks = event.clickCount

if numClicks == 2:
system.tag.write("[BlenderSim]Global Tags/Test String", “Double Click”);
else:
system.tag.write("[BlenderSim]Global Tags/Test String", “Single Click”);

And it is simple too.

Your code works as long as it doesn’t matter to your application that you get both the single click and double click actions for every double click. It writes “Single Click” and then “Double Click” to your test tag for every double click. You may not notice because it happens so fast.

The OP required a solution that prevents the single click action from occurring when there’s a double click. Because there’s always a single click event at the beginning of a double click, it must be filtered out if you don’t want to get both for each double click.

1 Like

Yes. I noticed it when I tried to add Window Nav.

Just out of curiosity, while this double click versus single click is good to know, for an application where you want an operator to distinguish between two or more navigation choices, have you considered a dropdown list?

Setting up like this did get it to work even for Window navigation. Since it is throwing out and acting on the single click event first, I do not think there would be a solution that could be a whole lot simpler. Thanks Witman.

1 Like