B4J Library JoyPad (only Windows OS at present)

Was playing around with JNA and ended up writing a library for joystick/joypads.
This currently only works on Windows OS.

It uses the windows winmm.dll to read the joysticks

It was compiled with Java 8. Also you will need to find copy of jna-3.1.0.jar I have not included it, as I am not sure of the licencing on it.

The library (JoyPad) can give you (on a ps3 controller for example) the dpad, left and right joysticks and the buttons.
It handles multi joystick as in left and right joystick on the controller, and as many buttons as you can press at once.

There is a demo B4j project that just displays the values read from the joypad.


There are 3 routines.
1, Initialize routine - JoyPadInit (no parameters)
2, readJoystick() - this takes one int parameter, the joypad/stick to read.

3, unloadDLL - this unloads the DLL (winmm) probably not a good idea to do while using the library.

I have only one joypad so cannot test how two perform together, but I suspect it is safer to make two joypad entries, one for each joypad.
ie,
B4X:
private joypad1 AS JoyPad
private joypad2 AS JoyPad
Then use joypad1.readJoystick(0) and joypad2.readJoystick(1) etc

I may try and find how to get it to work on mac and linux too.

Added joystickdemo2 - moves a circle around the screen under joystick control.

Added joystickdemo3 - similar to joystickdemo2 but shows how to handle button presses.

Added the wrapper so others can see how to access system .dll's
 

Attachments

  • joypaddemo.zip
    6.6 KB · Views: 433
  • joystickdemo2.zip
    2.4 KB · Views: 353
  • joystickdemo3.zip
    2.8 KB · Views: 368
  • JoyPadWrapper.zip
    843 bytes · Views: 451
Last edited:

Daestrum

Expert
Licensed User
Longtime User
Added new demo (joystickdemo3) to show how to handle button presses on joy pad.
 

ilan

Expert
Licensed User
Longtime User
thank you very much for that. just discovered it today (thanx to @Erel)

i have a simple Joystick (https://www.hookedgamers.com/tech/2014/01/24/speedlink_dark_tornado_joystick.html)

it has 4 buttons on the joystick and another 4 buttons on the left side + a Throttle stick and EVERYTHING works with yout lib so everything is triggered and thats awesome.

it would be very cool if we could use it also for mac os. like that i could make b4j games with SGA and add joystick support. anyway for windows it will work fine so thank you very much for this library!! :)
 

ilan

Expert
Licensed User
Longtime User
btw is it possible to apply Force (vibration) to the joystick using your library?

thank you
 

Daestrum

Expert
Licensed User
Longtime User
I had a look at winmm but never saw any reference to sending commands to the joystick to cause vibration etc.

There is a library JXInput (on GitHub) that can (or so the write up says), plus its in java so should be wrapable.
 

Marc De Loose

Member
Licensed User
Longtime User
Is there a way to list the connected controllers? The order of the Joystick can change. If you unplug joy 0 then joy 1 becomes 0. That can cause mayham :)

EDIT. Apparently the order does not change. But listing the controllers is still on my wishlist :)
 
Last edited:
Top