How do I execute two events at the same time?

1

I am developing a game which is for 2 players, in which two picture boxes must move if the W or S key is pressed, a PictureBox moves, and if you press the Up key or the Down key the other pictureBox should move, but both must react at the same time.

Until now, it works, but either a pictureBox moves or the other, but not at the same time

    
asked by Erik Aldeco 11.07.2017 в 04:44
source

1 answer

0

I recommend you use the following:

//otros using
using System.Runtime.InteropServices;

namespace Ejemplo
{
    public partial class MainWindow : Form
    {
        [DllImport("User32.dll")]
        private static extern short GetAsyncKeyState(Keys teclas);
        [DllImport("user32.dll")]
        private static extern short GetAsyncKeyState(Int32 teclas);

        public MainWindow()
        {
            InitializeComponent();
            timer1.Enabled = true;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            for (int i = 0;  i<=255; i++)
            {
                int numcontrol = GetAsyncKeyState(i);
                if (numcontrol == -32767) // Verificamos si numcontrol fue 
realmente presionado controlando que numcontrol sea -32767 
                {
                    if (Convert.ToBoolean(GetAsyncKeyState(Keys.Up)))
                    {
                        MessageBox.Show("Up");
                    }
                    if (Convert.ToBoolean(GetAsyncKeyState(Keys.Down)))
                    {
                        MessageBox.Show("Down");
                    }
                    if (Convert.ToBoolean(GetAsyncKeyState(Keys.W)))
                    {
                        MessageBox.Show("W");
                    }
                    if (Convert.ToBoolean(GetAsyncKeyState(Keys.W)))
                    {
                        MessageBox.Show("S");
                    }
                }
           }
       }
    }
}

You add a Timer in the Windows control, double click and the event Tick will do, which will listen when something is done about the control, either click or a key and then verify which key was pressed and call to what you want to do.

    
answered by 11.07.2017 / 18:30
source