Those devices are firearms. Guns.
How guns work is obvious, or it was until a little over a century ago. You have a strong tube, stopped at one end. Into it goes some fast burning propellant: gunpowder or modern 'smokeless' powder. On top of that goes the projectile: the bullet. The propellant is ignited. It burns rapidly, producing gases which have nowhere to go, so they push out the projectile.
The propellant can't be just any old flammable thing. Burning is a chemical reaction in which whatever is being burnt is combined with oxygen. For really fast burning, there isn't enough oxygen in the immediate vicinity to sustain it, so gunpowder and its successors incorporate oxidants to provide all the necessary oxygen.
That takes us up to the 19th Century. Around the middle of the century a new innovation appeared: the fully self-contained cartridge. This combined the propellant, projectile and primer (the ignition device) in a brass cartridge. It was soon found that this had a number of advantages when the enemy was charging.
Revolvers predated the cartridge, and six shots of slow-loading loose powder and ball were a lot better than just one. But a revolver with six cartridges which could be reloaded in seconds was even better. But there was more to come.
The genius of firearms design was John Moses Browning who over the decades around 1900 lodged 128 firearm patents. Amongst his designs are some of the most famous automatic handguns ever built, such as the Colt .45 automatic, used by the US Army until the 1980s, and the Browning Hi-Power 9mm automatic (which this writer carried in the 1980s when he was a bodyguard). For long-arms (rifles and the like), automatic means machine gun, whereas semiautomatic means self loading. For handguns, automatic means self loading.
Automatic handguns have an advantage over revolvers in their size. Because the bullets are all lined up in a magazine, they are flatter, unlike a revolver with its bulky cylinder holding five or six rounds. They can also carry more bullets. The Colt .45, for example, holds eight while the Browning 9mm holds thirteen.
Again, the concept seems simple. The round fires. The recoil pushes back the brass casing, which flicks out, then the spring-loaded bolt behind it pushes forwards, picking up the next round from the top of the magazine and pushing it back into the firing chamber. But, in fact, it is a bit harder than that.
With the light components in a handgun, the cartridge would push back too fast. As the brass cartridge emerged from firing chamber, all the pent up gases in it would blow out its sides.
They key to all self-loading guns, whether rifles, submachine guns or handguns, is that they have to delay the extraction of the cartridge until the bullet has left the barrel to allow the gas pressure to ease. Some lock the chamber, and use a pushrod ported halfway up the barrel (like the old Australian Army SLR rifle) to release it. Some just use a really heavy bolt that's hard to get moving (like the old Australian Army F1 submachine gun). But neither of these schemes is suitable for a light, handheld firearm.
The Browning approach was to use a movable barrel. The firing chamber and barrel are a single unit. At the front, the barrel is held in place by the slide -- the movable section on the top of the gun that goes all the way back when the gun fires. At the back, an angled slot on the bottom of the chamber fits over a lug inside the gun. On the top of the barrel, machined ridges fit into underside of the slide, so the barrel and slide move as one.
But only for a few millimetres. The angled slot drags the rear of the barrel down, disengaging the ridges, so the slide can continue on its way right to the back, while the barrel stops. But by that time, the bullet has left the chamber and is, hopefully, on its way to the target.