I don't see any rotation in your program. When I move the sphere with the arrow keys, the squares oscillate wildly, but the “code” behind this looks purely 2d.
To get all of this into 3d, you should start by using 3 coordinates, in such a way that x²+y²+z²=r². This is the condition for a point on the sphere with radius r. It might be easiest to choose r=1 and scale things later on when drawing. Next you can have a look at 3d rotation matrices. The matrix notation might be confusing at first, but expressed in terms of coordinates, it is rather simple. Take e.g. the z rotation:
xnew = xold* cos(phi) + yold*sin(phi) + zold*0
ynew = xold*(-sin(phi)) + yold*cos(phi) + zold*0
znew = xold* 0 + yold*0 + zold*1
The above is rather verbose, you could of course drop all these *0
terms completely, omit the *1
factor, and in fact leave z alone altogether. The point here is, matrix multiplication might look scary, but it isn't.
Once you have 3d coordinates, and know how to change them in response to a rotation, you can use two of the three coordinates to draw things, resulting in an orthogonal projection of the scene.