plane icon Welcome to Microsoft Flight Simulator’s SDK Q&A Platform!

You have questions regarding the SDK? DevMode Tools? SimConnect? You would like to submit an idea for future improvements, seek help or exchange knowledge? You’re in the right place.


Please take a moment to read the platform’s guidelines before you get started!


question

N6722C avatar image
N6722C asked N6722C commented

Request Documentation for SU4: Ability to intercept and mask key events has been added to the JS key event listener

Please can we have some documentation or examples on how to implement

  • Ability to intercept and mask key events has been added to the JS key event listener .

(as added in Sim Update #4)


I did get a little help with this, but so far, I have not been able to successfully intercept Key Events

ie the correct use of::

(1) RegisterViewListener('JS_LISTENER_KEYEVENT')

(2) listener.on('keyIntercepted'keyEventName => {
                  

(3) Coherent.call('INTERCEPT_KEY_EVENT''AP_NAV1_HOLD'1);

and anything else needed to intercept key events

Currently, I cannot find any mention of this in the SDK, or anywhere else I have searched, including all the MSFS JS files currently on my PC.




defaultgauges
1 comment
10 |10000

Up to 5 attachments (including images) can be used with a maximum of 4.8 MiB each and 23.8 MiB total.

N6722C avatar image N6722C commented ·

In the spirit of sharing, and not keeping technical secrets from others, this is my example of the code I ended up getting to work. (It could do with a little optimizing !!)


The design aim was to be able to allow the Garmin GNS530, to respond to Events being generated by another gauge, the VOR1 OBS knob, without constantly polling the VOR1 OBS knob it in the update loop,

JS_LISTENER_KEYEVENT-example.txt






1 Like 1 ·
dga avatar image
dga answered N6722C commented

One thing I see here... You might have to take care to do your subscriptions in the callback of RegisterViewListener.
When you do it immediately after the call it might not be registered yet.

Something like

this.keyListener = RegisterViewListener('JS_LISTENER_KEYEVENT', () => {
      Coherent.call('INTERCEPT_KEY_EVENT', 'AP_NAV1_HOLD', 1);
      this.keyListener.on('keyIntercepted', this.handleKeyIntercepted);
    });
4 comments
10 |10000

Up to 5 attachments (including images) can be used with a maximum of 4.8 MiB each and 23.8 MiB total.

N6722C avatar image N6722C commented ·

Thanks dga,

I still have not got this to work, which is why I was looking for a working code example.
When this got added in SU4, surely there was some example test code created, to verify that this actually works as expected ?


Looking for an example of the use of this:-

I can see in some js files, instances of

RegisterViewListener('JS_LISTENER_KEYEVENT'

but then, no instances of

Coherent.call('INTERCEPT_KEY_EVENT',


                  
0 Likes 0 ·
dga avatar image dga N6722C commented ·
It is currently not used in any stock avionics. It was an addition to give addon developers more possibilities to handle K Events on the JS side for the future.

I gave you a working example in my answer above.
You have to add your callback method (handleKeyIntercepted) in my example and handle the event there.

0 Likes 0 ·
N6722C avatar image N6722C dga commented ·
Thanks Matt & Dominic (WT),

Finally got it figured out and working !!!
Great addition to the MSFS JS toolkit :)


1 Like 1 ·
Show more comments
ericmarciano avatar image
ericmarciano answered N6722C commented

Thanks for this useful answer.

Do you know what is the third parameter for? I see it is set to 1 in your sample, I used the same function with 0 and it works as well.

2 comments
10 |10000

Up to 5 attachments (including images) can be used with a maximum of 4.8 MiB each and 23.8 MiB total.

ericmarciano avatar image ericmarciano commented ·
As I would like to know more about this feature, can you please tell me where it is documented? I couldn't find it anywhere in the SDK.


Thank you.

1 Like 1 ·
N6722C avatar image N6722C ericmarciano commented ·
I do not believe it is yet in the SDK documentation. I found out what i found out by looking at the WT's Nxi, that introduced this functionality into MSFS, and some kind help and assistnce from Matt & DGA.


I think the 3rd paramter, is for some control to register/unregister, but at the moment. I believe it is ignored, and the only thing you can do is register -(ie Unregister is not supported yet)


0 Likes 0 ·

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 5 attachments (including images) can be used with a maximum of 19.1 MiB each and 23.8 MiB total.