Differentiate between mouse and keyboard triggering onclick

前端 未结 7 914
你的背包
你的背包 2020-12-03 13:09

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



        
相关标签:
7条回答
  • 2020-12-03 13:49

    You can differentiate between a click and a keyboard hit capturing and discarding the keydown event originated at the moment of the key press:

    jQuery(function($) {
        $("a#foo").keydown(function() {
            alert("keyboard");
            return false;
        }).click(function() {
            alert("mouse");
            return false;
        })
    })
    

    http://jsfiddle.net/NuP2g/

    0 讨论(0)
  • 2020-12-03 13:50

    You can use event.detail

    if(event.detail === 0) {
        // keypress
    } else {
        // mouse event
    }
    
    0 讨论(0)
  • 2020-12-03 13:52

    Wasn't able to come up with solution relying entirely on the events but you can position an anchor tag over a button and give it a tabindex of -1. This gives you a button that can be focused and engaged with keyboard enter/spacebar, as well as giving you a clickable surface that gives you an option to differentiate the two codepaths.

    .button {
      position: relative;
    }
    
    .anchor {
      position: absolute;
      top: 0;
      right: 0;
      bottom: 0;
      left: 0;
    }
    <button id="button" class="button">
      button
      <a class="anchor" href="#example" tabindex="-1"></a>
    </button>

    0 讨论(0)
  • 2020-12-03 13:55

    I know this is an old question but given how much time I lost looking for a working, no jquery and IE-compatible solution, I think it won't be a bad idea to put it here (where I came first).

    I tested this and found it working fine :

    let mouseDown = false;
    
    element.addEventListener('mousedown', () => {
      mouseDown = true;
    });
    
    element.addEventListener('mouseup', () => {
      mouseDown = false;
    });
    
    element.addEventListener('focus', (event) => {
      if (mouseDown) {
        // keyboard
      } else {
        // mouse
      }
    });
    

    Source link : https://www.darrenlester.com/blog/focus-only-on-tab

    0 讨论(0)
  • 2020-12-03 13:57

    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.

    0 讨论(0)
  • 2020-12-03 14:04

    Handle the mouseup event.
    If you get a click right afterwards, it was probably done with the mouse.

    0 讨论(0)
提交回复
热议问题