如何使用inputsimulator将按键从Winform发送到另一个应用程序?

问题描述

我尝试发送一条消息,然后ctrl + ent进入chrome。 问题是几乎放弃后,它发送了ctrl + enter一次。 邮件已复制,粘贴在chrome中,但ctrl + enter命令不会发送到chrome。

这是我的代码的一部分: 顺便说一句,KeystrokeSimulation看起来像这样:

        public void KeystrokeSimulation()
        {
            var inputSim = new InputSimulator();
            inputSim.Keyboard.KeyDown(VirtualKeyCode.CONTROL);
            inputSim.Keyboard.KeyDown(VirtualKeyCode.RETURN);
            inputSim.Keyboard.KeyUp(VirtualKeyCode.CONTROL);
            inputSim.Keyboard.KeyUp(VirtualKeyCode.RETURN);
}


private void button3_Click(object sender,EventArgs e)

        {

            int mousex = Cursor.Position.X;
            int mousey = Cursor.Position.Y;
            try
            {

                if (string.IsNullOrEmpty(textBox7.Text) && string.IsNullOrEmpty(textBox8.Text)
                    || string.IsNullOrEmpty(textBox7.Text) || string.IsNullOrEmpty(textBox8.Text))
                {
                    if (string.IsNullOrEmpty(button3.Text))
                    {
                        MessageBox.Show("Please select a message from the dropBox!");
                    }
                    else
                    {
                        Clipboard.SetText(button3.Text.ToString());
                        if (string.IsNullOrEmpty(textBox7.Text) && string.IsNullOrEmpty(textBox8.Text)
         || string.IsNullOrEmpty(textBox7.Text) || string.IsNullOrEmpty(textBox8.Text))
                        {
                            SendKeys.Send("^{v}");
                            IDataObject iData = Clipboard.GetDataObject();
                            textBox1.Text = (String)iData.GetData(DataFormats.Text);
                        }
                        else
                        {
                            DoMouseClick();
                            SendKeys.Send("^{v}");
                            IDataObject iData = Clipboard.GetDataObject();
                            textBox1.Text = (String)iData.GetData(DataFormats.Text);
                        }
                    }
                }
                else
                {
                    int x = Int32.Parse(textBox7.Text);
                    int y = Int32.Parse(textBox8.Text);


                    Clipboard.SetText(button3.Text.ToString());
                    if (checkBox1.Checked == true)
                    {
                        Cursor.Position = new Point(pmousex,pmousey);
                    }
                    else
                    {
                        System.Windows.Forms.Cursor.Position = new Point(x,y);
                    }
                    DoMouseClick();
                    SendKeys.Send("^{v}");
                    KeystrokeSimulation();
                    System.Windows.Forms.Cursor.Position = new Point(mousex,mousey);
                    IDataObject iData = Clipboard.GetDataObject();
                    textBox1.Text = (String)iData.GetData(DataFormats.Text);
                }
            }
            catch
            {
                //do nothing
            }
        }

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)