The shift, capslock, alt, control and numlock keys are processed quite differently by the keyboard hardware. A regular key generates a code for that key that includes the state of the “shift” keys. So the key with the “4” in it produces its own code plus whether the shift, etc. keys are down or up. If the shift key is down, another layer of decoding can then be used to infer you really pressed “$”. Simple programs can also be written to just check if any given shift key (which differentiates the left and right ones) is currently up or down. (I’ve written them.) There’s also a code generated when a regular key is lifted, but not too many programs pay attention to that.
A book on assembly language programming for the IBM PC will tell you more. Other types of computers work in similar ways. (And you can even have “meta” and other shift-like keys.)
In X-windows, you can set up behavior for programs based on quite complicated combinations of shift+key presses or mouse clicks/drags. (In MS-Windows you are usually stuck with the settings of the program, unless you use an auxillary program.)