Hotkey requirement

Use this board for starting discussions, asking questions, and giving advice on Web programming for the IBM i platform (and predecessors.)
BFoster
Profound User
Posts: 50
Joined: Fri May 24, 2019 6:26 am
First Name: Ben
Last Name: Foster
Company Name: Manhattan Assurance Company
Contact:

Hotkey requirement

Post by BFoster »

I have an application that requires the use of hotkeys/shortcuts e.g. Alt+Shift+H, Ctrl+Shift+9, etc. Is there a way to accomplish this? Profound sends F1-F24 plus page up and page down among others, so this doesn't seem to be too much of a stretch. I've tried using javascript tied to onkeypress to intercept a combination of keyboard events to no avail.
Scott Klement
Experienced User
Posts: 2711
Joined: Wed Aug 01, 2012 8:58 am
First Name: Scott
Last Name: Klement
Company Name: Profound Logic
City: Milwaukee
State / Province: Wisconsin

Re: Hotkey requirement

Post by Scott Klement »

Yes, you're on the right track, you can do this using onkeyup, onkeydown, or maybe onkeypress (I seem to recall that onkeypress isn't supported by all browsers, but its a fuzzy memory, so not sure of the specifics. This is a non-issue with onkeydown or onkeyup, so I typically use those.)

Can you explain the problem you're having?

Also, can you explain what you want to happen when you hit this key combination?
BFoster
Profound User
Posts: 50
Joined: Fri May 24, 2019 6:26 am
First Name: Ben
Last Name: Foster
Company Name: Manhattan Assurance Company
Contact:

Re: Hotkey requirement

Post by BFoster »

The problem was related to not seeing the hot-key combination being registered by using the JS alert command for feedback/verification. It was solved by using pui.alert. I can get there from here using aforementioned technique. Thanks.
Scott Klement
Experienced User
Posts: 2711
Joined: Wed Aug 01, 2012 8:58 am
First Name: Scott
Last Name: Klement
Company Name: Profound Logic
City: Milwaukee
State / Province: Wisconsin

Re: Hotkey requirement

Post by Scott Klement »

