Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Differentiate between mouse and keyboard triggering onclick

I need to find a way to determine if a link has been activated via a mouse click or a keypress.

<a href="" onclick="submitData(event, '2011-07-04')">Save</a> 

The idea is that if they are using a mouse to hit the link then they can keep using the mouse to choose what they do next. But if they tabbing around the page and they tab to the Save link, then I'll open then next line for editing (the page is like a spreadsheet with each line becoming editable using ajax).

I thought the event parameter could be queried for which mouse button is pressed, but when no button is pressed the answer is 0 and that's the same as the left mouse button. They I thought I could get the keyCode from the event but that is coming back as undefined so I'm assuming a mouse event doesn't include that info.

function submitData(event, id) {     alert("key = "+event.keyCode + "  mouse button = "+event.button); } 

always returns "key = undefined mouse button = 0"

Can you help?

like image 762
Craig Avatar asked Sep 18 '11 22:09

Craig


People also ask

What is the use action setting differentiate between mouse click and mouse over?

The basic difference between a click and a drag event is the mouse movement. The mouse event that differentiates a click and drag event is the “mouse move” event. In a “click” event, there is no “mouse move” event. However, the “mouse down” and “mouse up” event remains the same for both click and drag.

How dragging is different from clicking of the mouse?

Using a pointing device, such as a mouse, to latch onto an icon on screen and move it to some other location. When the screen pointer (cursor) is over the icon of the object, the mouse button is clicked to grab it. The button is held down while the object is moved ("dragged") to its destination.


2 Answers

Could check if event.screenX and event.screenY are zero.

$('a#foo').click(function(evt) {   if (evt.screenX == 0 && evt.screenY == 0) {     window.alert('Keyboard click.');   } else {     window.alert('Mouse click.');   } }); 

Demo on CodePen

I couldn't find a guarantee that it works in all browsers and all cases, but it has the benefit of not trying to detect a "click" done via the keyboard. So this solution detects "click" more reliably at the cost of detecting if it's from keyboard or mouse somewhat less reliably. If you prefer the reverse, look as the answer from @Gonzalo.

Note: One place I found using this method is Chromium

like image 166
Wernight Avatar answered Sep 24 '22 20:09

Wernight


You can create a condition with event.type

function submitData(event, id) {     if(event.type == 'mousedown')     {         // do something         return;     }     if(event.type == 'keypress')     {         // do something else         return;     } } 

Note: You'll need to attach an event which supports both event types. With JQuery it would look something like $('a.save').bind('mousedown keypress', submitData(event, this));

The inline onClick="" will not help you as it will always pass that click event since that's how it's trapped.

EDIT: Here's a working demo to prove my case with native JavaScript: http://jsfiddle.net/AlienWebguy/HPEjt/

I used a button so it'd be easier to see the node highlighted during a tab focus, but it will work the same with any node.

like image 39
AlienWebguy Avatar answered Sep 25 '22 20:09

AlienWebguy