C#模拟媒体键时按两次

时间:2016-05-16 19:04:35

标签: c# .net dllimport

我使用this answer让我的电脑模拟键盘按下某些媒体键,但是当调用该功能时,它会按两次键,所以,当我想暂停一首歌时,它暂停它,然后再次播放它。知道可能是什么问题吗?这是我的代码:

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);
        }
    }
}

我确定我没有两次调用相同的功能。

调用该函数以从表单的开头

对其进行测试
        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();
        }

0 个答案:

没有答案