You could do it by creating a custom PTP command that reports keypresses. You could do it in a module, like I did recently: https://github.com/petabyt/ptpview/blob/master/ptpview.c
For receiving the data, you could use Python ptpy and https://github.com/fujihack/fujihack/blob/master/ptp/hijack.py to run custom opcodes.
For receiving the data, you could use Python ptpy and https://github.com/fujihack/fujihack/blob/master/ptp/hijack.py to run custom opcodes.