0

One of the requirements of our customer is for the support of hot keys for different buttons(like save,create etc) in our application(using jsf 2.0) and the user should only do one key press. i.e. in case of Save button, if I click on say F1 key, the save button should be clicked.

Invoking the save button thru access key in JSF: Lets say, 's' is the access key for the Save button in my page. To click on the save button using access key, user needs to click on the alt,shift and then the 's' key. Alt and shift keys will differ based on the browser but the 's' key which is assigned will be constant.

Since I want the user to click on only one key(say F1), I would like to programatically trigger the Alt+Shift+ 's' key press.

Following is the code I have tried but didn't work.

$(document).keydown(function (e) {  
 if (e.which == 112) {  
    var e1 = $.Event("keydown", { keyCode: 83});
    e1.altKey = true;  
    e1.shiftKey = true;  
    $("body").trigger(e1);  
 }  
 else {  
    console.log("you click on " + e.keyCode 
    + " altkey= " + e.altKey + " shift key" + e.shiftKey);  
 }  
});
  • I'd like to help, but I've read your question 3 times, and didn't figure out what you're trying to achieve. Can you edit your question to make it more clear for us? – Buzinas Sep 25 '15 at 02:47
  • text says `didn't work` but comments in code say `this works`. Need to be more specific about what isn't working and what happens both in UI and browser console. Also not entirely clear what you want – charlietfl Sep 25 '15 at 02:56
  • @Buzinas Sorry for the inconvenience. I have edited the question. – Naveen Darisi Sep 25 '15 at 04:22
  • @NaveenDarisi your code works just fine for me... it prints in the console the values, etc. – Buzinas Sep 25 '15 at 05:40
  • @Buzinas I can see the values in the console printed correctly but the save button is not clicked. But when I manually press on the alt+shift+s then the button is clicked. – Naveen Darisi Sep 25 '15 at 05:50
  • @NaveenDarisi Can't you simply call `$('#idOfYourButton').click();`? – Buzinas Sep 25 '15 at 06:10
  • @Buzinas JSF 2 allows lot of re usability and the ids of the button is appended with all the parent components as well.(Ex: if the id of the component is l1, then client id of it might be pt1:r1:0:rdb11:0:lv1:2:l1). Also the page contains lots of other reusable components and so it is not straight forward to use the approach that you have suggested. Tq – Naveen Darisi Sep 25 '15 at 06:27
  • 1
    Can't you add a CSS class to that button, then? E.g `save-button`, and then `$('.save-button').click()`? – Buzinas Sep 25 '15 at 06:39
  • @Buzinas Using css class is a good alternative solution. It worked. Thanks for your help. – Naveen Darisi Oct 01 '15 at 01:08

0 Answers0