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();
}

