Having 2 objects being controlled and moved at the same time

I’ve created 2 spheres and I want to be able to control both of them separately with the keyboard at the same time. Right now I’m only able to control only one of them at a time. How can I go about doing this? What I’m doing is incrementing and decrementing the x and z coordinates through the keyboard.
e.g.
glPushMatrix();
glTranslatef(p1x, 0 , p1z);
drawSphere(1, 1, 1, 1);
glPopMatrix();

glPushMatrix();
glTranslatef(p2x, 0 , p2z);
drawSphere(1, 1, 1, 1);
glPopMatrix();

and then I have functions that update the p1x, p1z, p2x, p2z when the key is pressed

Anyone have an elegant solution for this?

Don’t use keypress but directly check keyup and keydown events. When you detect a key down start moving corresponding item with a timer until you get a keyup event. But you are limited to 10 keys (because you only have 10 fingers:whistle:)