Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Keyboard not working  (Read 1362 times)

0 Members and 1 Guest are viewing this topic.

firewolf

  • Newbie
  • *
  • Posts: 2
    • View Profile
Keyboard not working
« on: September 06, 2018, 02:46:57 am »
#include <SFML/Graphics.hpp>
#include <iostream>

int main()
{
        //Render Window
        sf::RenderWindow window(sf::VideoMode(200, 200), "Demo");
        //Render Objects
        sf::RectangleShape shape(sf::Vector2f(100,100));
        //Move code
        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Right))
        {
                shape.move(1, 0);
        }
        //Window Stuff
        while (window.isOpen())
        {
                sf::Event evnt;
                while (window.pollEvent(evnt))
                {
                        if (evnt.type == sf::Event::Closed)
                                window.close();
                        switch (evnt.type)
                        {
                        case sf::Event::Closed:
                                window.close();
                                break;
                        case sf::Event::Resized:
                                printf("New Height: %i New Width: %i\n", evnt.size.height, evnt.size.width);
                                break;
                        case sf::Event::TextEntered:
                                if (evnt.text.unicode < 128)
                                {
                                        printf("%c", evnt.text.unicode);
                                }
                        }
                }

                window.draw(shape);
                window.display();
        }

   return 0;
}

It seems to skip the if statement on line 11
I've tried everything from fixing simple errors to completely reinstalling the library but nothing is working, any idea of what is going wrong?

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: Keyboard not working
« Reply #1 on: September 06, 2018, 06:00:31 am »
It's outside of your game loop, so it gets called once at start up.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

firewolf

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: Keyboard not working
« Reply #2 on: September 06, 2018, 01:31:28 pm »
I moved it into the game loop and sure enough that fixed the problem, thank you for your time.