?
Solved

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

Posted on 2004-10-06
4
Medium Priority
?
1,964 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 189 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 186 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

762 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