Obsuga Joysticka w delphi

0

Czy ktos z szanownych forumowiczow ma gotowy patent na obsluge joy'a w delphi ? Procedurke lub chociaz link do stosownego info / faq. Z gory THX.

StreaK

0

poprzez directx za pomocą delphix

0

Po co aż DX? Poniżej masz wyciąg z kodu mojego starego (sprzed 5 lat) programu - jest w nim lista komponentów więc złożyć go sobie też możesz:

unit Joy;

interface

uses
  Windows, StdCtrls, Buttons, Controls, ExtCtrls, Classes, Forms, Messages,
  MMSystem, SysUtils, XPMan;

type
  TMainForm = class(TForm)
    Panel: TPanel;
    ComboBox: TListBox;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    Label6: TLabel;
    Label7: TLabel;
    Label8: TLabel;
    GroupBox1: TGroupBox;
    PovS: TRadioButton;
    PovWS: TRadioButton;
    PovW: TRadioButton;
    PovWN: TRadioButton;
    PovN: TRadioButton;
    PovEN: TRadioButton;
    PovE: TRadioButton;
    PovES: TRadioButton;
    PovCenter: TRadioButton;
    Xpos: TEdit;
    Ypos: TEdit;
    Zpos: TEdit;
    Rpos: TEdit;
    RadioButton1: TCheckBox;
    RadioButton2: TCheckBox;
    RadioButton3: TCheckBox;
    RadioButton4: TCheckBox;
    RadioButton5: TCheckBox;
    RadioButton6: TCheckBox;
    RadioButton7: TCheckBox;
    XPManifest1: TXPManifest;
    procedure FormCreate(Sender: TObject);
    procedure ComboBoxChange(Sender: TObject);
    procedure Joy1Move(var msg:TMessage); message MM_JOY1MOVE;
    procedure SpeedButton1Click(Sender: TObject);

  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  MainForm: TMainForm;
  JoyCnt:Byte;          // Number of available joysticks in the system
  JoyAct:Boolean;       // Is joystick plugged?
  JoyCps:JoyCaps;       // Extended joystick type informations
  Joy_Pos:JoyInfoEx;    // Extended joystick position informations

implementation

{$R *.DFM}
{$R windowsxp.RES}

// Procedure reacting on MM_JOY1MOVE message
// Message parameter contains info, but for details call JoyGetPosEx
procedure TMainForm.Joy1Move(var msg:TMessage);
begin
joyGetPosEX(JOYSTICKID1,@Joy_Pos);
RadioButton1.Checked:=(Joy_Pos.wButtons and 1)=1;
RadioButton2.Checked:=(Joy_Pos.wButtons and 2)=2;
RadioButton3.Checked:=(Joy_Pos.wButtons and 4)=4;
RadioButton4.Checked:=(Joy_Pos.wButtons and 8)=8;
RadioButton5.Checked:=(Joy_Pos.wButtons and 16)=16;
RadioButton6.Checked:=(Joy_Pos.wButtons and 32)=32;
RadioButton7.Checked:=(Joy_Pos.wButtons and 64)=64;
Xpos.Text:=inttostr(Joy_Pos.wXpos);
Ypos.Text:=inttostr(Joy_Pos.wYpos);
Zpos.Text:=inttostr(Joy_Pos.wZpos);
Rpos.Text:=inttostr(Joy_Pos.dwRpos);
case Joy_Pos.dwPOV of
    0:PovN.Checked:=true;
    4500:PovEN.Checked:=true;
    9000:PovE.Checked:=true;
    13500:PovES.Checked:=true;
    18000:PovS.Checked:=true;
    22500:PovWS.Checked:=true;
    27000:PovW.Checked:=true;
    31500:PovWN.Checked:=true;
    else PovCenter.Checked:=true;
    end;
end;

// Detection of available joysticks
procedure TMainForm.FormCreate(Sender: TObject);
begin
Panel.ParentBackground:=FALSE;
Joy_Pos.dwSize:=sizeof(Joy_Pos);
Joy_Pos.dwFlags:=JOY_RETURNAll;
JoyCnt:=JoyGetNumDevs();
JoyAct:=(joyGetPosEX(JOYSTICKID1,@Joy_Pos)<>JOYERR_UNPLUGGED);
if JoyAct then
   begin
   JoyGetDevCaps(JOYSTICKID1,@JoyCps,sizeof(joycaps));
   ComboBox.Items.Insert(0,JoyCps.szPname);
   end;
ComboBox.Enabled:=JoyAct and not (JoyCnt=0);
end;

// Linking joystick messages with parent window
procedure TMainForm.ComboBoxChange(Sender: TObject);
begin
JoySetCapture(MainForm.Handle,ComboBox.ItemIndex,50,false);
ComboBox.Enabled:=false;
end;

end.

1 użytkowników online, w tym zalogowanych: 0, gości: 1