我正在努力制作一个自动化工具,用于为工作中的调度程序应用程序打包带有用户名的文本框。
如果inputArrayX [i]数组包含a,b,c
,我在尝试模拟按键时遇到一些问题keyboardsim将按下abc,但如果数组包含a,b,b,c,c,它仍然只输出abc,而不是abbcc,就像我想要它一样。
任何人都知道我在这里做错了什么?
private void MouseMacroChangeUser()
{
//move form to 0,0
this.Location = new Point(0, 0);
//set xy to mouse current pos
userMousePos();
//inputBlocker();
xX = int.Parse(this.Location.X.ToString());
yY = int.Parse(this.Location.Y.ToString());
defaultMousePos();
//Thread.Sleep(600);
Cursor.Position = new Point(Cursor.Position.X + 739, Cursor.Position.Y + 162);
//Thread.Sleep(100);
MouseSimulator.DoubleClick(MouseButton.Left);
for (int i = 0; i < inputArrayX.Length; i++)
{
string tempX = inputArrayX[i].ToString();
Keys keys = mapToKeyboardMacro(tempX);
KeyboardSimulator.KeyDown(keys);
}
KeyboardSimulator.KeyPress(Keys.Enter);
MouseSimulator.Click(MouseButton.Left);
//reset mouse to user pos.
Cursor.Position = new Point(x, y);
needUnblock = true;
//inputBlocker();
}
private Keys mapToKeyboardMacro(string key)
{
if (key == "space")
{
return Keys.Space;
}
else if (key == "a")
{
return Keys.A;
}
else if (key == "b")
{
return Keys.B;
}
else if (key == "c")
{
return Keys.C;
}
else if (key == "d")
{
return Keys.D;
}
}
答案 0 :(得分:2)
您永远不会从KeyboardSimulator触发KeyUp命令。当钥匙关闭时,无法再次按下。您必须让KeyUp才能触发新的KeyDown事件。
答案 1 :(得分:2)
试试changing KeyboardSimulator.KeyDown(keys);
使用KeyboardSimulator.KeyPress(keys);
我不确定KeyDown事件是否会检查密钥的状态是否已经关闭..