Receiving H Events
What Are H Events?
H events, so named as they are prefixed with the text H:, are events that are used to communicate with Javascript/HTML instruments. They can be triggered by any number of sources, including but not limited to: model behaviors, SimConnect add-ons, and even Javascript instruments themselves. H events are not required to be defined ahead of time, similar to L Vars, and can be named with any contiguous sequence of alphanumeric characters and underscores.
How To Receive H Events
Inside your main instrument class that extends BaseInstrument, you must define the method onInteractionEvent() in order to receive H events:
class MyInstrument extends BaseInstrument {
get templateID(): string {
return 'MyInstrument';
}
// Will be called by the VCockpit system when an H event is received.
public onInteractionEvent(args: string[]): void {
const hEventName = args[0];
console.log(`Got a H Event named ${hEventName}!`);
}
}
The args parameter of onInteractionEvent() will contain the name of the H event that was received as the first element of the array, and any arguments passed to the H event as additional elements in the array. If the H event does not have any arguments, then args will only contain the H event name.
The avionics framework also provides a convenient utility publisher class to publish H events to an EventBus:
import { EventBus, HEventPublisher } from '@microsoft/msfs-sdk';
class MyInstrument extends BaseInstrument {
private readonly eventBus = new EventBus();
private readonly hEventPublisher = new HEventPublisher(this.eventBus);
get templateID(): string {
return 'MyInstrument';
}
public connectedCallback(): void {
super.connectedCallback();
this.hEventPublisher.startPublish();
}
public onInteractionEvent(args: string[]): void {
this.hEventPublisher.dispatchHEvent(args);
}
}
HEventPublisher will publish the received H events to the event bus under the topics defined in HEvent. H events without any arguments will be published to the hEvent topic and those with at least one argument will be published to the hEvent_with_args topic.
Once HEventPublisher is set up, you can subscribe to H events on the event bus:
import { HEvent } from '@microsoft/msfs-sdk';
const subscriber = eventBus.getSubscriber<HEvent>();
subscriber.on('hEvent')
.handle(eventName => {
console.log(`Got a H Event named ${eventName}!`);
});
subscriber.on('hEvent_with_args')
.handle(data => {
console.log(`Got a H Event named ${data.name} with args ${data.args}!`);
});