PSP Programming/General/Button Input

Button Input
In this tutorial we will learn how to work with handling user input. Let's start by including some headers. You should already know what the first two are for, and the third one is to get controller input. As before we will also include the callback header.

We'll raise the revision so that our program is now at version 1.1. This is assuming each progression of tutorial deserves a raise to the revision. The rest of the code is the same aside from the name change in the psp module info.

In our main function we do the usual, but this time create an 'running' variable so that we can update it if the exit callback occurs or if the user presses the button 'start' (more on this later). Then we create the button input object and set the sampling rate to 0, and allow analog mode so that we can read where the analog pad is.

Now we start the while loop, update our variable from the exit callback telling us if we should quit or not. We reset the position to print, and print out the analog pad position. Printing the variable is done by passing it in the second parameter and using '%d' as a placeholder for it.

One thing to remember, when you get the position of the analog stick using the 'Lx' and 'Ly' properties, you get a value from 0 to 255. You can subtract 128 from that so that the center would be 0. Unfortunately there is such a thing as a “dead-zone” which means that the user might not even be touching the analog stick, but it may still detect a value between -30 to 30. We'll learn how to deal with this problem in later tutorials.

To update our button input variable with the button information we call 'sceCtrlPeekBufferPositive' and give it the reference to our variable. When we want to check what buttons are pressed we first check if any buttons are pressed at all. If the 'start' button is pressed, we print it out and then close our program by making 'running' equal to zero.

We do the same thing with the other buttons, but we do not close the program, just print out the name of the button.

At the end we close our program. You may notice that we don't check for all buttons, this is because we need kernel control to check for those buttons. More on that in later tutorials or if you want to check how to get kernel control in your program, you can skip right to it.

Makefile
Once again create the 'Makefile' file and make it like so, the only difference being the names again.

Once you're done with this and everything is working, you can proceed to the next tutorial.