Playing sounds by sending keystrokes to AutoHotKey - MobiFlight/MobiFlight-Connector GitHub Wiki

MobiFlight does not support playing sound effects directly, but it is possible to use another very useful piece of open source software to do this.

AutoHotKey lets you define "hotkeys" like Shift-F12 to perform tasks, like, among other things, to play sounds. The software has a lot of features to automate your computer, and many of them are potentially useful in a home cockpit / simulator setup, so please consult the Autohotkey documentation if you want to learn more. But for now, lets continue and define our "play sound" script.

After you have installed AutoHotKey, it does not have a "user interface" per se, but you can create a text file with a .ahk extension with a text editor (please be aware Notepad is very insistent on saving every file with a .txt extension, so you want to View > Show > File name extensions on Windows Explorer to see that your file is whatever.ahk, and not whatever.ahk.txt, so it will be correctly associated with AutoHotKey.

Right click and select "Open" with AutoHotKey (you can see the icon here in the menu to confirm it is associated correctly)

image

Here's a quick script to play a sound when a given (Shift-F12) keyboard input is detected:

#Requires AutoHotkey v2.0
~+F12::SoundPlay, C:\vPilot\Sounds\SelCal.wav
return

The specifics of this can be found in documentation, but save this to a file like mobiflight-sounds.ahk and open it by double clicking it or selecting Open from the menu. Now test Shift-F12 and the sound should play. You naturally need to substitute the full path to the wav to something that exists in your computer. If the sound plays, we can create the keyboard send event in MobiFlight like this:

image

As our synthetic keyboard input is not intended to MSFS2020, but rather to AutoHotKey, we don't need to worry about the limitation of MSFS to not see programmatically generated keyboard input, and the event should work.

How do I make a sound when something happens in the simulator?

In this example we do a low fuel warning sound when the fuel quantity goes below a certain value. This requires an extra step, because MobiFlight supports keyboard shortcuts only from input devices. However we can use a workaround as detailed below:

  • We create an output config to read the fuel quantity on selected tank from the simulator
  • We then create a Transform rule to set our config value to 1 when the fuel quantity is below a certain value
  • Finally we set the "Display" to "Input Action" to create the keyboard event for Autohotkey.

First the fuel quanity read, create a new input configuration:

image

Set the Transform to if($<3,1,0) so that our output value is set to 1 whenever the fuel quantity is below 3 gallons, as an example.

To send the keystroke, we will use a bit of a workaround, since MobiFlight only supports keyboard events from input devices like switches. So we set our "Display" to "Input Action" and specify the keyboard shortcut there. Since our value is either 0 or 1, we will choose the "OnPress / OnRelease" option which generates the OnPress event when the fuel quantity gets low enough for a warning.

image

You can name your Output configuration so that it includes the defined shortcut key, so you can have them nicely listed for when you want to add them to the Autohotkey script.

image

Happy flying!