So I have a macro style keyboard that comes pre-programmed for a specialised piece of software, I want to use a Pi to take those keypresses and translate them to OSC, which I have a python library for, however I can't find anyway to capture keyboard presses that include modifier keys, for example, 'c' does x, 'alt c' does y, 'cntrl c' does z and so forth. Everything I can find is either for graphic environments (This will be run on a headless server) or for a single character with no modifiers, it mainly the modifiers I am struggling with. Whats the best way of doing this? I'm happy if the detection is done in something other than python but would prefer python for speed reasons (So I can open one OSC connection at the start instead of with each command).
1 Answer
you can use curses library in python. (https://docs.python.org/3/howto/curses.html)
each keypress returns a number. 'c' is 99 and 'C' is 67, so you can use this to check when Shift+Key is pressed.
try this code to get the letters code:
damn = curses.initscr()
damn.nodelay(1) # doesn't keep waiting for a key press
try:
while True:
c = damn.getch()
if c > 1:
print (c)
I used it to get 'w' and 's' key presses, so this is my code:
while True:
c = damn.getch()
if c == 119: # W key
sleepTime += 1
elif c == 115: # S key
sleepTime -= 1