Thanks for a great question! There is a lot of variation in this so I’ll concentrate on basic principles and start my answer by telling you a short story.
Image credit: Brandy Kennedy courtesy of Unsplash.
One evening, Finn and Gers were walking past the abandoned stately home of Ty-Pin Manor and Finn wondered if it was haunted. “Nonsense”, said Gers, “I’ll look inside, you wait out here”. Finn didn’t think that exploring old houses in the dark was a very good idea but Gers had already gone!
Finn noticed many windows and wondered which room Gers was in. Gers had noticed that each room had a light switch. She switched one on and the light revealed many treasures. Gers turned off the light and tried the next room along. Finn realised that, as long as only one window was lit, he would always know where Gers was.
Gers wanted Finn and her other friends to see the treasures too and tried to describe the order in which she had visited the rooms. This was hard because the house was so big. “I know”, said Finn, “why don’t we put a number in each window? Then when you turn on the light, I can write the number down. When someone visits, they’ll know which room you were in because the numbers will match.”
They drew a sketch and numbered each window. When Gers lit up a room, Finn wrote down its number.
When they had finished, they had a sequence of numbers that described precisely the order of Gers’ visit. They carefully stored the information and went home for tea.
The story of Finn and Gers illustrates what happens when a key is pressed on a keyboard. Each key has a switch under it. Pressing a key is like Gers turning on a light in Ty-Pin Manor. Finn could see where Gers was because electricity flowed through the light switch and lit up the bulb. Similarly, in your keyboard, the amount of electricity flowing through the key switch changes when pressed and the keyboard has a ‘digital’ Finn that can see which it is.
Notice that Finn and Gers turned Gers’ physical actions into information: they could represent Gers’ actions simply by a sequence of numbers. Your keyboard does something similar. Each key (and combination of keys) has a unique number (a scan code). The keyboard sends the scan codes to the operating system of the computer (e.g. Windows). What happens after that varies a lot. There will probably be one or more tables that map things like the scan codes to characters (and unique numbers for those characters e.g. ‘A’ = 65, ‘a’ = 97) and map those to descriptions of the shapes that actually appear on the screen. We use maps because it’s easier to change a table entry than to change a whole keyboard to make a key press display something different. Getting the character on the screen involves (very roughly) creating a representation of the character in the appropriate place in the computer’s memory from where the graphics hardware can tell your screen what dots (pixels) to display so that we perceive a character.
In summary, to get a character on screen when you press a key, your keyboard observes which switch has been closed by the pressed key, sends the scan code for that key to the operating system, and then one or more programs use a series of maps to look up the character shape to be displayed based on the scan code. As with all software, there are lots of ways to achieve the same end result!