On grands, the key length is determined by the hammer's need to be under a particular point on the string, and where in the action the key must sit. That is a gross simplification, but Del Fandrich can give more details than most of us are likely to understand, if more info is needed.
Thatâ€™s pretty much it. Pianos are laid out from their hammer strikeline out. The strikeline is an imaginary line in space that indicates whereâ€”at what point along the speaking lengthâ€”the hammers are going to hit the strings. Generally when a new piano is designed there will be some specific overall length in mind. In other words, weâ€™re going to design a 175 cm grand or a 275 cm grand. Whatever. So, starting with the strikeline weâ€™re going to extend the speaking portion of the strings both ways both toward the back of the piano (the tail) and toward the front of the piano where the agrafes, tuning pins, etc., are located. There will be some proportion of the speaking length that must be placed between the strikeline and the agrafe. There will also be some length from the agrafe to a bearing surface and then on to the tuning pin(s). For physical reasons there needs to be some area around the tuning pins and then there is the front cross-strut giving stability to the front edge of the string frame. And then there is (usually) a cross stretcher of some kind that provides closure to the rim and keeps things looking pretty. Finally there will be a keycover of some kind and then we can position the front ends of the keys.
Going back to the strikeline; this line positions the hammers and the hammers more-or-less position the rest of the action including the wippens. While there is no absolute position for the capstan block along the bottom of the wippen body it usually falls within a fairly narrow range so the capstan is generally positioned between 90 and 100 mm from the strikeline. There is no absolute here and there are good reasons for going both waysâ€”piano design is nothing if not compromise.
So generally speaking the key length is going to be determined by the position of the strikeline, the length of the string(s) from the strikeline forward to the agrafe, the space between the agrafe and the tuning pin field, the amount of space allowed around the tuning pin field, the thickness of the stringframe cross-strut, the thickness of the stretcher, the amount of space required by the keycover and the lengths of the keys across the key headscale. (And Iâ€™ve left out a few incidentals that might add a few mm here or there.)
There are no absolutes for any of these things but, in general, piano designers over the decades have tended to make these dimensions as short as practical so that more length can be put into the speaking lengths of the strings. Occasionally a designer will make one or more of these things abnormally long in order to provide a longer key in a piano of a given overall length. The Law of Compromise always dictates that something must be compromised when this is done. The speaking lengths of the strings can be made shorter; string angles can be increased, etc. There is, sadly, neither a free lunch nor a free longer key.
On the bigger pianos, that optimum strike point is farther down the string, away from the pianist, and the action needs to be "out there" so the hammers and sweet spots can find one another. The actions are the same, so the keys are longer to get it out far enough So, the larger the piano, the longer the key. For simplicity, most pianos under 7 ft. have keys of equal length from top to bottom, but concert grands have keyboards that increase in length, with the bass longer.
Some shorter pianos as well. Iâ€™ve seen this done on pianos as short as 185 cm. Iâ€™ve also encountered a couple of 275 (Â±) concert grands with keys of equal length.