“如何检测”;“外部”;按C#键?
我正在用Microsoft Visual Studio用C#制作一个简单的应用程序 应用程序使光标移动到一个点(窗体外窗口)并单击多次。我通过按X开始单击循环。因此,如果您感兴趣,代码如下所示:“如何检测”;“外部”;按C#键?,c#,windows,winforms,mouse,mouseevent,C#,Windows,Winforms,Mouse,Mouseevent,我正在用Microsoft Visual Studio用C#制作一个简单的应用程序 应用程序使光标移动到一个点(窗体外窗口)并单击多次。我通过按X开始单击循环。因此,如果您感兴趣,代码如下所示: public void Wait(int milliseconds) { System.Threading.Thread.Sleep(milliseconds); } [System.Runtime.InteropServices
public void Wait(int milliseconds)
{
System.Threading.Thread.Sleep(milliseconds);
}
[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 = 0x08;
public const int MOUSEEVENTF_RIGHTUP = 0x10;
public void MouseClick(Point pos, int click = 0)
{
int x = pos.X;
int y = pos.Y;
//MessageBox.Show("clicking mouse on " + pos.ToString());
if (click == 1)
{
mouse_event(MOUSEEVENTF_RIGHTDOWN, x, y, 0, 0);
mouse_event(MOUSEEVENTF_RIGHTUP, x, y, 0, 0);
}
else
{
mouse_event(MOUSEEVENTF_LEFTDOWN, x, y, 0, 0);
mouse_event(MOUSEEVENTF_LEFTUP, x, y, 0, 0);
}
}
public void MouseMove(int x, int y)
{
MouseMove(new Point(x,y));
}
public void MouseMove(Point target)
{
Cursor.Position = target;
}
private void Form1_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.A)
{
MessageBox.Show(Cursor.Position.ToString());
}
MouseMove(42, 42);
if (e.KeyCode == Keys.X)
{
MessageBox.Show(Cursor.Position.ToString());
Wait(42);
Random r = new Random();
for (int i = 0; i < number_of_times ; i++)
{
Wait(r.Next(42));
MouseClick(Cursor.Position);
}
}
}
public void Wait(整数毫秒)
{
系统线程线程睡眠(毫秒);
}
[System.Runtime.InteropServices.DllImport(“user32.dll”)]
公共静态外部无效鼠标事件(intdwflags、intdx、intdy、intcbuttons、intdwextrainfo);
public const int MOUSEEVENTF_LEFTDOWN=0x02;
公共常数int MOUSEEVENTF_LEFTUP=0x04;
公共常数int MOUSEEVENTF_RIGHTDOWN=0x08;
公共常数int MOUSEEVENTF_RIGHTUP=0x10;
公共无效鼠标单击(点位置,int click=0)
{
int x=位置x;
int y=位置y;
//Show(“在“+pos.ToString()上单击鼠标”);
如果(单击==1)
{
鼠标事件(鼠标事件右下,x,y,0,0);
鼠标事件(鼠标事件右起,x,y,0,0);
}
其他的
{
鼠标事件(MOUSEEVENTF_LEFTDOWN,x,y,0,0);
鼠标事件(MOUSEEVENTF_LEFTUP,x,y,0,0);
}
}
公共无效鼠标移动(整数x,整数y)
{
MouseMove(新点(x,y));
}
公共无效鼠标移动(点目标)
{
光标位置=目标;
}
私有void Form1\u KeyUp(对象发送方,KeyEventArgs e)
{
if(e.KeyCode==Keys.A)
{
Show(Cursor.Position.ToString());
}
MouseMove(42,42);
if(e.KeyCode==Keys.X)
{
Show(Cursor.Position.ToString());
等待(42);
随机r=新随机();
for(int i=0;i
但是,正如您所看到的,一旦单击开始,表单就会返回(在屏幕上不可见),因此它无法检测按键。那么我如何停止点击循环呢?如果我按Ctrl+Alt+Del组合键,它会暂停,然后打开任务管理器,但单击会继续
也许是检测ctrl-alt-del的一种方法?或者在车窗关闭时按下任何其他按键
谢谢你的帮助 看看下面的文章: 另外,下面是一个代码示例:
全局热键可能是一个很好的解决方案:您可能需要一些
全局键盘挂钩解决方案。@KingKing谢谢。但我不知道(谷歌搜索后)该怎么做。事实上,每次按X
时,你的代码都会工作,按后,自动点击将随机进行,那么你的表单可能会处于非活动状态。因此,您可以尝试将其放在前面,等待用户按下另一个X
。