在Windows中模拟来自C#的箭头按键

时间:2015-03-08 18:08:51

标签: c# speech-recognition

我正在为c#中的windows方向键做语音识别系统。我已经完成了鼠标点击事件,但现在我想用箭头键。这是鼠标单击事件的代码。

 public partial class Form1 : Form
{
    SpeechRecognitionEngine _recognizer = new SpeechRecognitionEngine();
    Random rnd = new Random();
    public Form1()
    {
        InitializeComponent();

        this.KeyPreview = true;
        this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.Form1_KeyDown);

        var button1 = new Button();
        button1.Location = new Point(10, 10);
        button1.Text = "click";
        button1.AutoSize = true;
        button1.Click += new EventHandler(button1_Click);
        this.Controls.Add(button1);

    }

    [System.Runtime.InteropServices.DllImport("user32.dll")]
    public static extern void mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo);

    public const int MOUSEEVENTF_LEFTDOWN = 0x02;
    public const int MOUSEEVENTF_LEFTUP = 0x04;
    public const int MOUSEEVENTF_RIGHTDOWN = 0x02;
    public const int MOUSEEVENTF_RIGHTUP = 0x04;


    private void Form1_KeyDown(object sender, KeyEventArgs e)
    {
        SimulateLeftClick();
        SimulateRightClick();


    }


    private void SimulateLeftClick()
    {
        int xpos = Cursor.Position.X;
        int ypos = Cursor.Position.Y;
        mouse_event(MOUSEEVENTF_LEFTDOWN, xpos, ypos, 0, 0);
        mouse_event(MOUSEEVENTF_LEFTUP, xpos, ypos, 0, 0);
    }
    private void SimulateRightClick()
    {
        int xpos1 = Cursor.Position.X;
        int ypos1 = Cursor.Position.Y;
        mouse_event(MOUSEEVENTF_RIGHTDOWN, xpos1, ypos1, 0, 0);
        mouse_event(MOUSEEVENTF_RIGHTUP, xpos1, ypos1, 0, 0);
    }
    private void _recognizer_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
    {
        int ranNum = rnd.Next(1, 10);
        string speech = e.Result.Text;
        switch (speech)
        {

            case "next":
                int xpos = Cursor.Position.X;
                int ypos = Cursor.Position.Y;
                mouse_event(MOUSEEVENTF_LEFTDOWN, xpos, ypos, 0, 0);
                mouse_event(MOUSEEVENTF_LEFTUP, xpos, ypos, 0, 0);
                break;


            case "back":
                int xpos1 = Cursor.Position.X;
                int ypos1 = Cursor.Position.Y;
                mouse_event(MOUSEEVENTF_RIGHTDOWN, xpos1, ypos1, 0, 0);
                mouse_event(MOUSEEVENTF_RIGHTUP, xpos1, ypos1, 0, 0);
                break;


        }

    }
    private void button1_Click(object sender, EventArgs e)
    {
        MessageBox.Show("Left Click simulated");
    }
     void button1_MouseClick(object sender, MouseEventArgs e)
    {
        if (e.Button == System.Windows.Forms.MouseButtons.Right)
        {
            MessageBox.Show("Right click");
        }
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        _recognizer.SetInputToDefaultAudioDevice();
        _recognizer.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs>(_recognizer_SpeechRecognized);
        _recognizer.LoadGrammar(new Grammar(new GrammarBuilder(new Choices(File.ReadAllLines(@"C:\Users\Gayath\Documents\myAI\commands.txt")))));
        _recognizer.LoadGrammar(new DictationGrammar());
        _recognizer.RecognizeAsync(RecognizeMode.Multiple);


    }

当我说“向上”向上箭头键应该工作同样应该操作其他键。我应该在上面的代码中做些什么改变..任何人都可以给我一个解决方案。

0 个答案:

没有答案