How to use one function for many blocks

Here are some sample projects for you to read that do what you want ...

(A bad example, written before generic events became available ...)

Better ...

Another ...

Lastly, if you don't mind browsing github projects ...