There are various methods. But the most common method is that under each key are 2 switches at different heights. As the key moves down, the time between the 2 switches closing is used to determine the velocity. This is not defined by MIDI, just a common design solution.
Mike thank you. This information is also important to me, but the thing I wanted to ask is what happens next. That is, what kind of mathematical function brings this potentially unlimited value back to an integer from 0 to 127. Can you help me? thank you
The General MIDI 2 specification says that "The velocity effect on volume is not defined". But it does define the response to the Channel Volume and Expression controllers as dB = 40 × log₁₀ (127 / velocity). (Many devices allow to select different velocity response curves.)
The DLS specification defines the response to Velocity, Volume, and Expression as dB = 20 × log₁₀ (127² / velocity²):