When I was little, growing up in Poland, it was customary to knock on wood whenever you did something bad (like swearing). That was in reference to confession where afterwards the priest tells you to go in peace and knocks on the (wooden) confessional three times.