Using the Keyboard with a NSB.MsgBox on the Desktop

How would I go about allowing users to use their keyboards to respond to an NSB.MsgBox instead of their mouse when my app is used on the desktop?

I’m able to access the MsgBox buttons using button = document.querySelectorAll('.dialog-buttonNoBorder') and testing for a specific button’s text with button[0].textContent, but I’m not sure how to connect to and then emulate the click event with a keyboard event (onkeyup, onkeypress, etc.) in code for that particular button. TIA for any advice or code samples.

Kind regards,
Doug

Hi Doug,
You can do this with a function that looks for a particular key:

Function turnKey() 'trap for special keystrokes
JavaScript
document.onkeydown = function(evt)
{
evt = evt || window.event;
myKey = evt.keyCode;
getKey();
};
End JavaScript
End Function

You need to turn this on before the NSB.MsgBox appears. Then capture the keys with a function that responds to the possible keys:

Function getKey()
If myKey=27 Then Exit Function ‘ “Esc”
If (myKey<65) Or (myKey>90 And myKey<97) Or (myKey>122) Then Exit Function ‘ not a letter
myKey=UCase(Chr(myKey))
End Function

myKey is now a letter that you can process

You then have to turn off the key function:

Function noKey()
JavaScript
document.onkeydown = function(evt)
{
myKey="";
};
End JavaScript
End Function

A little cumbersome, but it works if you assign a single letter to each button.

Helen

Thank you, Helen. I’ll give that a go.

Kind regards,
Doug

Helen’s code captures the key that was pressed but the tricky part for me is connecting to and emulating the click event on one of the NSB.MsgBox’s buttons. For example, for a MsgBox with 2 buttons (YES and NO), I want the key event to trigger the same response a mouse click would have done, if it had been used, on that button.

We’re able to do this kind of thing on our own controls easily enough, but accessing that same ability with the built-in MsgBox for consistency across our apps when on the desktop seems harder. :thinking:

Kind regards,
Doug

So to explain a little further -

You need to indicate to the user which key to use. For YES or NO you could use YES (Y) and NO (N). You would have to set the button titles yourself.

Since you are listening for a key, if the user types ‘Y’ or ’N’, you would close the MsgBox:
NSB.closeMsgBox()
and proceed with your code.

Helen

Thanks, Helen. I was having a brain fade there and forgot I could just dismiss the MsgBox programmatically myself after getting what I needed from the user rather than “handling” the dialog’s events. Nice and simple. Much appreciated.

Kind regards,
Doug

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.