Page 1 of 2

Letters as Shortcut for Buttons

Posted: Tue May 31, 2016 9:38 am
by dieter
Hello,
we have a tool, which shows a question in a window. The window has three buttons with the caption "Yes", "No" and "Cancel". For the cancel button we have defined the shortcut key F12. The users wish to have shortcut keys for the "yes" and "no" buttons too. So they would like to have a "Y" for the "yes button" and a "N" for the "no button".

Do you have any idea to implement this?

Dieter

Re: Letters a Shortcut for Buttons

Posted: Tue May 31, 2016 10:43 am
by Glenn
Dieter,

I would put something like the code below in the 'onload' event of the window's record format. Replace the values in the pui.click() API calls with the ID's of your buttons.

Code: Select all

document.addEventListener('keydown', 
  function(event) {
    
    if(event.keyCode == 89) {
      // 'Y' key pressed
      pui.click("btnYes");
    }
   
    if(event.keyCode == 78) {
      // 'N' key pressed
      pui.click("btnNo");
    }   
    
  }
);
Glenn

Re: Letters a Shortcut for Buttons

Posted: Tue May 31, 2016 12:05 pm
by emhill
Glenn/Dieter,

Just an FYI:

This code will not work for anything before Internet Explorer 9. We tried this a couple of years ago and got dinged by some users still using IE8 due to the stability. Don't know if this will come into play for Dieter, but thought I'd throw that out there.

Re: Letters a Shortcut for Buttons

Posted: Tue May 31, 2016 2:41 pm
by Glenn
Eric,

Thank you for the warning.

Glenn

Re: Letters a Shortcut for Buttons

Posted: Tue May 31, 2016 4:22 pm
by Scott Klement
Profound UI provides an API, addEvent(), that you should consider using instead of document.addEventListener() since some older browsers don't support addEventListener(). The addEvent() API solves that.

To handle older Internet Explorer versions in your event routine, you need to handle the event being global vs. a parameter, and the keyCode being called 'which'. Basically, Internet Explorer never followed the standards, so it has it's own way of doing everything -- as newer versions came out, they made it mode and more compatible with the standards (often breaking applications that were written specifically for IE, causing people to stick with older versions.) Now, of course, IE is such a mess Microsoft is trying to discontinue it completely. But, anyway... I digress...

Try coding like this and it should work on all browsers:

Code: Select all

addEvent(document, "keydown", function(event) {
    event = event || window.event;
    var key = event.which || event.keyCode;
   
    if(key == 89) {
      // 'Y' key pressed
      pui.click("btnYes");
    }
   
    if(key == 78) {
      // 'N' key pressed
      pui.click("btnNo");
    }   
   
});

Re: Letters a Shortcut for Buttons

Posted: Tue May 31, 2016 4:26 pm
by Scott Klement
Also bear in mind that if you attach the event at the document level (as I did above) it will remain until the event is removed. so when you switch to other screens, etc, the code will still be there trying to run on those screens as well.

If you want it to apply to only one screen, you should do one of these:

1) Attach the event to a widget rather than the whole screen. (The keydown event will only fire if that widget has focus when the key is presssed.)
-or-
2) Remove the event with the removeEvent() API when the screen is submitted.

Re: Letters a Shortcut for Buttons

Posted: Wed Jun 01, 2016 4:57 am
by dieter
Thank you for all anwers.
I will try the suggestions.

(We don't use IE. We use Chrome.)

Dieter

Re: Letters a Shortcut for Buttons

Posted: Wed Jun 01, 2016 11:07 am
by emhill
Scott,

On the removeEvent() - is that all that needs to be entered or would you need something like
removeEvent(document, "keydown");

or something similar?

Thanks!!!!

Re: Letters as Shortcut for Buttons

Posted: Fri Jun 03, 2016 12:20 pm
by Scott Klement
to find the documentation for the removeEvent() API, start here:
http://www.profoundlogic.com/docs

In the upper-right there's a field that says "Search". If you type removeEvent into that field it should take you right to the page. Let me know if this doesn't work for you, or if you have questions that are not answered by the documentation.

Re: Letters as Shortcut for Buttons

Posted: Sat Jun 04, 2016 2:40 pm
by emhill
Scott,

I found that earlier but what I used in the parameters did not work. I did my addEvent exactly as you have above just changing what keycodes I needed. I coded the removeEvent like this in the onsubmit event:

removeEvent(document, "keydown", event);


It did not work correctly. I went to another screen and as soon as I hit one of my keycodes it tried to perform my pui.click(). What did I miss?

Thanks!!!!!