Solved

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

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

760 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now