0

How do I handle multiple keys?

Like I have multiple saving options, and I want to handle one button at a time. I am not getting how to handle multiple saving buttons on an entire page. I am using jQuery in an ASP.NET MVC project. How can I do this?

I am using this code:

$(document).bind('keydown', 'ALT+S', function() {
    // Alt + S for saving shortcut
    if (e.keyId == '#btnSaveCompany') {
        // Here I want to handle a function for the key btnSaveCompany
        funSaveCreation();
    }
});

Here I want to handle the save button for a keyboard shortcut for saving. I want to handle multiple buttons through this.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Vivek Shukla
  • 1,535
  • 1
  • 13
  • 13
  • This is not how the `keydown` event handler works. Read the docs for more info: http://api.jquery.com/keydown – Rory McCrossan Feb 26 '16 at 11:25
  • i am getting this but problem it that ,in my page i have multiple saving buttons for saving multiple information,like one for creation and one popup saving and other also and i am colling a different function for saving different things,cancel and for other purpose also ,so how to handle this case, through which fuction will coll . – Vivek Shukla Feb 26 '16 at 11:36
  • You need to bind your keydown to an html element/node, *then* check what the key is. "ALT+S" is not an html element (in fact you don't need this parameter at all - if you put "#btnSaveCompany" here then it will only work when that button has the focus - which defeats the point of having a keyboard shortcut. – freedomn-m Feb 26 '16 at 11:37
  • If you have multiple buttons - which one are you expecting alt+s to work for? Do you know how keyboard shortcuts work? – freedomn-m Feb 26 '16 at 11:37
  • and how i find that ,like i am on page there is different button for saving .and on popup different for saving so how to handle this through shortcut which will colled.how to find Rory sir? – Vivek Shukla Feb 26 '16 at 11:40

1 Answers1

0

Using Alt + S in Firefox for example will open the history menu. So you will have to figure out which key down or hot key combinations you want to use that will not fire built in browser hot keys. For the example below I using Shift + S to fire a function to simulate saving.

Live Example: http://codepen.io/larryjoelane/pen/zqOQEd?editors=1010

//variable to hold the value of keydown press
//combinations
var saveHotKeys = 0;

//keysDown flag intialized to false
var keysDown = false;

$(document).on("keydown", function(e) {

  //set keysDown flag to true
  keysDown = true;

  //if the keysDown flag is true
  //and the key pressed is not equal to value stored
  if(keysDown === true && e.which !== saveHotKeys){

      //add key code value saveHotKeys
      saveHotKeys += e.which;

  }

  //debug only(used to determine total value of keydown codes)
  console.log(saveHotKeys);

  // SHIFT + S for saving shortcut
  // Using CTRL + S will invoke
  // browser hot key
  if ( saveHotKeys === 99) {

    //call the save function
    funSaveCreation();       

  }

}); 

//key up event to clear the flag  and hot keys values
$(document).on("keyup",function(){

  //set keysDown flag to false
  keysDown = false;

  //clear the saveHotKeys value
  saveHotKeys = 0;

});

function funSaveCreation() {

  alert("saved");

}
Larry Lane
  • 2,141
  • 1
  • 12
  • 18