Search code examples
javascripthtmlbuttoninputtextinput

How to bind two keyboard keys like Ctrl +L in an input


I have one text input and one button (see below). How can I use JavaScript to trigger the button's click event when the some "Ctrl + Enter (other shortcut)" key is pressed?

<input type="text" id="txtSearch" />
<input type="button" id="btnSearch" value="Search" onclick="doSomething();" />

Solution

  • Those keys have are stored differently in the event values so you can check if they are both pressed like so

    if(event.keyCode == 13 && event.ctrlKey){
       //insert logic
    }
    

    Other keys can be accessed this way as well.