2

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();
        }
Community
  • 1
  • 1
Javier Bullrich
  • 389
  • 1
  • 5
  • 22
  • 2
    need to see code with eventhandler – sammarcow May 16 '16 at 19:16
  • @sammarcow It's literary a MediaControl mc = new MediaControl(); mc.Play(); on the start of the form – Javier Bullrich May 16 '16 at 19:42
  • you need to post the code you are describing. – sammarcow May 16 '16 at 20:04
  • @sammarcow I updated the question as you asked – Javier Bullrich May 16 '16 at 20:47
  • 1
    It is broken code, it does not also generate the KEYUP notification. So the OS does not know any better than that you are keeping the key pressed down. What happens next is pretty unpredictable but keys that are held down do tend to repeat. Just don't do it this way, [use WM_APPCOMMAND instead](http://stackoverflow.com/a/21504831/17034) instead. – Hans Passant May 16 '16 at 20:49
  • @HansPassant I tried copying the code from that link to a new class, but when calling the class with the command Send it doesn't do anything. Any idea if I have to set up something before? – Javier Bullrich May 16 '16 at 21:14
  • @HansPassant I made it work, but it doesn't work with the media keys. I can raise the volume and other keys work, but for some reason the media ones doesn't – Javier Bullrich May 16 '16 at 23:02

0 Answers0