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
| unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, CheckLst, JwaWinUser, Registry, StrUtils;
type
TForm1 = class(TForm)
CheckListBox1: TCheckListBox;
Memo1: TMemo;
Edit1: TEdit;
procedure FormCreate(Sender: TObject);
private
function GetDeviceDesc(aName :string) :string;
procedure WMInput(var Message :TMessage); message WM_INPUT;
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
//Description du Device (Base des registres)
function TForm1.GetDeviceDesc(aName :string) :string;
var
Reg :TRegistry;
begin
//Supprime \??\ et le GUID de fin
aName := Copy(aName, 5, LastDelimiter('#', aName) -5);
//Remplace # par \
aName := StringReplace(aName, '#', '\', [rfReplaceAll]);
Reg := TRegistry.Create;
try
Reg.RootKey := HKEY_LOCAL_MACHINE;
if Reg.OpenKeyReadOnly('SYSTEM\CurrentControlSet\Enum\' +aName)
then Result := Reg.ReadString('DeviceDesc')
else Result := '<Unknown>';
finally
Reg.Free;
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
var
RawsList :array of TRawInputDeviceList;
RIDList :array[0..0] of TRawInputDevice;
RIDInfo :TRidDeviceInfo;
DeviceName :string;
Count :cardinal;
Size :dword;
i :integer;
begin
//Détermine le nombre de device
GetRawInputDeviceList(nil, Count, SizeOf(TRawInputDeviceList));
if Count > 0 then
begin
//Récupère la liste des devices
SetLength(RawsList, Count);
GetRawInputDeviceList(@RawsList[0], Count, SizeOf(TRawInputDeviceList));
for i := 0 to Count -1 do
begin
//Récupère le type du device
Size := SizeOf(TRidDeviceInfo);
RIDInfo.cbSize := Size;
GetRawInputDeviceInfo(RawsList[i].hDevice, RIDI_DEVICEINFO, @RIDInfo, Size);
//Ne concerve que les claviers
case RIDInfo.dwType of
RIM_TYPEKEYBOARD : begin
//Nom interne du device
GetRawInputDeviceInfo(RawsList[i].hDevice, RIDI_DEVICENAME, nil, Size);
SetLength(DeviceName, Size);
GetRawInputDeviceInfo(RawsList[i].hDevice, RIDI_DEVICENAME, @DeviceName[1], Size);
//Ne prend pas en compte les infos pour Terminal Server
if StartsStr('\??\Root', DeviceName) then Continue;
//Ajout à la liste (Object est rempli avec hDevice pour contrôle dans WM_INPUT)
CheckListBox1.AddItem(GetDeviceDesc(DeviceName), pointer(RawsList[i].hDevice));
end;
end;
end;
//Tous actif par défaut
CheckListBox1.CheckAll(cbChecked);
//S'enregistre pour la réception des messages clavier
RIDList[0].usUsagePage := $01;
RIDList[0].usUsage := $06;
RIDList[0].dwFlags := RIDEV_NOLEGACY; //Ne génère pas WM_KEYDOWN, WM_KEYUP !!!
RIDList[0].hwndTarget := Handle;
if not RegisterRawInputDevices(@RIDList[0], Length(RIDList), SizeOf(TRawInputDevice)) then
RaiseLastOSError;
end;
end;
procedure TForm1.WMInput(var Message :TMessage);
var
Data :PRawInput;
Size :dword;
i :integer;
begin
Message.Result := 1;
if Assigned(ActiveControl) then
begin
GetRawInputData(Message.LParam, RID_INPUT, nil, Size, SizeOf(TRawInputHeader));
GetMem(Data, Size);
if GetRawInputData(Message.LParam, RID_INPUT, Data, Size, SizeOf(TRawInputHeader)) = Size then
begin
//Est-ce un clavier ?.. (ça devrait, mais pour être sûr !)
if Data.header.dwType = RIM_TYPEKEYBOARD then
begin
//...oui. Ce handle est dans notre liste ?..
i := CheckListBox1.Items.IndexOfObject(pointer(Data.header.hDevice));
//...oui. Est-il coché ?..
if (i > -1) and CheckListBox1.Checked[i] then
begin
//...oui => Traitement du WM_KEYDOWN
if Data.keyboard.Message = WM_KEYDOWN then
ActiveControl.Perform(WM_CHAR, Data.keyboard.VKey, 0);
Message.Result := 0;
end;
end;
end;
end;
end;
end. |
Partager