I'm using this answer to make my computer simulate the keyboard press of certain media keys, but when the function is called, it presses the key twice, so, when I want to pause a song, it pauses it, then plays it again. Any idea what could be the problem? Here is my code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace namespaceDefault
{
public class MediaControl : Form
{
[DllImport("user32.dll", SetLastError = true)]
public static extern void keybd_event(byte virtualKey, byte scanCode, uint flags, IntPtr extraInfo);
public const int KEYEVENTF_EXTENDEDKEY = 1;
public const int KEYEVENTF_KEYUP = 2;
public const int VK_MEDIA_NEXT_TRACK = 0xB0;
public const int VK_MEDIA_PLAY_PAUSE = 0xB3;
public const int VK_MEDIA_PREV_TRACK = 0xB1;
public void Play()
{
keybd_event(VK_MEDIA_PREV_TRACK, 0, KEYEVENTF_EXTENDEDKEY, IntPtr.Zero);
}
}
}
I'm sure that I'm not calling the same function twice.
The function is called to test it from the start of the form
SpeechRecognitionEngine spchReco = new SpeechRecognitionEngine();
SpeechSynthesizer _taraSynth = new SpeechSynthesizer();
string[][] dialogues;
private Boolean wake = true;
MediaControl mediaKeys = new MediaControl();
public Form1()
{
InitializeComponent();
spchReco.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs>(speechreco_SpeechRecognized);
string csvDirectory = Directory.GetParent(Directory.GetParent(Environment.CurrentDirectory).ToString()).ToString();
dialogues = csvArray(csvDirectory + "\\csvv.csv");
LoadGrammar();
spchReco.SetInputToDefaultAudioDevice();
spchReco.RecognizeAsync(RecognizeMode.Multiple);
//System.Diagnostics.Debug.Write("gato!! ");
speak("Ejecutando");
mediaKeys.Play();
}