|
From: Florent R. <f.r...@fr...> - 2015-12-10 18:12:13
|
Hello,
"Ebbesen Soren" <seb...@id...> wrote:
> I have connected a DIY game controller to my Raspberry Pi. I would like to
> navigate a dialog with the output of the game controller rather than a normal
> keyboard. I've created a Python script that can read the output of the game
> controller and store it in a variable, so that part is working. I just need to
> fix something somewhere to change from the keyboard to my own custom variable
> which is triggered by the controller. Can this be done?
>
> I looked through Dialog.py but I didn't see anything about keyboard events.
Correct, this is handled at ncurses level, on which dialog is based
(maybe directly in dialog sometimes too, but I don't think so).
> Maybe the right place to fix this is not even in pythondialog but rather in
> the underlying dialog utility?
I don't think you need to modify dialog for this.
> I've actually made it work using pygame and ncurses directly but I would like
> to take advantage of the features of pythondialog to build a proper dialog.
I think there are mainly two ways to do what you want:
- send appropriate byte sequences to the controlling terminal. I would
try writing to a device returned by os.ttyname(). You should be able
to obtain the codes for arrow keys for instance with something like:
cat >/tmp/whatever
Then type Ctrl-V followed by the key you are interested in, then
Enter, Ctrl-D to close the file, then inspect /tmp/whatever with a
hex editor or od(1).
Another approach at the same level would be to use pseudo-terminals
(pty), I believe. Python has os.openpty() and a 'pty' module for
this.
I have no experience with this kind of stuff, so I can't promise it
works this way.
- simulate the keyboard events at kernel or X server level. I have
good experience sending mouse events to the Linux kernel using
python-evdev:
http://python-evdev.readthedocs.org/en/latest/
but it can also send keyboard events. There are other modules doing
the same, I only know this one works. This approach requires write
permissions to /dev/uinput; you may want to use an udev rule such
as:
SUBSYSTEM=="misc", KERNEL=="uinput", GROUP="your-group", MODE="0664"
to automatically set this up at each boot.
There are also many tools/hacks to simulate events at the X server
level (xdo, PyAutoGUI...) but my attempts with this approach have
not been successful. I remember trying XSendEvent and
XTestFakeKeyEvent in C++ as well as xdo from the command line, but
this didn't work well for me.
Finally, unsuitable for dialog, but using D-Bus to control other
programs can be very effective. Using the following simple patch:
|