3

I'm using a Raspberry Pi running Raspbian to power my arcade cabinet. I want to shut the Pi down using the sudo poweroff command without using the keyboard. I'm thinking the best way would be to create a simple script that would somehow be triggered by a combination of controller button inputs. It seems easy to bind a keyboard to a script, but not so easy to use a joystick input instead.

Does anyone have any ideas?

EDIT #1

The joystick and controller buttons all work fine and show up using the jstest tool from the jstest-gtk package and all work fine in Mame4all. I haven't tried xev but will look into it.

  • Are the joystick buttons/movements recognized by xev? – terdon Jan 4 '14 at 12:52
  • Thanks again for the replies. I have the joystick package installed and jstest recognises all inputs. How exactly do I use the other executables from the package to run a script based on a combination of inputs? Thanks in advance – Jody Jan 5 '14 at 8:45
  • Jody, welcome to the site. If you want someone to be notified of a comment, either comment on their post or @mention them by name, e.g. @slm. If one of the answers solves your problem, please mark it as accepted. – terdon Jan 5 '14 at 9:10
  • Jody, I've updated my answer with some additional info on tools and a method that shows how you can bind keyboard shortcuts to run a script. – slm Jan 5 '14 at 14:11
  • I have found this which appears to get input from each axis via pygame. I have yet to test/adapt it :) – Wilf Oct 23 '15 at 16:45
2

xev

I do not believe you can use xev to detect joysticks. I'm using Fedora 19 and there is a package called joystick which installs this package, linuxconsoletools. This project is what you're looking for.

linuxconsole project

It contains a number of tools for hooking joysticks up so that their signals are received by the Linux Kernel.

excerpt from linuxconsole website

This project maintains the Linux Console tools, which include utilities to test and configure joysticks, connect legacy devices to the kernel's input subsystem (providing support for serial mice, touchscreens etc.), and test the input event layer.

The package includes executables such as these

$ rpm -ql linuxconsoletools.x86_64
/usr/bin/ffcfstress
/usr/bin/ffmvforce
/usr/bin/ffset
/usr/bin/fftest
/usr/bin/inputattach
/usr/bin/jscal
/usr/bin/jscal-restore
/usr/bin/jscal-store
/usr/bin/jstest
/usr/libexec/joystick/extract
/usr/libexec/joystick/filter
/usr/libexec/joystick/ident

Ubuntu

I do not have a Debian installation available but Ubuntu also has the same package, joystick. Running the command apt-file list joystick shows the same files as the RPM I mentioned above.

What else?

I would also suggest taking a look at the ArchLinux Wiki topic titled: Joystick. It's probably the most exhaustive resource on the topic, related to Linux.

Mapping joystick to keyboard

The above will get your joystick working in Linux, but doesn't offer any method for mapping joystick to a script, at least not in a easy fashion. To do something like that you'll need to enlist the help of another application. There is one that I'm familiar with called jkeys.

You'll need to install some dependencies first to install it, python-xlib & python-pygame. These should be in most distros' repos.

However to install jkeys you'll have to download it from the project's subversion repository. NOTE: It's written in Python.

$ svn checkout http://jkeys.googlecode.com/svn/trunk/ jkeys-read-only

After downloading it you can run it like so:

$ ./jkeys 
usage: jkeys joystick.conf application [params]

To map a key you'll need to create your own joystick.conf file:

<config>
    <joystick id="0">
        <axis number="0" low="Left" high="Right" />
        <axis number="1" low="Down" high="Up" />
        <button number="0" key="Space" />
        <button number="1" key="Return" />
        <button number="2" key="a" />
        <button number="3" key="b" />
        <button number="4" key="c" />
        <button number="5" key="d" />
        <button number="6" key="z" />
        <button number="7" key="x" />
        <button number="9" key="Escape" />
        <button number="10" key="p" />
    </joystick>
</config>

Now when you're ready to run your game you do it like so:

$ ./jkeys joystick.py <somegame>

The overridden joystick buttons will send whatever key you press instead of the joystick buttons to the app.

QJoy

Another alternative is to use the app QJoyPad. I was unable to test this since I have no joystick. The GUI should look like this.

                               ss of qjoypad

From this GUI you could map any button presses to keypresses fairly easily.

What about running a script?

Well with the above methods you could make use of another application called XBindKeys to map any keyboard presses to run an actual command or script.

NOTE: My example below creates a keyboard shortcut so that Nautilus will launch with certain directories opened.

Example

You'll need to first make sure the packages xbindkeys is installed.

Then you'll need to run the following command, one time only, to create a template xbindkeys configuration file.

$ xbindkeys --defaults > /home/saml/.xbindkeysrc

With the file created you can open it in a text editor and add a rule like this:

"nautilus --browser /home/saml/projects/path/to/some/dir"
  Mod4+shift + q

With the above change made we need to kill xbindkeys if it's already running and then restart it.

$ killall xbindkeys
$ xbindkeys

Now with this running any time I type Mod+Shift+Q Nautilus will open with the corresponding folder opened.

References

| improve this answer | |
  • The OP's last comment was for you I think. – terdon Jan 5 '14 at 8:48
  • 1
    Thank you so much for your comprehensive answer. I think I almost have all I need to do what I want. The final piece of the puzzle is how to map a combination of button presses to a key or to run a script. Thanks again, Jody. Edit: actually, I think you've shown me how to do that already, sorry. I'll have a go and report back. Cheers – Jody Jan 6 '14 at 10:22
2

I went a little bit different route and developed a lightweight background daemon to run a script once special buttons are pressed:

https://github.com/workinghard/jslisten

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.