简体   繁体   中英

Perform action while key is pressed

In my program I would need that an action is perfomed while a key is pressed. I have already searched but the solutions either were not for c#, neither for forms or I couldnt understand them.

Is there a proper and easy solution to this?

EDIT: I am using WinForms and I want that while the form is focussed and a key is pressed an action is repeatedly performed.

First off, you need to provide a bit more information and if possible some code you already tried. But nevertheless I'll try.
The concept is relatively easy, you add a timer to your form, you add key_DOWN and key_UP events (not key pressed). You make a bool that resembles if key is currently pressed, you change its value to true on keydown and false on keyup. It will be true while you hold the key.

    bool keyHold = false;
    public Form1()
    {
        InitializeComponent();
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
        if (keyHold) 
        {
            //Do stuff
        }
    }

    private void Key_up(object sender, KeyEventArgs e)
    {
        Key key = (Key) sender;
        if (key == Key.A) //Specify your key here !
        {
            keyHold = false;
        }
    }

    private void Key_down(object sender, KeyEventArgs e)
    {
        Key key = (Key)sender;
        if (key == Key.A) //Specify your key here !
        {
            keyHold = true;
        }
    }

**If you're trying to make a simple game on forms and you're struggling with the input delay windows has (press and hold a key, it will come up once, wait and then spam the key) This solution works for that (no pause after the initial press).

You can try this.


In the Key down event you set the bool 'buttonIsDown' to TRUE and start the method 'DoIt' in an Separate Thread. The code in the While loop inside the 'DoIt' method runs as long the bool 'buttonIsDown' is true and the Form is on Focus. It stops when the Key Up event is fired or the Form loose the focus. There you can see the 'buttonIsDown' is set to false so that the While loop stops.


      //Your Button Status
      bool buttonIsDown = false;

      //Set Button Status to down
      private void button2_KeyDown(object sender, KeyEventArgs e)
        {
            Key key = sender as Key;
            if (key == Key.A)
            buttonIsDown = true;

            //Starts your code in an Separate thread.
            System.Threading.ThreadPool.QueueUserWorkItem(DoIt);
        }
      //Set Button Status to up
       private void button2_KeyUp(object sender, KeyEventArgs e)
        {
            Key key = sender as Key;
            if (key == Key.A)
            buttonIsDown = false;
        }

      //Method who do your code until button is up
      private void DoIt(object dummy)
      {
        While(buttonIsDown && this.Focused)
        {
            //Do your code
        }
      }

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