Just recently, I came across a dilemna why one of our pedal devices would not work with our Java program. We already incorporated the code to communicate with it by using key listeners and mapping key codes with the pedal.

Turns out that the pedal device was detected as a gamepad device. Luckily, there is an existing Java library to handle gaming device communication. I used JXInput and it worked flawlessly. Plus, the code is easy to write too.

JXInput gives access to any number of DirectInput gaming devices – although it seems to be quite unusual to have more than one connected. Additionally, JXInput allows to interpret each key on the keyboard as interpreted by Swing to be a JXInput button. This gives another source for about 100 additional buttons.

Here is a simple class I made to initialize the gamepad device to be usable in your Java program.

It is up to you to change the key code values with which you want to associate the pedal/button values. To use the class, simply instantiate it and set the Component that it should listen to whenever you step on the pedal.

To use the class, do this:

Where comp is your component UI that handles pedal functions in case users do not have any pedal device.

Related Posts Plugin for WordPress, Blogger...