When a key is pressed, keyboard sends signal to device driver which interrupts CPU and interrupt handler is run. The pressed key is stored at address mapped to keyboard interrupt. My question is: If there are multiple processes say, editor and shell, how does kernel decide which one the received key press belongs to? I mean how does this key press get to it's destination?
Also is there way when key press goes to multiple processes?
I'd suggest reading up on evdev and X11, evdev is the mechanism within the linux kernel for dispatching input events (such as keyboard) X11 is the window manager.
In a multiwindow environment (i.e not a VT), the window manager (X11) determines where the keypress is routed too. Something like xdotool should be able to send keystroke to multiple windows at once. Wayland is slated to replace X11 I do not know much about how it works but it uses evdev as well.
This is what I found after reading some resources:
There is something called Foreground process groups, where among the process groups in a session at most one can be the foreground process group of that session. The tty input and tty signals (generated by ^C, ^Z, etc.) go to processes in this foreground process group.