[removed] Check if CTRL button was pressed

后端 未结 3 1411
甜味超标
甜味超标 2020-12-03 13:06

I need to check if the CTRL button was pressed while I am clicking on a control on my html page using JavaScript.

How can I do this?

相关标签:
3条回答
  • 2020-12-03 13:42

    I use this and works fine

    <a  href="" onclick="return Details(event)" ></a>
    
    function Details(event) {
                if (event.ctrlKey) {
                    alert('Ctrl down');
                }
    }
    
    0 讨论(0)
  • 2020-12-03 13:45

    Try looking in the event object.

    e.g.

    document.body.onclick = function (e) {
       if (e.ctrlKey) {
          alert("ctr key was pressed during the click");
       }
    }
    <p>Click me, and sometimes hold CTRL down!</p>

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

    I did it using cntrlIsPressed global flag; also takes care of select all option using Control + A

    // Check whether control button is pressed
    $(document).keydown(function(event) {
        if (event.which == "17")
            cntrlIsPressed = true;
        else if (event.which == 65 && cntrlIsPressed) {
            // Cntrl+  A
            selectAllRows();
        }
    });
    
    $(document).keyup(function() {
        cntrlIsPressed = false;
    });
    
    var cntrlIsPressed = false;
    
    0 讨论(0)
提交回复
热议问题