While reading about CRT on Wikipedia, an information (CRT Allows the use of light guns/pens; https://en.wikipedia.org/wiki/Cathode_ray_tube#Advantages_and_disadvantages) suddenly triggered a a vast lots of memories.
Around early 1990's when I was a toddler, in a local "mela" or fair each year, there was a stall for video games .
They had several big boxes (maybe CPUs), several tiny to large sized, portable monochrome televisions with a mesh of cables. They provided any 1 of 2 kinds of input devices, 1. a game-control with arrow-keys & fire-key etc; as-well, 2. a toy Gun-shaped device.
(The above diagram drawn completely based-upon memory.)
In the game, to destroy the enemy (a rocket, or an octopus, or a ghost etc), the tip of the toy-gun were almost-touched on that object on screen, and on pressing the trigger, the selected rocket were destroyed.
It was, specifically surprising to the kids, because that was a pre-touchscreen era. The television set, acted quite like a touchscreen.
Now I want to know, how that systems worked?
Which-one is acting as the sensor (of the particular place where the rocket was fired)? the toy-gun or the CRT? What is the mechanism to identify a particular spatial-position (of target-object such as rocket) out of many-other objects (other rockets)?
The CRT TV picture was generated by deflecting an electron beam fired from the back of the tube in a raster scan as shown in Figure 1. Two sawtooth oscillators control this: one for the vertical and one for the horizontal - the horizontal one running much faster than the vertical. For PAL used in Europe there are 625 lines (horizontal) refreshed at 25 frames/second (vertical). (It's a little more complex as the lines are interlaced to reduce flicker. The vertical scan runs at double the frequency alternately drawing the odd and even lines.)
Figure 1. CRT TV raster scan. Source: Wikipedia Analog Television.
The electron beam excites the phosphor on the inside of the tube making a sudden step change in the brightness of the dot. This then decays until the next refresh. The persistence of the dot was a balance between keeping bright between refresh and "motion smear" if it was too slow.
The gun contains the required optics to view a small spot on the screen and a fast light sensor. Some analog logic is required to keep track of the vertical and horizontal dot position relative to the position of the on-screen "target" and match this with the step change in light level by the gun sensor. If the two coincide at the moment the trigger is pressed a "hit" is registered.
Note that this system could only work with bright targets as there would be no dot impulse on black or very dark areas of the screen.
I think @Passerby's answer is more accurate and obviously a much simpler method of working without the timing issues of my answer in the case of the gun. I predate video games. He obviously had a mis-spent youth - although it has paid back on this occasion.
Figure 2. The cutting edge - once upon a time. This would be easier to date if we could see how wide his trousers flares were.
The method I have described is how the light pen worked - to the best of my recollection.