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 characters needed characters left characters exceeded

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

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






0 Likes 0 ·

1 Answer

· Write an Answer
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 characters needed characters left characters exceeded

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

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 ·
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 ·
Thanks Matt & Dominic (WT),

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


1 Like 1 ·
Show more comments

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 4.8 MiB each and 23.8 MiB total.