Automatic Click at a certain time

1

I'm new to programming in C # and would like to know if there is any way to create a program that displays local time in the format HH: mm: ss: mmm and allows the user to choose a time specific for an automatic Click of the mouse, for example you want a Click as 21: 01: 25: 316 the program will do this same Click automatically.

I've been searching on youtube and some C # forums and could not find anything to do with this. I just found some tutorials that taught you to program a Click when a certain amount of time (user-selected) passed.

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

    
asked by anonymous 13.01.2017 / 21:52

2 answers

0

Add using System.Runtime.InteropServices; to your Form class, and then the code below within that 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 () as you like and modify it according to your needs.

Part of the code was taken from this question link

Here is the link to the DateTime.CompareTo method link

    
15.01.2017 / 06:36
0

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

    
14.01.2017 / 00:00