Solved

What's the best way to read USB Joystick buttons?

Posted on 2004-10-06
4
1,941 Views
Last Modified: 2012-06-22
I used to read the joystick buttons the old way with the joystick port, but now everything is going to USB and I am not as familiar with that area yet.

Can someone give me some ideas on how to read what button was pushed?

Thanks.
0
Comment
Question by:perkley
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 17

Accepted Solution

by:
Wim ten Brink earned 63 total points
ID: 12244219
I think you should just use the DirectX-related device input methods. That way, you let Windows handle the communications with the joystick and you communicate through DirectX with whatever is connected. I do have an USB joystick but it's just configured through Windows and games just detect it through DirectX or whatever.

Apparantly in Mmsystem.h (and the related Delphi variant of this header file) there are several methods available to capture the joystick events, including joyConfigChanged, joyGetDevCaps, joyGetNumDevs, joyGetPos, joyGetPosEx, joyGetThreshold, joyReleaseCapture, joySetCapture, joySetThreshold.

You might check out the MSDN site for more information about these functions, though.
0
 
LVL 11

Assisted Solution

by:robert_marquardt
robert_marquardt earned 62 total points
ID: 12245768
Workshop_Alex got it right.
The only other way is using my free HID component and directly read the HID device.
http://www.soft-gems.net/Controls.php
0
 

Author Comment

by:perkley
ID: 12246253
This is the code I ended up with, for those that might be interested.  It is only the joystick information, I can't reveal my game, hehe.  If you see anything that can be simplified let me know, but it seems to be working with a USB joystick.  I only wanted the 4 main buttons.

unit uMain;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, math, StdCtrls, ExtCtrls, mmsystem;

type
  TGameState = (gsNull, gsPlaying, gsPaused, gsWrong, gsCorrect, gsWin, gsGameOver, gsTimeUp, gsPlayerOver, gsSetup, gsAbout, gsQuestions);

  TForm1 = class(TForm)
    Timer1: TTimer;
    Label1: TLabel;
    procedure FormCreate(Sender: TObject);
    procedure Timer1Timer(Sender: TObject);
  private
    JoyState: array[0..3] of boolean;
    JoyCaps: TJoyCaps;
    CanPressJoy: Boolean;
    procedure GetJoyInfoPoll;
    procedure GameKeyPressed(Key: Char);
    { Private declarations }
  public
    GameState: TGameState;
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
var
  lpjoyInfoEx: TJOYINFOEX;    // holds extended joystick information
  lpjoyinfo: tjoyinfo;
begin
//Setup Joystick stuff
  { - if there are no joystick devices present, indicate an error - }
  if  joyGetNumDevs = 0  then
    Beep;
    //ShowMessage('Controller driver not present');

  { - determine if there is a joystick present - }
  if joyGetPosEx(JOYSTICKID1, @lpjoyInfoEx) = MMSYSERR_BADDEVICEID then
    Beep;
    //ShowMessage('Controller 1 is not present');

    {this one isn't working right}
  { - determine if the joystick is unplugged - }
  if joyGetPos(JOYSTICKID1, @lpjoyInfo) = JOYERR_UNPLUGGED then
    Beep;
    //ShowMessage('Controller is unplugged');

  { - There seems to be a joystick device driver, and a joystick is plugged -
    - in to the system, so retrieve its capabilities.                       - }
  if joyGetDevCaps(JOYSTICKID1, @JoyCaps, SizeOf(JoyCaps))<>JOYERR_NOERROR then
    Beep;
    //ShowMessage('Controller capabilities could not be retrieved');

  Timer1.Interval := JoyCaps.wPeriodMin;
  Timer1.Enabled := TRUE;
end;

procedure TForm1.Timer1Timer(Sender: TObject);
begin
  GetJoyInfoPoll;
end;

procedure TForm1.GetJoyInfoPoll;
var
   // holds the joystick position coordinates
   JoyPos: TJoyInfoEx;
   vKey: Word;
begin
  //Just for testing remove in real game
  GameState := gsPlaying;
  //Just for testing, remove in real game
  CanPressJoy := True;

  {poll the joystick for its status, retrieving information for up to 32 buttons
   and 6 axis positions}
  JoyPos.dwSize := SizeOf(TJoyInfoEx);
  JoyPos.dwFlags := JOY_RETURNALL;
  joyGetPosEx(JOYSTICKID1, @JoyPos);

  {test the joystick buttons}
  if Longbool(JoyPos.wButtons and Trunc(Power(2, 0))) = NOT(JoyState[0]) then
  begin
    JoyState[0] := NOT(JoyState[0]);
    if CanPressJoy then
    begin
      CanPressJoy := False;
      if GameState = gsPlaying then
      begin
        GameKeyPressed('A');
      end else
      begin
        vKey := VK_SPACE;
        Form1.OnKeyUp(Form1, vKey, []);
      end;
    end;
  end else
  if Longbool(JoyPos.wButtons and Trunc(Power(2, 1))) = NOT(JoyState[1]) then
  begin
    JoyState[1] := NOT(JoyState[1]);
    if CanPressJoy then
    begin
      CanPressJoy := False;
      if GameState = gsPlaying then
      begin
        GameKeyPressed('B');
      end else
      begin
        vKey := VK_SPACE;
        Form1.OnKeyUp(Form1, vKey, []);
      end;
    end;
  end else
  if Longbool(JoyPos.wButtons and Trunc(Power(2, 3))) = NOT(JoyState[3]) then
  begin
    JoyState[3] := NOT(JoyState[3]);
    if CanPressJoy then
    begin
      CanPressJoy := False;
      if GameState = gsPlaying then
      begin
        GameKeyPressed('C');
      end else
      begin
        vKey := VK_SPACE;
        Form1.OnKeyUp(Form1, vKey, []);
      end;
    end;
  end else
  if Longbool(JoyPos.wButtons and Trunc(Power(2, 2))) = NOT(JoyState[2]) then
  begin
    JoyState[2] := NOT(JoyState[2]);
    if CanPressJoy then
    begin
      CanPressJoy := False;
      if GameState = gsPlaying then
      begin
        GameKeyPressed('D');
      end else
      begin
        vKey := VK_SPACE;
        Form1.OnKeyUp(Form1, vKey, []);
      end;
    end;
  end;

end;

procedure TForm1.GameKeyPressed(Key: Char);
var
  AnswerID: integer;
begin
  if Char(Key) in ['A'..'D'] then
  begin
    ShowMessage(Char(Key));
  end;
end;

end.
0
 

Author Comment

by:perkley
ID: 12251591
robert_marquardt, does your component support Delphi 7?  It doesn't say it does.  I think I will split the points between the both of you if you can just give me more ideas on the following:

As you can see by the code above, I am building a game.  I need 4 buttons or maybe more, and I want to be able to just plug these buttons in using USB.  I don't really want to have a driver that runs them, but it would be neat that if when they were plugged in then it mentioned in Windows XP, Found Perkley's Buttons.  Right now I bought the cheapest joystick I could find and took it apart and hooking up the buttons to the joystick circuit board.  This way Windows already knows how to work with it and I don't have to have my own driver.  But when I plug it in it mentions the brand and model of the joystick, so I am considering building my own circuit board.  But I need some ideas on where to start and whether things already exist that will speed up my process.

Thanks, Perkley
0

Featured Post

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

724 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question