5,928
社区成员




uses
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 : array[0..255]of byte;
MouseT : TDIMouseState;
JoyT : TDIJoyState2;
DX,DY:integer;
public
constructor create(viewhand:HWND);
destructor Destroy; override;
function keydown(key:byte):bool;
function keydown2():integer;
function moudown(key:byte):bool;
function joydown(key:byte):bool;
function moux:integer;
function mouy:integer;
function mouz:integer;
function joyx:integer;
function joyy:integer;
function keyx:integer;
function keyy:integer;
procedure joyfree(ko:integer=0);
procedure getall();
procedure getkey();
procedure getmouse();
procedure getjoy();
end;
implementation
constructor TDxInput.create(viewhand: HWND);
var p:DWORD;
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,mouse,nil);
mouse.SetDataFormat(c_dfdimouse);
mouse.SetCooperativeLevel(viewhand,p);
dinput.CreateDevice(GUID_Joystick,joy,nil);
if joy<>nil then
begin
joy.SetCooperativeLevel(viewhand,p);
joy.SetDataFormat(c_dfDIJoystick2);
end;
DX:=0;
DY:=0;
end;
destructor TDxInput.Destroy;
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;
begin
getkey;
getmouse();
getjoy();
end;
procedure TDxInput.getjoy;
begin
getmouse;
if joy<>nil then
begin
joy.Acquire;
joy.GetDeviceState(sizeof(TDIJoyState2),@JoyT);
end;
end;
procedure TDxInput.getkey;
begin
keybord.Acquire;
keybord.GetDeviceState(sizeof(KeyT),@KeyT[0]);
end;
procedure TDxInput.getmouse;
begin
mouse.Acquire;
mouse.GetDeviceState(sizeof(TDIMouseState),@MouseT);
end;
function TDxInput.joydown(key: byte): bool;
begin
result := bool(JoyT.rgbButtons[key] and $80);
if joy=nil then result:=false;
end;
procedure TDxInput.joyfree(ko: integer);
var k,i:integer;
begin
k:=ko;
while k=ko do
begin
getjoy();
k:=1;
for i:=0 to 31 do
begin
if joydown(i)=true then k:=0;
end;
end;
if ko=1 then
begin
k:=0;
while k=0 do
begin
getjoy();
k:=1;
for i:=0 to 31 do
begin
if joydown(i) then k:=0;
end;
end;
end;
end;
function TDxInput.joyx: integer;
begin
result := trunc((JoyT.lX-32767)/32767);
if joy <> nil then
inc(DX,result*4)
else
result:=0;
inc(DX,moux);
end;
function TDxInput.joyy: integer;
begin
result := trunc((JoyT.lY-32767)/32767);
if joy <> nil then
inc(DY,-result*4)
else
result:=0;
inc(DY,-mouy);
end;
function TDxInput.keydown(key: byte): bool;
begin
result := bool(KeyT[key] and $80);
end;
function TDxInput.keydown2: integer;
var i:integer;
begin
for i:=0 to 255 do
begin
if bool(KeyT[i] and $80) then
begin
result := i;
exit
end;
end;
result := -1;
end;
function TDxInput.keyx: integer;
begin
result := 0;
if keydown(DIK_LEFT ) then
result:=-1;
if keydown(DIK_RIGHT) then
result:=1;
end;
function TDxInput.keyy: integer;
begin
result := 0;
if keydown(DIK_UP ) then
result := -1;
if keydown(DIK_DOWN) then
result:=1;
end;
function TDxInput.moudown(key: byte): bool;
begin
result := bool(MouseT.rgbButtons[key] and $80);
end;
function TDxInput.moux: integer;
begin
result := MouseT.lX;
end;
function TDxInput.mouy: integer;
begin
result := MouseT.ly;
end;
function TDxInput.mouz: integer;
begin
result := MouseT.lz;
end;
//使用方法:
FDxInput := TDxInput.create(Handle);
在定时器,或消息循环体中判断
FDxInput.getkey;
if DxInput.keydown(DIK_L) then