I have this C# code taken from the P3d sdk and adapted to run in FSX with Simconnect.
The code works for the Pause/Unpause and I am able to use it to Pause / Unpause FSX with the status output to the console.
However with the code to set the GearUp/Gear Down does not work. The program recognises the words "GEAR UP" and "GEAR DOWN" and prints them to the console but the gear of the a/c in FSX do not go up or down.
I an not very familiar with C# so if someone can tell me what the problem is and how to fix it that would be great.
Thanks
Here is the code:
The code works for the Pause/Unpause and I am able to use it to Pause / Unpause FSX with the status output to the console.
However with the code to set the GearUp/Gear Down does not work. The program recognises the words "GEAR UP" and "GEAR DOWN" and prints them to the console but the gear of the a/c in FSX do not go up or down.
I an not very familiar with C# so if someone can tell me what the problem is and how to fix it that would be great.
Thanks
Here is the code:
C#:
// Copyright (c) 2010-2019 Lockheed Martin Corporation. All rights reserved.
// Use of this file is bound by the PREPAR3D® SOFTWARE DEVELOPER KIT END USER LICENSE AGREEMENT
// Managed Voice Controls
/*
Use T to toggle the Voice control OFF/ON while console has focus
Say PAUSE to pause FSX
Say UNPAUSE to unpause FSX
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Speech;
using System.Speech.Synthesis;
using System.Speech.Recognition;
using System.Windows.Forms;
using Microsoft.FlightSimulator.SimConnect;
using System.Runtime.InteropServices;
namespace Managed_Voice_Controls
{
class ManagedVoiceControls
{
enum EVENTS
{
UNPAUSE = 0,
PAUSE,
GEARUP,
GEARDOWN
};
Dictionary<string, EVENTS> phrases = new Dictionary<string, EVENTS>()
{
//{ "pause prepared", EVENTS.PAUSE },
// { "unpause prepared", EVENTS.UNPAUSE }
{ "pause", EVENTS.PAUSE },
{ "unpause", EVENTS.UNPAUSE },
{ "gear up", EVENTS.GEARUP },
{ "gear down", EVENTS.GEARDOWN }
};
enum GROUPID
{
FLAG = 2000000000,
};
SimConnect simconnect = null;
GrammarBuilder grammarBuilder = null;
SpeechRecognitionEngine recognitionEngine = null;
bool bVoiceRecognitionEnabled = true;
bool done = false;
const int WM_USER_SIMCONNECT = 0x0402;
public IntPtr Handle { get; private set; }
public ManagedVoiceControls()
{
}
public void Run()
{
Connect();
BuildGrammar();
Listen();
}
public void Connect()
{
try
{
simconnect = new SimConnect("Managed Voice Control", this.Handle, WM_USER_SIMCONNECT, null, 0);
simconnect.MapClientEventToSimEvent(EVENTS.PAUSE, "PAUSE_ON");
simconnect.MapClientEventToSimEvent(EVENTS.UNPAUSE, "PAUSE_OFF");
simconnect.MapClientEventToSimEvent(EVENTS.GEARUP, "GEAR_UP");
simconnect.MapClientEventToSimEvent(EVENTS.GEARDOWN, "GEAR_DOWN");
}
catch (Exception ex)
{
Console.WriteLine("Sim Connect unable to connect to FSX!\n\n{0}\n\n{1}", ex.Message, ex.StackTrace);
}
}
public void BuildGrammar()
{
grammarBuilder = new GrammarBuilder();
recognitionEngine = new SpeechRecognitionEngine();
Choices phraseChoices = new Choices();
foreach (KeyValuePair<string, EVENTS> i in phrases)
{
phraseChoices.Add(i.Key);
}
grammarBuilder.Append(phraseChoices);
Grammar grammar = new Grammar(grammarBuilder);
recognitionEngine.SetInputToDefaultAudioDevice();
recognitionEngine.LoadGrammar(grammar);
}
public void Listen()
{
recognitionEngine.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs>(OnSpeechRecognized);
recognitionEngine.RecognizeAsync(RecognizeMode.Multiple);
while (!done)
{
string key = Console.ReadKey(true).Key.ToString();
if (key.ToUpper() == "T")
{
ToggleVoiceRecognition();
}
}
}
public void ToggleVoiceRecognition()
{
if (bVoiceRecognitionEnabled)
{
recognitionEngine.RecognizeAsyncStop();
bVoiceRecognitionEnabled = false;
Console.WriteLine("Voice recognition disabled\n");
}
else
{
recognitionEngine.RecognizeAsync(RecognizeMode.Multiple);
bVoiceRecognitionEnabled = true;
Console.WriteLine("Voice recognition enabled\n");
}
}
public void OnSpeechRecognized(object sender, SpeechRecognizedEventArgs e)
{
if (e.Result.Confidence < .90 || !bVoiceRecognitionEnabled)
{
return;
}
switch (phrases[e.Result.Text])
{
case EVENTS.PAUSE:
simconnect.TransmitClientEvent((uint)SimConnect.SIMCONNECT_OBJECT_ID_USER, EVENTS.PAUSE, (uint)0, GROUPID.FLAG, SIMCONNECT_EVENT_FLAG.GROUPID_IS_PRIORITY);
Console.WriteLine("Pausing FSX\n");
break;
case EVENTS.UNPAUSE:
simconnect.TransmitClientEvent((uint)SimConnect.SIMCONNECT_OBJECT_ID_USER, EVENTS.UNPAUSE, (uint)0, GROUPID.FLAG, SIMCONNECT_EVENT_FLAG.GROUPID_IS_PRIORITY);
Console.WriteLine("Unpausing FSX\n");
break;
case EVENTS.GEARUP:
simconnect.TransmitClientEvent((uint)SimConnect.SIMCONNECT_OBJECT_ID_USER, EVENTS.GEARUP, (uint)0, GROUPID.FLAG, SIMCONNECT_EVENT_FLAG.GROUPID_IS_PRIORITY);
Console.WriteLine("Gear Up FSX\n");
break;
case EVENTS.GEARDOWN:
simconnect.TransmitClientEvent((uint)SimConnect.SIMCONNECT_OBJECT_ID_USER, EVENTS.GEARDOWN, (uint)0, GROUPID.FLAG, SIMCONNECT_EVENT_FLAG.GROUPID_IS_PRIORITY);
Console.WriteLine("Gear Down FSX\n");
break;
default:
break;
}
}
public void clearGrammar()
{
recognitionEngine.UnloadAllGrammars();
}
}
}
