Move with spline with key hold

Hi,
I am working on a c ++ project in which I want to make a simple tram running on the rails after clicking “w” there is gear 1, after clicking “w” the second time is gear 2 and after clicking “s” gear lower.
I’d like to do this with a spline (unless someone recommends some other easier way).

Everything has to be simple, it doesn’t have to be advanced.

I just mean that
can it be done? If so, there is a guide for it, or someone can explain where to start

Thank you in advance
Regards