My previous answer seemed to be completely wrong, sorry. I think the correct approach would be to read from / dev / input / event 1 (?)
This short test showed scancodes for me, even if the terminal had no focus:
from struct import unpack port = open("/dev/input/event1","rb") while 1: a,b,c,d = unpack("4B",port.read(4)) print a,b,c,d
I don't know if / dev / input / event 1 is always a keyboard or how to determine which one, but at least it worked for me
source share