Automatic click at a certain time

Asked

Viewed 1,383 times

1

I am new to C# programming and would like to know if there is any way to create a program that shows the local time in the format HH:mm:ss:mmm and allows the user to choose a specific time for a Click automatic mouse, for example the user wants there to be a Click ace 21:01:25:316 the program will do just that Click automatically.

I’ve been searching Youtube and some C# forums and I couldn’t find anything to do with this. I just found some tutorials that taught to program a Click when a certain period of time (chosen by the user) passed by.

I do not know if this type of program is possible to be done in C# but as it is one of the languages that I am studying now and allows programming in windows I asked about it.

2 answers

0


Add using System.Runtime.InteropServices; in your Form class, and then the code below within that same class:

[DllImport("user32.dll",CharSet=CharSet.Auto,   CallingConvention=CallingConvention.StdCall)]
public static extern void mouse_event(uint dwFlags, uint dx, uint dy, uint cButtons, uint dwExtraInfo);
//Mouse actions
private const int MOUSEEVENTF_LEFTDOWN = 0x02;
private const int MOUSEEVENTF_LEFTUP = 0x04;
private const int MOUSEEVENTF_RIGHTDOWN = 0x08;
private const int MOUSEEVENTF_RIGHTUP = 0x10;

public async Task Clicar() {
    // Pedir ao usuário que informe o horário em que o click deva ocorrer
    // (exemplificado pelas variáveis hora_click, min_click, seg_click, miles_click)
    Console.WriteLine(DateTime.Now.ToString("HH:mm:ss:fff")); // imprime a hora atual. 
    //Refatorar isso de acordo com sua necessidade e seu Form, 
    //mas a string no formato desejado é DateTime.Now.ToString("HH:mm:ss:fff")
    DateTime objetivo = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, hora_click, min_click, seg_click, miles_click); // definir uma data em que o evento acontecerá
    // você precisa usar o objetivo.CompareTo(DateTime.Now) pra saber se a hora informada pelo usuário é depois do horário atual. Nesse caso o método retorna 1. Se for igual retorna 0 e anterior retorna -1
    TimeSpan wait_time = objetivo.Subtract(DateTime.Now); // pegar o tempo de espera para realizar o click
    await Task.Delay(wait_time); // recomendo uso de await pois não trava a GUI. É bom evitar métodos async com retorno void, mas nesse caso acho que não tem problema
    DoMouseClick(); // faz o click do mouse
}

public void DoMouseClick() {
    uint X = (uint)Cursor.Position.X; // você pode colocar as coordenadas X,Y pra qualquer valor que quiser, nesse caso ficou a coordenada atual
    uint Y = (uint)Cursor.Position.Y;
    mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, X, Y, 0, 0);
}

Then just call the Click() method the way you prefer and modify it according to your need.

Part of the code was taken from this question https://stackoverflow.com/questions/2416748/how-to-simulate-mouse-click-in-c

Here the link to the Datetime method.Compareto https://msdn.microsoft.com/pt-br/library/system.datetime.compareto(v=vs.110). aspx

  • I have never used either async or await. public async void Clicar() because I can’t merge async with void... Windows Forms and not the Console?

  • The context is fine, it is possible to use asynchronous programming in Windows Forms yes, I even tested this code with a Form and it worked. The problem is that the return void is not recommended in an asynchronous method and your IDE or compiler should be pointing this out. Try changing the method to: public async Task Clicar() because, according to the Microsoft, Async methods that don’t contain a Return statement or that contain a Return statement that doesn’t Return an operand usually have a Return type of Task.

  • Exactly. I was wrong and I thank you for the quick and explanatory answers you offered. I was using Visual Studio 2010 and this release uses a framework not compatible with these commands. Thanks again.

0

To run the event click you can call the method that represents this event. For example: public void button1_Click(Object sender, EventArgs e) { }. Regarding scheduling time, take a look on that topic.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.