CodePudding user response:
USES the
Windows, Messages, SysUtils, Variants, Classes, Forms, Graphics, Controls, Direct3D,
Dialogs, Direct3D9 StdCtrls, ActiveX, D3DX9 comobj, DirectInput,
ExtCtrls;
Type
TDxInput=class
Private
DInput: IDirectInput8;
Keybord: IDIRECTINPUTDEVICE8;
Mouse: IDIRECTINPUTDEVICE8;
Joy: IDIRECTINPUTDEVICE8;
KeyT: an array of byte [0.. 255];
MouseT: TDIMouseState;
JoyT: TDIJoyState2;
DX, DY: integer;
Public
The constructor create (viewhand: HWND);
The destructor Destroy; Override.
The function keydown (key: byte) : bool;
The function keydown2 () : integer;
The function moudown (key: byte) : bool;
The function joydown (key: byte) : bool;
The function moux: integer;
The function mouy: integer;
The function mouz: integer;
The function joyx: integer;
The function joyy: integer;
The function keyx: integer;
The function keyy: integer;
Procedure joyfree (ko: integer=0);
Procedure getall ();
Procedure getkey ();
Procedure getmouse ();
Procedure getjoy ();
end;
Implementation
The constructor TDxInput. Create (viewhand: HWND);
Var p: DWORD;
The begin
P:=DISCL_FOREGROUND or DISCL_NONEXCLUSIVE;
DirectInput8Create (GetModuleHandle (nil), DIRECTINPUT_VERSION IID_IDirectInput8, dinput, nil);
Dinput. CreateDevice (GUID_SysKeyboard keybord, nil);
Keybord. SetDataFormat (c_dfDIKeyboard);
Keybord. SetCooperativeLevel (viewhand, p);
Dinput. CreateDevice (GUID_Sysmouse, the mouse, nil);
Mouse. SetDataFormat (c_dfdimouse);
Mouse. SetCooperativeLevel (viewhand, p);
Dinput. CreateDevice (GUID_Joystick, joy, nil);
If joy<> Nil then
The begin
Joy. SetCooperativeLevel (viewhand, p);
Joy. SetDataFormat (c_dfDIJoystick2);
end;
DX:=0;
DY:=0;
end;
Destructor TDxInput. Destroy;
The begin
If DInput<> Nil then DInput:=nil;
If keybord<> Nil then keybord:=nil;
If mouse<> Nil then mouse:=nil;
If joy<> Nil then joy:=nil;
Inherited Destroy;
end;
Procedure TDxInput. Getall;
The begin
Getkey;
Getmouse ();
Getjoy ();
end;
Procedure TDxInput. Getjoy;
The begin
Getmouse;
If joy<> Nil then
The begin
Joy. Acquire;
Joy. GetDeviceState (sizeof (TDIJoyState2), @ JoyT);
end;
end;
Procedure TDxInput. Getkey;
The begin
Keybord. Acquire;
Keybord. GetDeviceState (sizeof (KeyT), @ KeyT [0]).
end;
Procedure TDxInput. Getmouse;
The begin
Mouse. Acquire;
Mouse. GetDeviceState (sizeof (TDIMouseState), @ MouseT);
end;
Function TDxInput. Joydown (key: byte) : bool.
The begin
Result:=bool (JoyT rgbButtons [key] and $80);
If joy=nil then result:=false;
end;
Procedure TDxInput. Joyfree (ko: integer);
Var k, I: integer;
The begin
K:=ko;
While k=ko do
The begin
Getjoy ();
K:=1;
For I:=0 to 31 do
The begin
If joydown (I)=true then: k=0;
end;
end;
If ko=1 then
The begin
K:=0;
While k=0 do
The begin
Getjoy ();
K:=1;
For I:=0 to 31 do
The begin
If joydown: (I) then k=0;
end;
end;
end;
end;
The function TDxInput. Joyx: integer;
The begin
Result:=trunc ((JoyT. LX - 32767)/32767);
If joy & lt;> Nil then
Inc (DX, result * 4)
The else
Result:=0;
Inc (DX, moux);
end;
The function TDxInput. Joyy: integer;
The begin
Result:=trunc ((JoyT lY - 32767)/32767);
If joy & lt;> Nil then
Inc (DY - result * 4)
The else
Result:=0;
Inc (DY - mouy);
end;
Function TDxInput. Keydown (key: byte) : bool.
The begin
Result:=bool (KeyT [key] and $80);
end;
The function TDxInput. Keydown2: integer;
Var I: integer;
The begin
For I:=0 to 255 do
The begin
If a bool (KeyT [I] and $80) then
The begin
Result:=I;
The exit
end;
end;
Result:=1;
end;
The function TDxInput. Keyx: integer;
The begin
Result:=0;
If keydown (DIK_LEFT) then
Result:=1;
If keydown (DIK_RIGHT) then
Result:=1;
end;
The function TDxInput. Keyy: integer;
The begin
Result:=0;
If keydown (DIK_UP) then
Result:=1;
If keydown (DIK_DOWN) then
Result:=1;
end;
Function TDxInput. Moudown (key: byte) : bool.
The begin
Result:=bool (MouseT rgbButtons [key] and $80);
end;
The function TDxInput. Moux: integer;
The begin
Result:=MouseT. LX;
end;
The function TDxInput. Mouy: integer;
The begin
Result:=MouseT. Ly;
end;
The function TDxInput. Mouz: integer;
The begin
Result:=MouseT. Lz;
end;
//method of use:
FDxInput:=TDxInput. Create (Handle);
In the timer, or a message in the loop body judgment
FDxInput. Getkey;
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull