1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150:
| unit Main;
interface
uses DirectInput, Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, ExtCtrls;
type TForm1 = class(TForm) GroupBox2: TGroupBox; btnTestJoystick: TButton; btnAbortJoystick: TButton; procedure FormShow(Sender: TObject); procedure btnTestJoystickClick(Sender: TObject); procedure btnAbortJoystickClick(Sender: TObject); procedure FormDestroy(Sender: TObject); private public procedure InitDI; end;
var Form1: TForm1;
DInput: IDIRECTINPUT; Temp: IDIRECTINPUTDEVICE; DIDJoystick: IDIRECTINPUTDEVICE2;
Aborted: Boolean;
implementation
{$R *.DFM}
procedure TForm1.InitDI; var i: Integer; begin i:=DirectInputCreate(HInstance, DIRECTINPUT_VERSION, DInput, nil); if i<>0 then ShowMessage('1: '+DIErrorString(i));
i:=DInput.CreateDevice(GUID_Joystick, Temp, nil); if i<>0 then ShowMessage('5: '+DIErrorString(i)); i:=Temp.QueryInterface(IID_IDirectInputDevice2, DIDJoystick); if i<>0 then ShowMessage('6: '+DIErrorString(i));
i:=DIDJoystick.SetDataFormat(c_dfDIJoystick); if i<>0 then ShowMessage('7: '+DIErrorString(i)); i:=DIDJoystick.SetCooperativeLevel(Handle, DISCL_FOREGROUND or DISCL_NONEXCLUSIVE); if i<>0 then ShowMessage('8: '+DIErrorString(i)); end;
procedure TForm1.FormShow(Sender: TObject); begin InitDI; end;
procedure TForm1.btnTestJoystickClick(Sender: TObject); var diJoyState: TDIJoyState; begin DIDJoystick.Acquire; btnTestJoystick.Enabled:=False; btnAbortJoystick.Enabled:=True; Aborted:=False; Caption:='Joystick / Gamepad testen ...'; repeat Application.ProcessMessages; DIDJoystick.Poll; DIDJoystick.GetDeviceState(SizeOf(diJoyState), @diJoyState);
if diJoyState.rgbButtons[0]>0 then Caption:='Button 1 gedrückt' else if diJoyState.rgbButtons[1]>0 then Caption:='Button 2 gedrückt' else if diJoyState.rgbButtons[2]>0 then Caption:='Button 3 gedrückt' else if diJoyState.rgbButtons[3]>0 then Caption:='Button 4 gedrückt' else if diJoyState.rgbButtons[4]>0 then Caption:='Button 5 gedrückt' else if diJoyState.rgbButtons[5]>0 then Caption:='Button 6 gedrückt' else if diJoyState.rgbButtons[6]>0 then Caption:='Button 7 gedrückt' else if diJoyState.rgbButtons[7]>0 then Caption:='Button 8 gedrückt' else if diJoyState.rgbButtons[8]>0 then Caption:='Button 9 gedrückt' else if diJoyState.rgbButtons[9]>0 then Caption:='Button 10 gedrückt' else if diJoyState.rgbButtons[10]>0 then Caption:='Button 11 gedrückt' else if diJoyState.rgbButtons[11]>0 then Caption:='Button 12 gedrückt' else if diJoyState.rgbButtons[12]>0 then Caption:='Button 13 gedrückt' else Caption:='Kein Button gedrückt';
until (GetKeyState(VK_ESCAPE) and 128>0) or Aborted; Caption:='Joystick / Gampad getestet'; btnTestJoystick.Enabled:=True; btnAbortJoystick.Enabled:=False; end;
procedure TForm1.btnAbortJoystickClick(Sender: TObject); begin Aborted:=True; end;
procedure TForm1.FormDestroy(Sender: TObject); begin if Assigned(Temp) then Temp:=nil; if Assigned(DIDJoystick) then DIDJoystick:=nil; if Assigned(DInput) then DInput:=nil; end;
end. |