Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I've got a question about toggling keys. How do You check if the key is toggled?

Here's my quick and easy code:

    public class KeyToggle
    {
        private bool isEnabled = false;
        private Keys key = Keys.None;

        public KeyToggle(Keys key)
        {
            this.key = key;
        }

        public bool IsEnabled()
        {
            if (keyboardState.IsKeyUp(key) && previousKeyboardState.IsKeyDown(key))
                isEnabled = !isEnabled;

            return isEnabled;
        }
    }

    private KeyToggle keyToggleF12 = new KeyToggle(Keys.F12);

    protected override void Update(GameTime gameTime)
    {
        if (keyToggleF12.IsEnabled())
            Method();
    }

Do You have any better idea for checking if the key is toggled?

EDIT: I didin't mean if the key is clicked (aka current up, previous down). Toggled key is like CapsLock, You press it once and it's enabled, press it once more and it's disabled. I've updated my code a little bit:

    private List<KeyToggle> keyToggleList = new List<KeyToggle>();

    protected override void Update(GameTime gameTime)
    {
        if (IsKeyToggled(Keys.F12))
            Method();
    }

    public static bool IsKeyToggled(Keys key)
    {
        if (ReferenceEquals(keyToggleList.Find(x => x.Key == key), null))
            keyToggleList.Add(new KeyToggle(key));

        return keyToggleList.Find(x => x.Key == key).IsEnabled();
    }
share|improve this question
    
With "toggled" I assume you mean, pressed this frame but wasn't pressed the previous? Then checking this frames keyboard state and keep a record of previous frames keyboard state is the simplest (and therefore one of the best) way to do it. –  Daniel Carlsson Aug 13 '12 at 11:44
    
I edited my answer –  Cyral Aug 13 '12 at 13:26
add comment

1 Answer

 keyboardState = Keyboard.GetState();

 if (!oldKeyBoardState.IsKeyDown(Keys.F12) && keyboardState.IsKeyDown(Keys.F12))
 {
     //Do Stuff
 }
 oldKeyBoardState = keyboardState;

Using 2 keyboard states is the best way in my mind, and I would say you are correct.

I wouldnt even use a class, I would just make a method and call it IsToggled(Keys.F12);

and check each key with that

bool IsToggled(Keys key)
{
   if (!oldKeyBoardState.IsKeyDown(key) && keyboardState.IsKeyDown(key))
      return true;
   else
      return false;
}

EDIT: As suggested, Their is a page on MSDN.

EDIT: I see you have anouther definitions of "Toggled" I suggest you make a dictionary of bools or something, and do Keys[key] =! Keys[key]; after you check if its been pressed.

share|improve this answer
    
+1 Agree, that's how I've solved it. Also on MSDN: msdn.microsoft.com/en-us/library/bb203902.aspx (could be added to answer to flesh it out ;) –  Oskar Duveborn Aug 13 '12 at 11:56
    
I like this method a lot. For years I would just do !oldKeyboardState.IsKeyDown(key) within my code, but using a function is a much better idea –  Jeff Aug 13 '12 at 15:40
    
Your second edit is probably what he's looking for. A bool, by definition, is all you need to store a 'toggle' state. Storing a bool for each toggled key will be the most efficient way to keep track of toggle states. When a key is pressed, just lookup the toggle state and flip the bool. –  A-Type Aug 14 '12 at 14:54
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.