Knocking on wood, or to touch wood, refers to the apotropaic tradition in western folklore of literally touching/knocking on wood, or merely stating that you are doing or intend same, in order to avoid "tempting fate" after making a favourable observation, a boast, or declaration concerning one's own death.
In old English folklore, "knocking on wood" also referred to when people spoke of secrets – they went into the isolated woods to talk privately and "knocked" on the trees when they were talking to hide their communication from evil spirits who would be unable to hear when they knocked. Another version holds that the act of knocking was to perk up the spirits to make them work in the requester's favor. Yet another version holds that a sect of Monks who wore large wooden crosses around their necks would tap or "knock" on them to ward away evil.
In Romania, there is also a superstition that one can avoid bad things aforementioned by literally knocking on wood ("a bate în lemn"). One of the possible reasons could be that there is a monastery practice to call people to pray by playing / knocking the simantron.
In Bulgaria the use of "knock on wood" is basically for protection against the evil news (not necessary for luck or anything else). The people use it mainly against illness or if they heard that something very bad has happened to some one far away, or if a bad word or news is heard. In this "knock on wood" ritual you must say "God keep us" and knock on the nearest wood (except on wooden table) then pull your ear (on the bottom) with the same hand you knocked. If there is not a wood near by its allowed to knock on your head. The knock on your head instead of wood comes from the ironical idea that "the stupid man has a wooden head"
In Serbia there is also the habit of knocking on wood when saying something positive or affirmative about someone or something and not wanting that to change. Frequently the movement of knocking on nearby wood is followed by да куцнем у дрво (I will knock on wood), or sometimes by да не урекнем (I don't want to jinx it). 
In Poland, as well as in Russia, there is a habit of knocking on (unpainted) wood (which may be preceded by saying odpukać w niemalowane drewno or simply odpukać, literally meaning to knock on unpainted wood) when saying something negative - to prevent it from happening - or, more rarely, something positive - in order not to "spoil it".
On an episode of the American TV show The Rifleman, first aired 11/05/1962, an Irish female character stated 'knocking on wood' was done to alert Lepercahns that a good deed was needed by the person doing the knocking.