Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8227629
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T16:11:16+00:00 2026-06-07T16:11:16+00:00

I want to write an algorithm to sequentially press keys F1-F3. My form has

  • 0

I want to write an algorithm to sequentially press keys F1-F3. My form has these controls:

lblF1
textboxF1
lblF2
textboxF2
lblF3
textboxF3
btnStart

In textboxF1-textboxF3 the time in seconds is entered. This when the program is to press the hotkey. It is important that the program can’t press two keys at once, for example F1 and F2. It may not press more than one key in a second. When I click on btnStart it calls Run().

This is how I tried to resolve this:

static int counterF1 = 9999;
static int counterF2 = 9999;
static int counterF3 = 9999;

    public void Run()
    {
        counterF1 = 9999;
        counterF2 = 9999;
        counterF3 = 9999;
        while (true)
        {
            Loop();
        }
    }

    public void Loop()
    {
        bool used = false;

            if (counterF1 >= (int)textboxF1.text)
            {
                counterF1 = PressKey(VK_F1);
                used = true;
            }

            if (counterF2 >= (int)textboxF2.text)
            {
                counterF2 = PressKey(VK_F2);
                used = true;
            }

            if (counterF3 >= (int)textboxF3.text)
            {
                counterF3 = PressKey(VK_F3);
                used = true;
            }
        if (used == false)
        {
            IncrementCounters();
            Delay(1000);
        }
    }

    public double PressKey(uint key)
    {
        myPostMessageA(hWindow, WM_KEYDOWN, (uint)key, (uint)key); 
        IncrementCounters();
        return 1; //return 1 because one second
    }

    public void IncrementCounters()
    {
        counterF1++;
        counterF2++;
        counterF3++;
    }

But often it doesn’t press any key (it is possible it is too late, but can’t be an omission). Can you explain how to make an algorithm for this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T16:11:20+00:00Added an answer on June 7, 2026 at 4:11 pm

    We will use a class KeyStroke that stores the necessary data for a special key:

    public class KeyStroke
    {
        public int period { get; set; } // Period in which to hit key
        public int next { get; set; } // ticks to the next hit of this key
        public int VK { get; set; } //KeyCode
    }
    public List<KeyStroke> keys = new List<KeyStroke>();
    

    An Initialize() method is needed to read the data from the text boxes and to init the simulation. We utilize a timer with the interval of one second to run the simulation. In my example, I don’t read from textboxes, but use constant values. Add the input and error handling. If you use WPF, you can bind the KeyStroke objects to the textboxes.

    void Init()
    {
        //Initialize keys with according periods from input
        keys.Clear();
        keys.Add(new KeyStroke() { VK = VK_F1, period = 1, next = 1 });
        keys.Add(new KeyStroke() { VK = VK_F2, period = 10, next = 10 });
        keys.Add(new KeyStroke() { VK = VK_F3, period = 5, next = 5 });
    
        //sort keys by period (descending), in order to handle long period keys, too
        keys.Sort((first, second) => second.period.CompareTo(first.period));
    
        //Start the program
        var t = new DispatcherTimer();
        t.Interval = TimeSpan.FromSeconds(1);
        t.Tick += new EventHandler(t_Tick);
        t.Start();
    }
    

    The tick event is similar to yours:

    void t_Tick(object sender, EventArgs e)
    {
        bool used = false;
        foreach (var key in keys)
        {
            if (key.next <= 0 && !used)
            {
                PressKey(key.VK);
                key.next = key.period;
                used = true;
            }
            key.next--;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to write fastest possible algorithm for 2 number multiplications. Each number has
Is there any algorithm for inertia scrolling like Mac has. I want to write
I want to write a batch script statement where: FINDSTR has to check for
I want to write an algorithm using a dynamic programming technique, which does the
I want to write a generic averaging algorithm. That is, for any type T
I want to write a Did you mean algorithm. I have a set of
I want to write the algorithm of Balanced Binary Search Tree with backtracking would
I want to write a shortest path program. I know how the algorithm works,
I want to write an algorithm that can take parts of a picture and
I want to write a program that processes images. If there are good algorithm

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.