Bayonet for vanilla
Some people say you should aim first, then press the spacebar. It attacks like a normal spear. But I found from the tests that this does not work, maybe it is a bug.
Bayonet for britas weapons
To use a bayonet to melee attack zombies, you need to equip the bayonet and then switch to melee mode.
To use melee mode, right-click on the weapon and select the mode that it’s currently not in. For example, if you’re using a rifle in single-shot mode, you would right-click on the rifle and select “melee mode.” This will allow you to use the weapon as a melee weapon, such as a bayonet.