问题描述
我正在构建一个Win应用程序,该应用程序将一些预制消息发送到chrome窗口(在一个小的聊天窗口中)按钮单击。
我能够做到,但是现在我想在单击特定按钮时模拟alt+enter
。
这是我的代码的一部分(按钮)。如您所见,我在这里有SendKeys.Send("^{v}");
,但是它什么也没做...
private void button5_Click(object sender,EventArgs e)
{
try
{
if (string.IsNullOrEmpty(textBox7.Text) && string.IsNullOrEmpty(textBox8.Text)
|| string.IsNullOrEmpty(textBox7.Text) || string.IsNullOrEmpty(textBox8.Text))
{
if (string.IsNullOrEmpty(button5.Text))
{
MessageBox.Show("Please select a message from the dropBox!");
}
else
{
Clipboard.SetText(button5.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}");
SendKeys.Send("^{ENTER}");
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(button5.Text.ToString());
System.Windows.Forms.Cursor.Position = new Point(x,y);
DoMouseClick();
SendKeys.Send("^{v}");
IDataObject iData = Clipboard.GetDataObject();
textBox1.Text = (String)iData.GetData(DataFormats.Text);
}
}
catch
{
//do nothing
}
}
解决方法
根据official documentation,您应将"^"
用于ctrl,将"%"
用于alt,并将"{ENTER}"
(或"~"
)用于Enter。
要发送 alt + Enter ,请使用SendKeys.Send("%{ENTER}");
或SendKeys.Send("%~");
,
并发送 ctrl + Enter 使用SendKeys.Send("^{ENTER}");
或SendKeys.Send("^~");