[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

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

Posted on 2004-10-06
4
Medium Priority
?
1,993 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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

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…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Suggested Courses

650 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