HOME
- - - - - Delphi Tutorials TOC - - - - - -
Other material for programmers
Delphi: Reading joysticks (And making a stopwatch to illustrate the concepts)
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 needs editing! Sorry! But I thought you'd rather have the information in it, however badly presented, than wait (probably forever) for the cosmetic surgery it needs? What happened is this: I wrote a first attempt at explaining the material in this. I then forgot I'd done that, and wrote a second attempt! You can read the first attempt just by reading the rest of this page. The second attempt is better, and covers everything that appears below and more... but you have to download it... but that means you get the sourcecode without having to type!
The downloadable version is not presented in the unusual format of these tutorials. The main text of the tutorial is included, as a text file, in the zip archive which also contains the application's source code. Click here to download that archive. Save the zip on your disc. You can go offline at that point. Double click on the zip. It was created with XP's "Send to zip", so you just drag the files you want into the folder of your choice.
Confused yet? No? Well, let me mention that there is yet another joysticks tutorial available for you here. It shows you a better way to read them, at least for some purposes, but it does involve more advanced concepts. I.e. responding to Windows messages.
Here begins the first version of the tutorial....
This tutorial is based on a newsgroups post from Mark Wilkinson... thank you, Mark! This tutorial will be short on explanation and not much longer on program... because Delphi makes things so easy!!
I have a switch that goes off-on-off-on... in response to rainfall. (It can come to rest in either state.) The more rain, the more cycles. I wanted to monitor that switch, and the "fire button" input of a joystick seemed the obvious, easy way. Don't worry, I have another computer for important things like Interactive Magic's Apache and Novalogic's F-16, so losing the joystick port was acceptable.
Though I couldn't find it in the Delphi 2 help file, Delphi 2 has built in joystick support.
Thanks to a newsgroups post from Mark Wilkinson (homepage: Mark Wilkinson), I discovered enough to write my program for my rainfall monitor.
The essential bits of reading the joystick port are all in DD18. A zip archive of it's source files source can be downloaded by clicking here. The finished .exe file is included, so that you can see if your system, in particular your joystick, is compatible with the software. It has worked under Windows XP with a Logitech USB joystick. I've also had reports that it also works with a BlackWidow USB joystick. It should work with any "old fashioned", "standard" (i.e. 15 way D plug) joystick, but they are, of course, more rare now.
It puts up a window showing you the current x and y positions of the joystick, and responds to Fire buttons 1 and 2.
A few things you shouldn't overlook:
Add MMSYSTEM to the "USES" clause. The unit is a standard, Borland unit, don't worry. But be sure to add the reference.
A few things you don't need to worry about:
The variable MyJoy is declared to be of type TJoyInfo. This class is set up by Borland; you don't need to worry about the internal details.
The call of JoyGetPos passes a parameter by means of a pointer... I think!! the "@" sign isn't a mistake, it is to do with pointers, I should probably go learn something about why Borland did JoyGetPos the way they did... but in the meantime, I'm just going to use it and be happy.
The information that follows about procedure ReadJoystick is NOT needed if you want to look at the DD18 version of "how to read a joystick". It may be relevant to the TM17 version.
The following procedure declaration header involves "var", which I don't often use, so I'll say a bit about it here if you haven't come across it. Note it appears before li1 AND before bo1. (If it isn't repeated, the boolean parameters bo1 and bo2 will not behave the way li1 and li2 behave.)
procedure ReadJoystick(var li1,li2:longint;var bo1,bo2:boolean);
The following is messy, and I wouldn't do things like this... but it illustrates what var is doing...
Imagine I have a global variable called Answer, and a procedure as follows:
procedure DoubleAndAdd(b1,b2:byte);
begin
b1:=b1+b1;
b2:=b2+b2;
Answer:=b1+b2;
end;
If, in my program, I said
DoubleAndAdd(3,4);
...then Answer would be filled with 14. Okay so far... simple stuff, so far.
Now suppose....
b1:=3;
b2:=4;
DoubleAndAdd(b1,b2);
.... Same result, I hope you agree. Answer is still filled with 14. Also, I were to check what was in b1 and b2 after the above, they would still hold 3 and 4. The b1 and b2 of the main program are separate from the b1 and b2 of the procedure. The fact that they have the same names is mere coincidence. This is all about scope... an important concept, one of the things Pascal Got Right, thus making it easy to write robust programs.
Now we start the tricky stuff....
If the procedure said:
procedure DoubleAndAdd(var b1,b2:byte);
begin
b1:=b1+b1;
b2:=b2+b2;
Answer:=b1+b2;
end;
(same as before, except for the "var")
and the main part of the program said the same as before....
b1:=3;
b2:=4;
DoubleAndAdd(b1,b2);
.... Answer would still be filled with 14... BUT!....now if I were to check what was in b1 and b2 after the above, they would hold 6 and 8. Even more "magic": if I did.....
FirstVariable:=3;
SecondVariable:=4;
DoubleAndAdd(FirstVariable,SecondVariable);
... Answer would hold 14, FirstVariable would hold 6, and SecondVariable would hold 8. 3 went into FirstVariable, in that it went into the procedure. In the procedure, it was moved to b1. As the procedure was finishing up, it passed back to FirstVariable whatever was in b1 at the end of executing the procedure.
There are other solutions. (user defined types.) Using var in the procedure declaration might be seen as a clumsy solution... but it works! Be sure to put var in front of each group of variables you want to pass that way.
Enjoy!
That's the simple approach to joysticks.... remember there are other tutorials on the topic in these pages, especially the downloadable zip archive with the version of this material that includes a timer.
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
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 .....