C++ Tutorial #16: Keyboard & Mouse Input


In today’s tutorial, I will show you how to write mouse and keyboard input using SFML. We will create a dot for our player and program the controls so we can move the character on the screen. We will also program our game to pause using the left mouse click button. Let’s get started!


SFML tutorial setup using only .cpp filesIf you want to, you can create a blank project like we did in the last tutorial. However, we will be using only one .cpp file on each of the tutorials. I recommend that you just create a blank folder labeled “SFML Tutorials”, place all of the .dll files in that folder, and create blank .cpp in that same folder. That way, you can just open the .cpp file and just compile & run without having to create a new project every time. Now that we have everything setup, let’s open our blank .cpp file and begin coding!


Here is the code:




When you compile and run it, your program should look like the image below. To move the square around the screen, you can use the arrow keys or the WSAD keys. To pause the game, hover the mouse over the window, and press the left mouse button. To resume, press the left mouse button again.


Keyboard & Mouse Input Project Compiled




Let me explain how this works:


We are only handling graphics in the tutorial, remember to include the graphics.hpp.



This section of code renders our window, using sf::VideoMode, we set our screen width to 480 pixels, and our screen height to 360. Next, we gave our window a title “The Epic Adventures of Red Square”.



In this section, we create a rectangle using sf:RectangleShape and named it “hero”. Then we set the position of the hero using setPosition. The first number we use in the parameters is the x-position, and the second number is the y-position. Next, we set the size of our hero using setSize and use sf::Vector2f for the parameter because this is required for the setSize function. Inside the sf::Vector2f parameters, we set the width and height of our hero. Last, we use the setFillColor  function and set the color of our character to red.



This boolean will be used later to handle what happens when the game is paused or resumed.



This whole section of our code is the game loop, remember that this is where everything else is after we initialize our contents of the game. The game loop handles updates and draws our images on the screen. I already explained all of this in a previous tutorial, so let’s take a closer look at the Mouse Button Pressed comment.



This section of code is inside of a switch statement, and handles the event of a mouse button being pressed. Inside of this case, we place an if statement to check if the left mouse button is pressed. If the condition is true it will either pause or resume the game. Notice, inside the if statement we have another if statement. This is where we check if the game is paused or not.



This section of code is where we handle our keyboard input. If the game is not paused, then the program will handle the controls. We also use if statements to handle when the key is pressed. Notice the two vertical lines ( || ) between the two conditions, it’s an operator that means “OR”. When either condition is true, we use the move function to move our hero. In the move function, the first parameter is the x-direction, and the second parameter is the y-direction. Also, when our game is not paused we have the color of our hero stay red. That’s because when our game is paused (else), we change the alpha of our hero to 128. Remember the sf::Color function can also have four parameters if you want to include alpha. Example: sf::Color(Red,Green,Blue,Alpha)



The section of code obviously displays the window, clears the screen, and draws our hero. Take a look at the parameters we set in the clear function. That’s right! We used sf::color to change the background color of our program.



I hope you’ve enjoyed this tutorial. On my next tutorials, we won’t be using SFML. We will get back to boredom and work on namespaces, classes, and polymorphism. Don’t worry, we will get back to SFML, but before we do, I believe it’s really important to learn this other stuff first. That way, we can write more efficient programs.


About Justin

Computer programmer and owner of kauergames.com.
Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.