我尝试使用 SFML 库编写代码,当我按一个键时,我更改了一个bool变量。问题是我使用按键操作,我不知道如何实现仅使用1次x键的密钥释放,这就是我正在尝试实现的原因,因为按键会影响多次更改变量的所有时间。
if (Keyboard.IsKeyPressed(Keyboard.Key.A))
{
values[0] = !values[0];
}
if (Keyboard.IsKeyPressed(Keyboard.Key.S))
{
values[1] = !values[1];
}
if (Keyboard.IsKeyPressed(Keyboard.Key.D))
{
values[2] = !values[2];
}
由于
答案 0 :(得分:1)
我认为您可以使用Window.KeyPressed和Window.KeyReleased并保存按下并释放的键
private Dictionary<Keyboard.Key, bool> keysArePressed = new Dictionary<Keyboard.Key, bool>
{
{Keyboard.Key.A, false},
{Keyboard.Key.S, false},
{Keyboard.Key.D, false}
};
// Key
Window.KeyPressed += OnKeyPressed;
Window.KeyReleased += OnKeyReleased;
public void OnKeyPressed(object sender, KeyEventArgs e)
{
if (e.Code == Keyboard.Key.A && !keysArePressed[Keyboard.Key.A])
{
Console.WriteLine("A pressed");
keysArePressed[Keyboard.Key.A] = true;
}
}
public void OnKeyReleased(object sender, KeyEventArgs e)
{
if (e.Code == Keyboard.Key.A)
{
Console.WriteLine("A released");
keysArePressed[Keyboard.Key.A] = false;
}
}