HOME - - - - - Delphi Tutorials TOC - - - - - - Other material for programmers

Delphi: Reading joysticks and using Windows messages

       (And making a stopwatch along the way)

This has good information, and a search button at the bottom of the page

Please don't dismiss it because it isn't full of graphics, scripts, cookies, etc!

Click here if you want to know more about the source and format of these pages.

This tutorial is based on information found at The Unofficial Delphi Developers FAQ... thank you, UDDF! I've worked up his information on how to read a joystick into a program which turns your computer into a stopwatch. Besides the joystick specific information, the program also illustrates how to respond to Windows messages coming from elsewhere in the computer. And finally, the program has, if I do say so myself, quite an elegant interface, slipping neatly between states. The tutorial comes complete with sourcecode.

This was originally written back in the days before USB joysticks, but it is believed that it will work with them. If it doesn't, then the code in my Level 3 tutorial on joysticks does!

This tutorial is a more advanced version of ideas first explored in my other joysticks tutorial. The program developed in that tutorial is similar to the one developed in this tutorial. However, in the simpler program, a timer repeatedly "went and looked at" the joysticks. Easy to understand, but not efficient. In this tutorial, a component of Window's mmSystem unit is configured to send a message to our application when a fire button is pressed, our program then deals with that. We are using event handlers to respond when there is a need. This is accomplished with calls of joyGetDevCaps, joySetCapture, and joyReleaseCapture.

This tutorial is not presented in the unusual format of these tutorials. The main text of the tutorial is included, as a text file, in the self extracting zip (SEZ) archive which also contains the application's source code. Click here to download that archive. Save the SEZ on your disc. You can go offline at that point. Double click on the SEZ. It was created with WinZip, and will allow you to select a destination folder for the material to be unzipped.

Here is some of what I worked from in preparing the tutorials....

Adapted from: http://cpcug.org/user/clemenzi/technical/Languages/Joysticks.htm#Delphi

A) Windows itself, regardless how you write programs for it, provides two ways to read the joystick, an input only device:

winmm.dll - 10 functions including joyGetPos and joyGetDevCaps.

B) Delphi 5 does not supply joystick support via the vcl.

Windows API support is defined in rtl\win\mmsystem.pas (At least some of what follows applies even to Delphi 2, but none of it, I think to Delphi 1)

The following code snippet 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.
  TMMJoyStick  = packed record
    Msg:     Cardinal;  // The message ID
    Buttons: Longint;   // The wParam
    XPos:    word;      // The lParam
    YPos:    word;
    Result:  Longint;

There is one interesting inconsistency:

The x and y positions are passed to a message handler as unsigned words (16 bits), but the associated Max and Min values in TJoyCaps and the x/y values in TJoyInfo are 32 bits (UINT).

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;
end; procedure TForm1.FormCreate(Sender: TObject);
  myJoyCaps: TJoyCaps;
 joyGetDevCaps(joystickid1,@myJoyCaps, sizeof(myJoyCaps)); // for test
 joySetCapture(self.Handle, joystickid1, 100, true);
end; procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);

(Here ends material quoted from other sources)

            powered by FreeFind
  Site search Web search
Site Map    What's New    Search This search merely looks for the words you enter. It won't answer "Where can I download InpOut32?"

Click here if you're feeling kind! (Promotes my site via "Top100Borland")
Ad from page's editor: Yes.. I do enjoy compiling these things for you. I hope they are helpful. However... this doesn't pay my bills!!! Sheepdog Software (tm) is supposed to help do that, so if you found this stuff useful, (and you run a Windows or MS-DOS PC) please visit my freeware and shareware page, download something, and circulate it for me? Links on your page to this page would also be appreciated!
Click here to visit editor's freeware, shareware page.

Link to Tutorials main page
How to email or write this page's editor, Tom Boyd

Valid HTML 4.01 Transitional Page WILL BE tested for compliance with INDUSTRY (not MS-only) standards, using the free, publicly accessible validator at validator.w3.org

If this page causes a script to run, why? Because of things like Google panels, and the code for the search button. Why do I mention scripts? Be sure you know all you need to about spyware.

....... P a g e . . . E n d s .....