xev is a program used to test x functions. It may be modified to only monitor x events on a screen.
by making it own the entire desktop, be transparent and have no content any keystroke or mouse press may be intercepted and dealt with.
The above link is a browseable pointer to the source. will update on the requirements to modify it to be useful.
Use an extension xtrap and record the button events.
sample programs and info @
google capture of xtrap architecture info
the above ps seems to be part of the xtrap distro.
also suggested to use xrecord to get the events.