This is called a velocity-sensitive keyboard. This is implemented by having two switches per key, positioned so that one of them closes a bit later than the other during the travel of the key. You can see some pictures of such a system on [this page](http://www.openmusiclabs.com/learning/digital/input-matrix-scanning/keyboard/index.html).
When you hit the key harder, it moves faster, and the delay between the two switches closing is reduced. The CPU then uses that information about key travel speed to alter the sound being played.
On a real piano, the sound is created by the key causing a small hammer to strike a string – the harder you hit the key, the harder the hammer will strike the string and this will cause the sound to change.
When we make digital pianos and keyboards, we are replicating the way a real piano works, so we can put varying amounts of effort into recreating this accurately.
The simplest keyboard is basically just a row of buttons – press the button and the keyboard pays back an appropriate sound. Nice and easy to manufacture, but it has some limitations in the way you can play.
We can put more effort into making a realistic piano equivalent of we want though – things like having proper weighted keys that feel the same as a piano key makes it feel more familiar than a lightweight plastic key to the player. In terms of the sound, rather than using a simple on/off switch we can use fancier switches that can also detect how quickly they transition between on and off, so the software in the piano can detect not only which key had been stuck, but how hard. Then, rather than just playing one sounds per key, it can be set to play back a range of sounds corresponding to how hard the key was struck.
Latest Answers