Solved

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

Posted on 2004-10-06
4
1,915 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
  • 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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

776 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