English 中文(简体)
键盘事件 SFML 2. 0
原标题:Keyboard Event SFML 2.0
  • 时间:2012-05-21 23:16:14
  •  标签:
  • sfml

我正试图在SFML 1.6 I 使用 SFML 2 中获取关键输入

   while (App.GetEvent(Event))
       {
          if (App.GetInput().IsKeyDown(sf::Key::Down)) { dir= d ; }
    }

但我不知道如何在 SFML 2中做到这一点。

问题回答

当您不需要担心键盘的实时输入时,您可以使用与您提供的 SFML 1.6 代码非常相似的方法。在您的应用程序中的事件处理环中,您可以做类似的事情:

sf::Event event;
while (mWindow.pollEvent(event))
{
    if (event.type == sf::Event::KeyPressed)
    {
        if (event.key.code == sf::Keyboard::Escape)
        {
            // Do something when Escape is pressed...
        }
        if (event.key.code == sf::Keyboard::W)
        {
            // Do something when W is pressed...
        }

        // And so on.
    }
}

当您必须保证您的应用程序在用户按下关键键时具有焦点( 因为关键事件不是以其他方式生成的) 时, 此类型的输入处理是不错的。 当相关键被不经常按下时, 也会很好。 您可以从这里的 SFML 2. 0 教程中查看一个例子。 您可以从这里的 SFML 2. 0 教程中查看这个例子, 题为“ 按键和按键释放的事件” 的一节 : < a href=" http://sfml- dev. org/tumentors/2. 0/ window-events.php" rel= “ noreferr. org/tutors/2. 0/window-events.php

另一方面,您可能实际上需要实时键盘输入。 为此, 请使用 SFML 2. 0 s 键盘类 :

if (sf::Keyboard::isKeyPressed(sf::Keyboard::W))
{
    //  W  is currently pressed, do something...
}

使用实时输入, 您可以在特定的时间点访问输入设备状态 。 这是方便的, 因为您不必在事件处理循环中将您的所有关键检查包包在一起 。 这种方法的一个缺点是 SFML 正在读取键盘的状态, 如果应用程序没有焦点, 您的事件处理代码仍然可以执行 。 您可以在此找到所有实时输入的教程 : http:// sfml- dev. org/tuatives/2. 0/ window- inputs.php" rel = “ noreferrer” >http://sfml- dev. org/tutors/2. 0/ window- inputs. p/ a >

在选择事件处理相对于实时处理方法时要小心。 对于游戏示例, 当用户按住空间栏时, 请考虑字符点燃机关枪的情况。 如果您在事件处理循环中操作空间栏, 机关枪会错误地像半自动一样点火, 因为 < code> sf:: Event:: KeyPressed 事件对同一键的延迟, 即使用户正在按下键。 如果您使用实时键盘输入来控制空间栏, 机关枪会按预期重复点火 。





相关问题
Input system reference trouble

I m using SFML for input system in my application. size_t WindowHandle; WindowHandle = ...; // Here I get the handler sf::Window InputWindow(WindowHandle); const sf::Input *InputHandle = &...

Qt Creator / QMake Linker Libraries

I m using SFML, and I want to use Qt Creator in conjunction with it. When I m compiling manually, I supply the following arguments to the linker -lsfmlsystem -lsfmlwindow. How do I do this if I m ...

SFML Plasma Sprite Effect?

Is there a way to create a plasma effect in SFML that doesn t slow my framerate to a crawl?

How can you animate a sprite in SFML

Lets say I have 4 images and I want to use these 4 images to animate a character. The 4 images represent the character walking. I want the animation to repeat itself as long as I press the key to ...

Algorithm for count-down timer that can add on time

I m making a general timer that has functionality to count up from 0 or count down from a certain number. I also want it to allow the user to add and subtract time. Everything is simple to implement ...

SFML SetFramerateLimit Not Limiting Frame Rate

Compiler: Visual C++ OS: Windows 7 Enterprise For some reason, Window::SetFramerateLimit isn t limiting the frame rate in the app I m working on, but it works fine for others. The framerate is capped ...

Race car game, car moving faster on faster computer

I understand why it does that but I don t really have any idea of how to prevent that. So the scenario is, every frame I move the car by a certain of predefined pixels. What happens is when I go on ...

热门标签