Button events - how to tell difference between mouse click and pressing enter

I placed a perspective button component on a view. I want to detect only mouse clicks, not enter presses. It appears that pressing the ENTER key and clicking with the mouse generates the same events.

I tested the behavior by creating and event script in the following 4 events:

  • onActionPeformed
  • onClick
  • onKeyDown
  • onKeyUp
def runAction(self, event):
	system.perspective.print(self.name + 'XXXXX')

Where I replaced XXX with the event name.

I noticed that when the button has focus and I press the ENTER key it generates both a "onClick" and 'onActionPerformed' event. It does not generate a "onKeyDown" event. It will generate an "onKeyUp" event if I have the "onClick" and "onActionPerformed" actions disabled.
I would like to raise the "onClick" event ONLY when the mouse clicks the button, not when the enter button is pressed.

Is this possible ?

You could probably filter out the enter key with one of the event properties.
onClick Event Script Example:

#def runAction(self, event):
	if event.detail > 0:
		#do enter keyless work here

That's a good suggestion and I poked around for something to use as a filter in the MouseEvent. Unfortunately the MouseEvent does not have anything that appears to contain information about the enter key being pressed. I specifically tested the 'button' and 'buttons' event but these appear to be related only to the buttons on the mouse, not the keyboard. No other event seemed like it would contain enter button pressed details although I did not test every event.


event.detail and all coordinate properties will be zero on enter press. When it's a mouse click, event.detail will be 1, and all coordinate parameters will be relative to mouse cursor position (probably not zero).

I imagine the if statement I provided will work for your usage case exactly as it is written.

I would caution that this breaks what the majority of users will expect. Generally, users are going to expect a button with focus to behave the same regardless of if they click on it with a mouse or press the enter key.

Why do you want to block Enter Key presses?


I would agree this is an antipattern, but you can prevent the default behavior of the browser by:

  1. Adding an onKeyDown handler
  2. Checking the 'Prevent Default' box in the options

From there, clicking the button will send onClick but pressing enter or spacebar will not.

Worked like a charm !
Thanks for quick response.

I am assuming that you mean the steps that @PGriffith listed? You should probably mark his reply as the solution.