pui.alert() simply calls the browser's alert() function, unless you're running on Mobile (then it uses the mobile device's native alert function.)

Was this on mobile?
BFoster
Profound User
Posts: 50
Joined: Fri May 24, 2019 6:26 am
First Name: Ben
Last Name: Foster
Company Name: Manhattan Assurance Company
Contact:

Re: Hotkey requirement

Post by BFoster »

No, it is an RPG program. I was using the alert method as a debugging aid to visually verify the hotkey was being intercepted.
Scott Klement
Experienced User
Posts: 2711
Joined: Wed Aug 01, 2012 8:58 am
First Name: Scott
Last Name: Klement
Company Name: Profound Logic
City: Milwaukee
State / Province: Wisconsin

Re: Hotkey requirement

Post by Scott Klement »

pui.alert() is the same thing as alert() when running in a browser. So it would not solve any problems to use pui.alert(), it is the same thing.

FWIW, it is possible for an RPG program to output to mobile, that's what the Profound UI Mobile module is all about. Sounds like it doesn't apply in this case, though.
BFoster
Profound User
Posts: 50
Joined: Fri May 24, 2019 6:26 am
First Name: Ben
Last Name: Foster
Company Name: Manhattan Assurance Company
Contact:

Re: Hotkey requirement

Post by BFoster »

This one is probably a one-off solution but it shows the versatility of the Profound UI. The problem centered on being able to detect CTRL-Y or CTRL-SHIFT-U. Typically you would attach the JavaScript to the onkeydown event, but it also means you would have to attach the JavaScript to every input-capable fields. Using the Profound UI in association with external JavaScript solves the problem. To do this, select the subfile's control record and under the property group of "External Files", use the property "external javascript" to specify the location of the .js file. Now, the cursor can be anywhere on the screen and the compound-key entry will be intercepted. The external .js must be placed here for this to work: /profoundui/userdata/custom/js/onUserActivity.js The field 'CtrlKeyInd' is bound to an RPG field.

Code: Select all

//This js will intercept compound keystrokes
pui.onuseractivity = function() {

var ctrlKey = event.ctrlKey;
var key = event.key; 
var shiftKey = event.shiftKey;

console.log("ctrlKey=" + ctrlKey + "   shiftKey=" + shiftKey + "   key="+key); 


  //Search Providers by prov Same as F11  
  if (ctrlKey && key=='y') {
    changeElementValue("CtrlKeyInd", "Ctrl-Y"); 
    pressKey("Enter");
  }

  //Search Providers by Name Same as F13    
  if (ctrlKey && shiftKey && key=='U') {
    changeElementValue("CtrlKeyInd", "Ctrl-Shf-U"); 
    pressKey("Enter");
  }
    
  //List Riders           Same as F6    
  if (ctrlKey && key=='q') {
    changeElementValue("CtrlKeyInd", "Ctrl-Q"); 
    pressKey("Enter");
  }
  
}
Last edited by BFoster on Wed Jul 31, 2019 9:29 am, edited 1 time in total.
Scott Klement
Experienced User
Posts: 2711
Joined: Wed Aug 01, 2012 8:58 am
First Name: Scott
Last Name: Klement
Company Name: Profound Logic
City: Milwaukee
State / Province: Wisconsin

Re: Hotkey requirement

Post by Scott Klement »

pui.onuseractivity is really meant to detect whether the user is present. It will fire for every single mouse movement, so your code will be constantly running.

A better way to do this is to use the addEvent() function to add an event handler for the "keydown" event (or similar.) You dont' have to do it on each individual widget, you only have to do it for the container that the widgets are inside (or at the document level if you want absolutely everything.) Any keyboard event that's not handled by the widgets themselves will "bubble up" to the container.

You can add this in the onload event, and remove it in the onsubmit event. (You could do it via pui.onload and pui.onsubmit if you wanted to add it to every single screen.)

Its also not necesary to use "external javascript", though that would work. Any way of loading/running JavaScript will do the same thing. I personally would be more inclined to use a file in userdata/custom because that way the code is always loaded, whereas "external javascript" will only have it loaded after that particular record format has been displayed.
BFoster
Profound User
Posts: 50
Joined: Fri May 24, 2019 6:26 am
First Name: Ben
Last Name: Foster
Company Name: Manhattan Assurance Company
Contact:

Re: Hotkey requirement

Post by BFoster »

Following up on your recommendation I have attached the following code the the Control Record's onload event:

Code: Select all

/*********************************/
//Add an event to intercept hotkeys
var myPanel = getObj("Panel1");
addEvent(myPanel, "onkeydown", greeting);
function greeting()
{
     alert("Hello There!");
}
"Panel1" is the container holding all of the labels and input-capable fields, buttons, etc. Widget type is "css panel".

The alert does not fire when I key into one of the fields.

I also placed this on the onsubmit event:

Code: Select all

removeEvent(myPanel, "onkeydown", greeting);
When I press enter I get a dialog message saying "Onsubmit Error: myPanel is not defined".
Scott Klement
Experienced User
Posts: 2711
Joined: Wed Aug 01, 2012 8:58 am
First Name: Scott
Last Name: Klement
Company Name: Profound Logic
City: Milwaukee
State / Province: Wisconsin

Re: Hotkey requirement

Post by Scott Klement »

"control record"? Are you referring to a DDS subfile control record? Your code is getting something name "Panel1", which I'm guessing is a panel widget, since that's the default ID that Profound UI assigns to panel widgets -- but that's not what we need to do, here.

What you need to do is add an event handler to the container that all of your widgets are inside. This is an HTML concept -- the idea that HTML tags can be inside other tags, and that keypresses from the "inner" tags will bubble up to the "outer" tags. So it helps to think in terms of HTML rather than DDS.

We provide an API called pui.getActiveContainer() that can be used to retrieve the container that's currently active.
https://docs.profoundlogic.com/x/A4AFAQ

So if you wanted to say "Hey!" when the user presses Ctrl-S you could do the following:

Put all of this in the "onload" event:

Code: Select all

var container = pui.getActiveContainer();

window.greeting = function() {
  // Ctrl-S -- say "Hey"
  if (event.ctrlKey && event.keyCode == 83) {
    alert("Hey!");
  }
}

addEvent(container, "keydown", window.greeting);
Put this in the onsubmit event to remove the keypress handler:

Code: Select all

var container = pui.getActiveContainer();
removeEvent(container, "keydown", window.greeting);
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest