简体   繁体   中英

Visual C# Simulating F12 keypress at certain time of day

My friend has asked me to create a program that will press F12 for him at a certain time of day.

This is an unusual request and I was wondering how to get Visual C# to send the F12 request and at a certain time. Maybe it's better to implement a program that presses F12 automatically and set it up in task scheduler? I don't know.

I think the ideal would just have the program run in the background and hit the key at the certain time. I don't know how to instruct Visual C# to send the F12 key. Also, I don't know how to set up things to go off at certain times.

Can someone help me out or point me to a resource?

You probably want to use keybd_event . Here is a sample code that illustrates how to use it.

using System;
using System.Threading;
using System.Runtime.InteropServices;

public class Program
{
    [DllImport("user32.dll", SetLastError = true)]
    static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);

    public const int KEYEVENTF_EXTENDEDKEY = 0x0001; // key down flag
    public const int KEYEVENTF_KEYUP = 0x0002; // key up flag
    public const int F12 = 123; // F12 key code

    public static void Main()
    {
        const int hour = 4;
        const int min = 15;

        // Get DateTime at which the key is supposed to be pressed
        DateTime nextCertainTime = DateTime.Now.Date.AddHours(hour).AddMinutes(min);

        // If it is already too late for today, add 1 day (set to tomorrow)
        if (nextCertainTime < DateTime.Now)
        {
            nextCertainTime = nextCertainTime.AddDays(1);
        }

        // Calculate the remaining time
        TimeSpan remainingTime = nextCertainTime - DateTime.Now;

        // Wait until "certain time"
        Thread.Sleep((int)remainingTime.TotalMilliseconds);

        keybd_event(F12, 0, KEYEVENTF_EXTENDEDKEY, 0); // press F12 down
        keybd_event(F12, 0, KEYEVENTF_KEYUP, 0); // release F12
    }
}

If you want to do this repeatedly, just put it into a while (true) loop and add 1 day to nextCertainTime with each iteration.

It would be better to use a Timer instead, but it is not as easy to set up.

Alternatively, it is also possible to use SendKeys.Send() instead of keybd_event , but it is a bit of an overkill for pressing a single key.

System.Windows.Forms.SendKeys.Send("{F12}");

If you decide to use Task Scheduler, as suggested in another answer, you might as well consider using something more appropriate than C#.

Perhaps an AHK script, which could be as simple as this:

Send {F12}

you can check Windows Input Simulator Library

public static void Main()
{
     InputSimulator.SimulateKeyPress(VirtualKeyCode.SPACE);
}

only thing you need to do is compiling your code into executable and then adding it to Windows Task Scheduler as Task.

or alternatively you can use native Windows API under User32.dll

[DllImport("user32.dll", SetLastError = true)]
static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);

public const int KEYEVENTF_EXTENDEDKEY = 0x0001; // key down flag
public const int KEYEVENTF_KEYUP = 0x0002; // key up flag
public const int F12 = 123; // F12 key code

public static void Main()
{ 
    keybd_event(F12, 0, KEYEVENTF_EXTENDEDKEY, 0); // press F12 down
    keybd_event(F12, 0, KEYEVENTF_KEYUP, 0); // release F12

}

again use Windows task scheduler

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM