Thank you for asking about you hamster on Just Answer. FIrst, I am sorry that you have found him this way and I can tell from your writing that you are very worried about him.
If he is over 2 years old, although that is not elderly for a hamster, it is getting in the range of their normal lifespan and what you are seeing does sound like a stroke. If you are seeing him moving his legs at times in what looks like a bicycle peddling movement, he probably has had a stroke and there is not a lot you can do for him right now, but offer him water or some diluted pedialyte.
He is not in pain, in fact he may barely be conscious of what is going on around him. Some rodents can recover from strokes and be able to feed and drink still, you will usually know if he can within 72 hours. If not, you need to consider humanly putting him down, that is the last act of compassion you do for your pet.
Stroke is not the only thing that might be causing this, there are virus and bacteria infections that also can, but again, those if they are affecting him neurologically do not have a good recovery prognosis. Only a vet cold check your hamster and might be able to tell you for sure what is going on with him, be able to help him or/and also put him down with medications so he does not suffer.
Again, I am sorry and I wish I had a better outlook for you, but I think you also realize that this is very major to him and only within the next few days can you tell if he has any chance of recovery. Also, if you have any doubts about what is going on with him, or his chance to recover, you should seek a vets exam of him and advice. Most hamsters or mice that do have strokes, do not survive but a few days unless they can recover some from them. Keep him warm and hydrated to avoid shock to him right now. Best to you and your little pet, please let me know if you have any other questions or needs regarding this.