gnome-screensaver emits some signals on dbus when something happens.
Here the documentation (with some examples).
You could write a scripts that runs:
dbus-monitor --session "type='signal',interface='org.gnome.ScreenSaver'"
and that does what you need anytime dbus-monitor prints a line about the screen locked/unlocked.