touchosc2midi icon indicating copy to clipboard operation
touchosc2midi copied to clipboard

See if we can support Sysex and key commands (TouchOSC 1.9.4+)

Open velolala opened this issue 10 years ago • 2 comments

http://hexler.net/forum/viewthread/1083/

  • [x] sysex support #6
  • [ ] key command support

velolala avatar Mar 03 '15 13:03 velolala

I managed to implement support for generating key presses from /key messages fairly easily with pyautogui.

You can try it out by checking out my feature/keys branch. I've added a ToucOSC template with a nice keyboard layout, which I created to test this new feature, to the repo.

I didn't do a PR yet, since pyautogui doesn't install cleanly out-of-the-box, its code is rather messy IMHO and it also includes several dependencies for functions, which are not needed for solely generating key events. pyautogui uses X11 to inject keystrokes, which is less general than using the Linux kernel uinput facility (e.g. via python-evdev), but has the advantage, that it works without special permissions for the user.

Let me know how you like the feature and whether you think it's ok to add pyautogui as a dependency. We could also rip out the relevant code from pyautogui, clean it up and include it in the touchosc2midi package, the X11-specific part it isn't actually that big.

SpotlightKid avatar Nov 06 '15 06:11 SpotlightKid

So, I worked a little bit more on my branch and eventually decided to extract the keyboard handling code from PyAutoGUI into a new submodule touchosc2midi/x11keyboard.py, since there were several shortcomings I wanted to fix and I wanted to get rid of the other unneeded stuff in PyAutoGUI and the heavy dependencies they bring (Pillow, in particluar).

The functions from PyAutoGUI to generate X Windows keyboard events use the fake_event function from the Xlib.ext.xtest module from python-xlib. After some testing it became apparent, that this function does not work correctly, since it does not take the current keyboard mapping (language, model, variant, etc. set via the X Keyboard (XKB) extension) into account, so you get wrong key presses. E.g., if the current keyboard layout is 'de', calling key_down('z') will generate a keyboard event for 'y'.

So I implemented a little wrapper for Xlib to call the XTestFakeKeyEvent function with Cython. The file touchosc2midi/xtest.pyx is compiled by python setup.py build|install to a C-extension module, which is then imported by x11keyboard.py. When the module is not available or importing fails, because no X server is available, the main module touchosc2midi.py will emit a warning and continue without keyboard support. python-xlib is still used for mapping keysyms to keycodes, so I added python3-xlib (supports Python2 too) to the dependencies in setup.py.

SpotlightKid avatar Nov 07 '15 17:11 SpotlightKid