Trap and use FORCE_RECOVERY pin in software

We are using an OEM nano based device and the recovery button is accessible to the end user.
I have noticed that if you press the button while the device is powered on with a GUI windows manger running, it will pop up a dialog and ask if you want to shut down etc…
If you running in text mode or just have a X app running fullscreen (as we do) pressing the button shuts down.

How can I trap this and prevent this from happening (as the window managers are doing?) I would like to perform my own tasks before shutting down.

It is behavior oof Ubuntu desktop. Please check if this helps: