Reading the Joystick Interface

Most Intel PCs have one or two joystick ports. Usually, these are located on the sound card.

Under DOS, it was possible to directly access these ports. However, Windows 95 and above forbid direct hardware access. Therefore, this page exists to help get around windows.

Visual Basic | Delphi | C++ Builder | Java | Windows API

Generic References

Windows provides two ways to read the joystick, an input only device.

Visual Basic 6.0


Delphi 5 does not supply joystick support via the vcl.
Windows API support is defined in rtl\win\mmsystem.pas

The following code snipet is from The Unofficial Delphi Developers FAQ.

  ... , mmSystem;

  myjoy: tjoyinfo;
  trackbar1.position := myjoy.wypos;
  trackbar2.position := myjoy.wxpos;
  radiobutton1.checked := (myjoy.wbuttons and joy_button1)>0;
  radiobutton2.checked := (myjoy.wbuttons and joy_button2)>0;
This snippet is pretty good, but only one radio button can be set at a time. Also note that the position range is 0 to 65,535 when calibrated. Be sure to set the trackbar max values appropriately. (On my test system, the center position is 38,500 x 26,900.)

Using a Message Handler

mmsystem.pas does a pretty good job of encapsulating the Windows joystick interface - all the functions in the Windows SDK Help file appear to be available. The only thing missing is a record definition so that you can use a message handler. This is the one I use. There is one interesting inconsistency The following code implements a message handler.
procedure MMJOY1BUTTONDOWN (var LocMessage: TMMJoyStick); message MM_JOY1BUTTONDOWN;
procedure MMJOY1BUTTONUP   (var LocMessage: TMMJoyStick); message MM_JOY1BUTTONUP;
procedure MMJOY1MOVE       (var LocMessage: TMMJoyStick); message MM_JOY1MOVE;

  // ButtonDown and Move are not shown, they just call ButtonUp
procedure TForm1.MMJOY1BUTTONUP (var LocMessage: TMMJoyStick);
  trackbar1.position := LocMessage.ypos;
  trackbar2.position := LocMessage.xpos;
  Edit1.Text := IntToStr(LocMessage.ypos);  // so you can see 
  Edit2.Text := IntToStr(LocMessage.xpos);  //   what is happening
  CheckBox1.checked := (LocMessage.Buttons and joy_button1)>0;
  CheckBox2.checked := (LocMessage.Buttons and joy_button2)>0;

procedure TForm1.FormCreate(Sender: TObject);
  myJoyCaps: TJoyCaps;
 joyGetDevCaps(joystickid1,@myJoyCaps, sizeof(myJoyCaps)); // for test
 joySetCapture(self.Handle, joystickid1, 100, true);

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);

Several free and shareware components are available via Torry's Delphi Pages.

C++ Builder


Windows API

Search the Windows SDK for Joystick. The following functions are listed under Multimedia Joystick Functions
Author: Robert Clemenzi -
URL: http:// / user / clemenzi / technical / Languages / Joysticks.htm