简体   繁体   中英

how to change a boolean value if a key is pressed down c#

I am trying to change a Boolean value if a key is pressed down, but when released revert back to false so I can only trigger a if statement if all the arguments a re correct and a key is pressed down.

public class boolean
{
    public static Boolean keyPresed = false;
}

private void mainForm_KeyDown(object sender, KeyEventArgs e)
{                
    if (e.KeyData == Keys.A)
    {
        MessageBox.Show(" just for debugging ");
        boolean.keyPresed = true;
     } 
      else {
          boolean.keyPressed = false;
         }
        if (tileBuyPlayer2.Checked && ownerShipsPlayer2.peacockOwner == false && boolean.keyPresed == true)
        {
            // do this sorry not on here cause how big it is
        }
    }
}

You can declare keyAPressed as a global var in this class , and reset it in your KeyDown event:

public class DemoClass
{
    public static Boolean keyAPressed = false;

    private void mainForm_KeyDown(object sender, KeyEventArgs e)
    {                
        if (e.KeyCode == Keys.A)
        {
            MessageBox.Show(" just for debugging ");
            keyAPressed = true;
        } 
        else 
        {
            keyAPressed = false;
        }
        if (keyAPressed == true)
        {
            // Reset keyAPressed to false
            keyAPressed = false
            if (tileBuyPlayer2.Checked && ownerShipsPlayer2.peacockOwner == false)
            {
                // do this sorry not on here cause how big it is
            }
        }
    }
}

or use KeyUp event to reset keyAPressed :

public class DemoClass
{
    public static Boolean keyAPressed = false;

    private void mainForm_KeyDown(object sender, KeyEventArgs e)
    {                
        if (e.KeyCode == Keys.A)
        {
            MessageBox.Show(" just for debugging ");
            keyAPressed = true;
        } 
        else 
        {
            keyAPressed = false;
        }

        if (tileBuyPlayer2.Checked && ownerShipsPlayer2.peacockOwner == false && keyAPressed == true)
        {
            // do this sorry not on here cause how big it is
        }
    }
    private void mainForm_KeyUp(object sender, KeyEventArgs e)
    {
        keyAPressed = false;
    }
}

If you are happy to try Microsoft's reactive framework then you could try doing something like this:

var keyDown = Observable.FromEventPattern<KeyEventArgs>(mainForm, "KeyDown");

var query =
    from kd in keyDown
    where kd.EventArgs.KeyCode == Keys.A
    where tileBuyPlayer2.Checked
    where ownerShipsPlayer2.peacockOwner == false
    select kd;

query.Subscribe(x =>
{
    // do your code here
});

I might have missed something that you're trying to do, but I think that makes things much easier.

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