{+----------------------------------------------------------------------------+ / Created: 3.1.2002 / Author: Antti Krats / Company: Gestapo / Copyright ©Gestapo-project 2002 / Description: Component is used to handle joystickport. Component causes event when x- or y-axel value changes over the ChangeLimit. Changelimit is the percent how much axelvalue must be changed. Before staring component, JoystickID must be set by SetJoystickID(two options JOYSTICKID1 and JOYSTICKID2). JoystickInterval tells how often axel-state is checked. / Version: 1.0 / Open Issues: +----------------------------------------------------------------------------+} unit Joystick; interface uses Windows, Messages, SysUtils, Classes,mmsystem,ExtCtrls; type TJoystick = class(TComponent) private TimerSnoop : TTimer; ID : cardinal; //JoystickID XAxelPos : integer; YAxelPos : integer; OldXAxelPos : integer; OldYAxelPos : integer; FOnXAxelChange : TNotifyEvent; FOnYAxelChange : TNotifyEvent; FChangeLimit : real; FJoystickInterval : integer; procedure SetChangeLimit(value : real); procedure SetJoystickInterval(value : integer); protected { Protected declarations } public constructor Create(AOwner : TComponent); override; function GetJoystickPos() : JOYINFO; procedure SetJoystickID(JoyID : cardinal); procedure StartSnooping(); procedure StopSnooping(); procedure Snoop(Sender : TObject); published property OnXAxelChange : TNotifyEvent read FOnXAxelChange write FOnXAxelChange; property OnYAxelChange : TNotifyEvent read FOnYAxelChange write FOnYAxelChange; property ChangeLimit : real read FChangeLimit write SetChangeLimit; property JoystickInterval : integer read FJoystickInterval write SetJoystickInterval; end; procedure Register; implementation procedure Register; begin RegisterComponents('Gestapo', [TJoystick]); end; { TJoystick } constructor TJoystick.Create(AOwner: TComponent); begin inherited; OnXAxelChange := nil; OnYAxelChange := nil; TimerSnoop := TTimer.Create(self); TimerSnoop.Enabled := false; JoystickInterval := 100; TimerSnoop.OnTimer := Snoop; Changelimit := 0.05; end; //Returns JOYINFO which contains x- and y-axel position. function TJoystick.GetJoystickPos: JOYINFO; var JoyInf : JOYINFO; begin joyGetPos(ID,@JoyInf); result := JoyInf; end; //Sets the changelimit(percent). procedure TJoystick.SetChangeLimit(value: real); begin if (value >= 0) or (value <= 1) then FChangeLimit := value; end; //Sets the joystickid. procedure TJoystick.SetJoystickID(JoyID : cardinal); begin ID := JoyID; end; //Sets joystickinterval, how often axelstate is checked. procedure TJoystick.SetJoystickInterval(value: integer); begin FJoystickInterval := value; TimerSnoop.Interval := FJoystickInterval; end; //Checks if axelpositions are changed over changelimit, //then event OnXAxelChange or OnYAxelChange is caused. procedure TJoystick.Snoop(Sender : TObject); var JoyInf : JOYINFO; begin joyGetPos(ID,@JoyInf); XAxelPos := JoyInf.wXpos; YAxelPos := JoyInf.wYpos; if Abs(XAxelPos - OldXAxelPos) > XAxelPos*Changelimit Then begin if Assigned(OnXAxelChange) Then OnXAxelChange(self); OldXAxelPos := XAxelPos; end; if Abs(YAxelPos - OldYAxelPos) > YAxelPos*Changelimit Then begin if Assigned(OnYAxelChange) Then OnYAxelChange(self); OldYAxelPos := YAxelPos; end; end; //Starts timer to check axelpos change. procedure TJoystick.StartSnooping; begin TimerSnoop.Enabled := true; OldXAxelPos := XAxelPos; OldYAxelPos := YAxelPos; end; //Stops timer. procedure TJoystick.StopSnooping; begin TimerSnoop.Enabled := false; end; end.