It does not happen only when I play piano but the problem is quite bad when I do. I remember once they were just shaking so much in front of my piano teacher it was quite odd.
I had an x-ray and all seemed fine--I feared I had a carpal problem--but I wonder if an x-ray can even identify anything. I would imagine, I think this is the name, a CT scan would help identify a problem--if there is one.
I can barely write with a pen for too long without my hands just giving up (yet I still do good on exams thank goodness!). I sadly have the chicken scratch penmanship (without the medical degree to justify it).
Since your hands are shaking at other times and become weak when writing,,,,,,,,,,,,,,,,,,,
Please go see a doctor, and do not rely on internet advice, no matter how well intentioned. No one can diagnosis the cause without proper testing.