IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Langage Delphi Discussion :

Keyboard hook probleme


Sujet :

Langage Delphi

  1. #1
    Membre régulier
    Profil pro
    Développeur informatique
    Inscrit en
    Septembre 2009
    Messages
    168
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Septembre 2009
    Messages : 168
    Points : 120
    Points
    120
    Par défaut Keyboard hook probleme
    Bonjour,
    je suis en train de développer un petit hook du keyboard mais je suis confronter à un petit problème très gênant..

    Tout dabord je n'utilise pas de dll je hook avec SetWindowsHookEx avec WM_KEYBOARD_LL (13d)

    sa fonctionne très bien je reçois bien une structure de données a chaque touches pressés mais quand j'affiche le résultat dans un memo sa m'affiche pas toujours le bon caractères de temps en temps (toujours quand c'est les caractères spéciaux) il ne les affiche pas..

    j'utilise cette API pour normalement convertir la touche :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
     ToAscii(infos.vkCode, infos.scanCode ,State,@wBuf,infos.flags);
    State = TKeyboardState
    Inos est la structure : PKBDLLHOOKSTRUCT

    Et pourtant sa ne fonctionne pas..
    Une idée ?

    Merci

  2. #2
    Rédacteur/Modérateur
    Avatar de Andnotor
    Inscrit en
    Septembre 2008
    Messages
    5 754
    Détails du profil
    Informations personnelles :
    Localisation : Autre

    Informations forums :
    Inscription : Septembre 2008
    Messages : 5 754
    Points : 13 340
    Points
    13 340
    Par défaut
    Tu devrais essayer avec MapVirtualKey.

  3. #3
    Membre régulier
    Profil pro
    Développeur informatique
    Inscrit en
    Septembre 2009
    Messages
    168
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Septembre 2009
    Messages : 168
    Points : 120
    Points
    120
    Par défaut
    Citation Envoyé par Andnotor Voir le message
    Tu devrais essayer avec MapVirtualKey.
    ToAscii(infos.vkCode, MapVirtualKey(infos.vkCode,0),State,@wBuf,infos.flags);

    même résultat

  4. #4
    Rédacteur/Modérateur
    Avatar de Andnotor
    Inscrit en
    Septembre 2008
    Messages
    5 754
    Détails du profil
    Informations personnelles :
    Localisation : Autre

    Informations forums :
    Inscription : Septembre 2008
    Messages : 5 754
    Points : 13 340
    Points
    13 340
    Par défaut
    Je pensais plutôt à quelque chose comme:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    var
      c :Char;
    begin
      c := Chr(MapVirtualKey(infos.scanCode, 1));  //MAPVK_VSC_TO_VK
    end;
    edit: Non, ça ne semble pas suffisant...

  5. #5
    Membre régulier
    Profil pro
    Développeur informatique
    Inscrit en
    Septembre 2009
    Messages
    168
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Septembre 2009
    Messages : 168
    Points : 120
    Points
    120
    Par défaut
    Citation Envoyé par Andnotor Voir le message
    Je pensais plutôt à quelque chose comme:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    var
      c :Char;
    begin
      c := Chr(MapVirtualKey(infos.scanCode, 1));  //MAPVK_VSC_TO_VK
    end;
    edit: Non, ça ne semble pas suffisant...
    merci encore

    Par contre sa ne fonctionne toujours pas , il ne gère plus l'état des touches , majuscule / minuscule , pavé num etc etc et toujours les symboles, par contre si je fait un
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    PostMessage(Memo.handle,WM_KEYDOWN,infos.vkcode,infos.scanCode);
    il affiche bien les symboles , j'aurais aimer pouvoir faire la même chose que le PostMessage mais pour un char et non passer par un memo.

  6. #6
    Rédacteur/Modérateur
    Avatar de Andnotor
    Inscrit en
    Septembre 2008
    Messages
    5 754
    Détails du profil
    Informations personnelles :
    Localisation : Autre

    Informations forums :
    Inscription : Septembre 2008
    Messages : 5 754
    Points : 13 340
    Points
    13 340
    Par défaut
    As-tu bien rempli State avec GetKeyState avant l'appel à toAscii ?

  7. #7
    Membre régulier
    Profil pro
    Développeur informatique
    Inscrit en
    Septembre 2009
    Messages
    168
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Septembre 2009
    Messages : 168
    Points : 120
    Points
    120
    Par défaut
    Citation Envoyé par Andnotor Voir le message
    As-tu bien rempli State avec GetKeyState avant l'appel à toAscii ?
    Oui

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    GetKeyboardState(state);

  8. #8
    Membre expert
    Avatar de LadyWasky
    Femme Profil pro
    Inscrit en
    Juin 2004
    Messages
    2 932
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Âge : 53
    Localisation : France, Hauts de Seine (Île de France)

    Informations forums :
    Inscription : Juin 2004
    Messages : 2 932
    Points : 3 565
    Points
    3 565
    Par défaut
    Oui, c'est une vrai plaie.
    Je me suis occupée dans le passé d'un projet coriace qui nécessitait l'affichage "texte" de la touche appuyée. ( http://nostromon50onvi.sourceforge.net/)
    Je m'en suis sortie comme ceci à l'époque :
    1) tu télécharges l'unité créée par Troel Jakobsen qui s'appelle "HotKeyManager". Tu la trouveras ici :http://www.subsimple.com/delphi.asp

    2) Dedans, il y a une fonction qui s'appelle HotKeyToText ( function HotKeyToText(aKey: Cardinal): string; )

    Et ça s'utilise comme ceci, d'abord une unité qui permet d'enregistrer des macros :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    324
    325
    326
    327
    328
    329
    330
    331
    332
    333
    334
    335
    336
    337
    338
    339
    340
    341
    342
    343
    344
    345
    346
    347
    348
    349
    350
    351
    352
    353
    354
    355
    356
    357
    358
    359
    360
    361
    362
    363
    364
    365
    366
    367
    368
    369
    370
    371
    372
    373
    374
    375
    376
    377
    378
    379
    380
    381
    382
    383
    384
    385
    386
    387
    388
    389
    390
    391
    392
    393
    394
    395
    396
    397
    398
    399
    400
    401
    402
    403
    404
    405
    406
    407
    408
    409
    410
    411
    412
    413
    414
    415
    416
    417
    418
    419
    420
    421
    422
    423
    424
    425
    426
    427
    428
    429
    430
    431
    432
    433
    434
    435
    436
    437
    438
    439
    440
    441
    442
    443
    444
    445
    446
    447
    448
    449
    450
    451
    452
    453
    454
    455
    456
    457
    458
    459
    460
    461
    462
    463
    464
    465
    466
    467
    468
    469
    470
    471
    472
    473
    474
    475
    476
    477
    478
    479
    480
    481
    482
    483
    484
    485
    486
    487
    488
    489
    490
    491
    492
    493
    494
    495
    496
    497
    498
    499
    500
    501
    502
    503
    504
    505
    506
    507
    508
    509
    510
    511
    512
    513
    514
    515
    516
    517
    518
    519
    520
    521
    522
    523
    524
    525
    526
    527
    528
    529
    530
    531
    532
    533
    534
    535
    536
    537
    538
    539
    540
    541
    542
    543
    544
    545
    546
    547
    548
    549
    550
    551
    552
    553
    554
    555
    556
    557
    558
    559
    560
    {*****************************************************************************
     *
     *  UnitMacrosList.pas - Nostromo N50 Macros Editor (21-November-2008)
     *
     *  Copyright (c) 2008 Wilfrid AVRILLON
     *
     *  Author:     Wilfrid AVRILLON
     *  E-mail:     wilfrid.avrillon@gmail.com
     *  Homepage:   http://sourceforge.net/projects/nostromon50onvi/
     *
     *
     *  This file is part of Nostromo N50 Macros Editor.
     *
     *  Nostromo N50 Macros Editor is free software: you can redistribute it and/or modify
     *  it under the terms of the GNU General Public License as published by
     *  the Free Software Foundation, either version 3 of the License, or
     *  (at your option) any later version.
     *
     *  Nostromo N50 Commander is distributed in the hope that it will be useful,
     *  but WITHOUT ANY WARRANTY; without even the implied warranty of
     *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     *  GNU General Public License for more details.
     *
     *  You should have received a copy of the GNU General Public License
     *  along with Nostromo N50 Macros Editor.
     *  If not, see <http://www.gnu.org/licenses/>.
     *
     *----------------------------------------------------------------------------
     *
     *  Revision history:
     *
     *     DATE     REV                 DESCRIPTION
     *  ----------- --- ----------------------------------------------------------
     *
     *  Nov 21 2008 TF  First Release !
     *                  TMacro : Class definition of a macro-command
     *                           (One Macro=one key or one key combination)
     *                  TMacroList : handles a list of TMacro
     *  Dec 18 2008 1.2 Feature : Mouse support
     *****************************************************************************}
     unit UnitMacrosList;
     
    interface
    uses Sysutils, Classes, Contnrs, StdCtrls;
    Type
      TTouche=record
        down:boolean;
        VK_code:word;
      end;
     
      PTTouche = ^TTouche;
     
      TMacro = class(TObject)
      private
        FList:TList;
        FMacroName: string;
        FMacroID: integer;
        function GetItem(Index: Integer): TTouche;
        procedure SetItem(Index: Integer; const Value: TTouche);
        function GetCount: Integer;
        procedure SetCount(const Value: Integer);
        procedure SetMacroName(const Value: string);
        procedure SetMacroID(const Value: integer);
      protected
        procedure SaveToStream(AStream:TStream);
        function LoadFromStream(AStream:TStream):boolean;
      public
        FirstKeyUp,LastKeyDown,IndexUp:integer;
        constructor Create;
        destructor Destroy; override;
        function Add(Item:TTouche): Integer;
        procedure Delete(Index: Integer);
        procedure Insert(Index: Integer; Item: TTouche);
        procedure Move(CurIndex, NewIndex: Integer);
        procedure Exchange(Index1, Index2: Integer);
        procedure Clear;
        procedure ComputeIndexUp;
        function IndexOf(const Value: TTouche): Integer;
        property MacroName:string read FMacroName write SetMacroName;
        Property MacroID:integer read FMacroID write SetMacroID;
        property Items[Index:Integer]:TTouche read GetItem write SetItem;default;
        property Count: Integer read GetCount write SetCount;
      end;
     
      TMacroList = class
      private
        FList: TObjectList;
        FFichier: string;
        FGameApp: string;
        function GetItem(Index: Integer): TMacro;
        function GetCapacity: Integer;
        function GetCount: Integer;
        procedure SetItem(Index: Integer; const Value: TMacro);
        procedure SetCapacity(const Value: Integer);
        procedure SetCount(const Value: Integer);
        function GetOwnsObjects: boolean;
        procedure SetOwnsObjects(const Value: boolean);
        procedure SetFichier(const Value: string);
        procedure SetGameApp(const Value: string);
      protected
        procedure SaveToStream(AStream:TStream);
        function LoadFromStream(AStream:TStream):boolean;
      public
        constructor Create;
        destructor Destroy; override;
        function Add(Item: TMacro): Integer;
        procedure Clear; virtual;
        procedure Delete(Index: Integer);
        procedure Exchange(Index1, Index2: Integer);
        function Expand: TMacroList;
        function Extract(Item: TMacro): TMacro;
        function First: TMacro;
        function IndexOf(Item: TMacro): Integer;
        function IndexOfID(AMacroID:Integer): Integer;
        procedure Insert(Index: Integer; Item: TMacro);
        function Last: TMacro;
        procedure Move(CurIndex, NewIndex: Integer);
        procedure SortByNames;
        function Remove(Item: TMacro): Integer;
        procedure Pack;
        procedure Sort(Compare: TListSortCompare);
        procedure Assign(ListA: TMacroList; AOperator: TListAssignOp = laCopy; ListB: TMacroList = nil);
        property GameApp:string read FGameApp write SetGameApp;
        property Capacity: Integer read GetCapacity write SetCapacity;
        property Count: Integer read GetCount write SetCount;
        property Items[Index: Integer]: TMacro read GetItem write SetItem; default;
        property OwnsObjects: boolean read GetOwnsObjects write SetOwnsObjects;
        property Fichier:string read FFichier write SetFichier;
        function LoadFromFile:boolean;
        procedure SaveToFile;
      end;
    implementation
    { TMacro }
     
    procedure WriteStreamString(Stream : TStream; UneChaine : string);
    var LongueurChaine : integer;
    begin
      {obtenir la longueur de la chaîne de caractères}
      LongueurChaine := Length(UneChaine);
      {écrire cette longueur dans le flux}
      Stream.Write(LongueurChaine,SizeOf(integer));
      {écrire les caractères (tous d'un coup !)}
      Stream.Write(UneChaine[1], LongueurChaine);
    end;
     
    {retourne une chaîne de caractères depuis le flux}
    function ReadStreamString(Stream : TStream) : string;
    var LongueurChaine : integer;
    begin
      {obtenir la longueur de la chaîne de caractères}
      Stream.Read(LongueurChaine,SizeOf(integer));
      {Redimensionner la chaine pour allouer la mémoire nécessaire}
      SetLength(Result, LongueurChaine);
      {Lire les caractères (Tous d'un coup)}
      Stream.Read(Result[1], LongueurChaine);
    end;
     
     
    function TMacro.Add(Item:TTouche): Integer;
    var
      p:PTTouche;
    begin
      GetMem(p,SizeOf(Item));
      p^ := Item;
      result := FList.Add(p);
    end;
     
    procedure TMacro.Clear;
    var
      i:integer;
    begin
      for i := 0 to FList.Count-1 do FreeMem(FList[i]);
      FList.Clear;
    end;
     
    procedure TMacro.ComputeIndexUp;
    var i:integer;
    begin
      FirstKeyUp:=Count;
      LastKeyDown:=0;
      for I := 0 to Count - 1 do
      begin
        if Items[i].down then
        begin
          if (i>LastKeyDown) then LastKeyDown:=i;
        end
        else begin
          if (i<FirstKeyUp) then FirstKeyUp:=i;
        end;
      end;
      if (FirstKeyUp<LastKeyDown)
        then IndexUp:=Count
        else IndexUp:=FirstKeyUp;
    end;
     
    constructor TMacro.Create;
    begin
      FList := TList.Create;
    end;
     
    procedure TMacro.Delete(Index: Integer);
    begin
      FreeMem(FList[Index]);
      FList.Delete(Index);
    end;
     
    destructor TMacro.Destroy;
    begin
      Clear;
      FList.Free;
      inherited;
    end;
     
    procedure TMacro.Exchange(Index1, Index2: Integer);
    begin
      FList.Exchange(Index1, Index2);
    end;
     
    function TMacro.GetCount: Integer;
    begin
      result := FList.Count;
    end;
     
    function TMacro.GetItem(Index: Integer): TTouche;
    begin
      result := PTTouche(FList[Index])^;
    end;
     
    function TMacro.IndexOf(const Value: TTouche): Integer;
    var i: Integer;
        found:Boolean;
        AnItem:TTouche;
    begin
      Result := -1;
      found:=False;
      i:=0;
      while ((i<FList.Count) and (not found)) do
      begin
        AnItem:=GetItem(i);
        found:=CompareMem(@Value,@AnItem,SizeOf(TTouche));
        if not found then Inc(i);
      end;
      if found then Result:=i;
    end;
     
    procedure TMacro.Insert(Index: Integer; Item: TTouche);
    var
      p: PTTouche;
    begin
      GetMem(p, SizeOf(Item));
      p^ := Item;
      FList.Insert(Index, p);
    end;
     
    function TMacro.LoadFromStream(AStream: TStream): boolean;
    var NbreTouches,i:integer;
        ATouche:TTouche;
    begin
      try
        result:=true;
        FMacroName:=ReadStreamString(AStream);
        AStream.Read(FMacroID,sizeof(integer));
        AStream.Read(NbreTouches,sizeof(integer));
        for i := 0 to NbreTouches - 1 do
        begin
          AStream.Read(ATouche,sizeof(TTouche));
          add(Atouche);
        end;
      except
        result:=false;
      end;
    end;
     
    procedure TMacro.Move(CurIndex, NewIndex: Integer);
    begin
      FList.Move(CurIndex, NewIndex);
    end;
     
    procedure TMacro.SaveToStream(AStream: TStream);
    var NbreTouches,i:integer;
        ATouche:TTouche;
    begin
      WriteStreamString(AStream,FMacroName);  
      NbreTouches:=Count;
      AStream.Write(FMacroID,SizeOf(integer));
      AStream.Write(NbreTouches,SizeOf(integer));
      for i := 0 to NbreTouches - 1 do
      begin
        Atouche:=Items[i];
        AStream.Write(ATouche,sizeof(TTouche));
      end;
     
    end;
     
    procedure TMacro.SetCount(const Value: Integer);
    var
      i, ListCount: Integer;
      p: PTTouche;
    begin
      if FList.Count = Value then
        Exit
      else
      if Value < FList.Count then
      begin
        ListCount := FList.Count;
        for i := ListCount downto Value+1 do
        begin
          FList.Delete(i);
        end;
      end else
      if FList.Count < Value then
      begin
        for i := FList.Count+1 to Value do
        begin
          GetMem(p, SizeOf(TTouche));
          FillChar(p^, SizeOf(TTouche), 0);
          FList.Add(p);
        end;
      end;
    end;
     
    procedure TMacro.SetItem(Index: Integer; const Value: TTouche);
    begin
      PTTouche(FList[Index])^ := value;
    end;
     
    procedure TMacro.SetMacroID(const Value: integer);
    begin
      FMacroID := Value;
    end;
     
    procedure TMacro.SetMacroName(const Value: string);
    begin
      FMacroName := Value;
    end;
     
    { TMacroList }
    function TMacroList.Add(Item: TMacro): Integer;
    var AnID:integer;
    begin
      AnID:=0;
      while IndexOfID(AnID)<>-1 do inc(AnID);
      Item.MacroID:=AnID;
      Result := FList.Add(Item);
    end;
     
    procedure TMacroList.Assign(ListA: TMacroList; AOperator: TListAssignOp;
      ListB: TMacroList);
    begin
      if ListB <> nil then
        FList.Assign(ListA.FList, AOperator, ListB.FList) else
        FList.Assign(ListA.FList);
    end;
     
    procedure TMacroList.Clear;
    begin
      FList.Clear;
    end;
     
    constructor TMacroList.Create;
    begin
      FList := TObjectList.Create;
    end;
     
    procedure TMacroList.Delete(Index: Integer);
    begin
      FList.Delete(Index);
    end;
     
    destructor TMacroList.Destroy;
    begin
      FList.Free;
      inherited;
    end;
     
    procedure TMacroList.Exchange(Index1, Index2: Integer);
    begin
      FList.Exchange(Index1, Index2);
    end;
     
    function TMacroList.Expand: TMacroList;
    begin
      Result := TMacroList(FList.Expand);
    end;
     
    function TMacroList.Extract(Item: TMacro): TMacro;
    begin
      Result := Extract(Item);
    end;
     
    function TMacroList.First: TMacro;
    begin
      Result := TMacro(FList.First);
    end;
     
    function TMacroList.GetItem(Index: Integer): TMacro;
    begin
      Result := TObject(FList[Index]) as TMacro;
    end;
     
    function TMacroList.GetCapacity: Integer;
    begin
      Result := FList.Capacity;
    end;
     
    function TMacroList.GetCount: Integer;
    begin
      Result := FList.Count;
    end;
     
    function TMacroList.IndexOf(Item: TMacro): Integer;
    begin
      Result := FList.IndexOf(Item);
    end;
     
    function TMacroList.IndexOfID(AMacroID: Integer): Integer;
    begin
      Result := 0;
      while (Result < Count) and (Items[Result].MacroID <> AMacroID) do
        Inc(Result);
      if Result = Count then
        Result := -1;
    end;
     
    procedure TMacroList.Insert(Index: Integer; Item: TMacro);
    begin
      FList.Insert(Index, Item);
    end;
     
    function TMacroList.Last: TMacro;
    begin
      Result := TMacro(FList.Last);
    end;
     
    function TMacroList.LoadFromFile:boolean;
    var AFS:TFileStream;
    begin
      result:=false;
      if not FileExists(Fichier) then exit;
      AFS:=TFileStream.Create(Fichier,fmOpenRead);
      try
        result:=LoadFromStream(AFS);
      finally
        AFS.Free;
      end;
    end;
     
    function TMacroList.LoadFromStream(AStream: TStream):boolean;
    var nbreMacros,i:integer;
        AMacro:TMacro;
    begin
      try
        result:=true;
        Clear;
        FGameApp:=ReadStreamString(AStream);
        AStream.Read(nbreMacros,SizeOf(integer));
        for i:=0 to nbreMacros-1 do
        begin
          AMacro:=TMacro.Create;
          AMacro.LoadFromStream(AStream);
          Add(AMacro);
        end;
      except
        result:=false;
      end;
    end;
     
    procedure TMacroList.Move(CurIndex, NewIndex: Integer);
    begin
      FList.Move(CurIndex, NewIndex);
    end;
     
    procedure TMacroList.Pack;
    begin
      FList.Pack;
    end;
     
    procedure TMacroList.SetItem(Index: Integer; const Value: TMacro);
    begin
      FList.Items[Index] := Value;
    end;
     
    function TMacroList.Remove(Item: TMacro): Integer;
    begin
      Result := FList.Remove(Item);
    end;
     
    procedure TMacroList.SaveToFile;
    var AFS:TFileStream;
    begin
      SortByNames;
      if FileExists(FFichier) then DeleteFile(fichier);
      AFS:=TFileStream.Create(Fichier,fmCreate);
      try
        SaveToStream(AFS);
      finally
        AFS.Free;
      end;
    end;
     
    procedure TMacroList.SaveToStream(AStream: TStream);
    var nbreMacros,i:integer;
        AMacro:TMacro;
    begin
      WriteStreamString(AStream,FGameApp);
      nbreMacros:=Count;
      AStream.Write(nbreMacros,SizeOf(integer));
      for i:=0 to nbreMacros-1 do
      begin
        AMacro:=Items[i];
        AMacro.SaveToStream(AStream);
      end;
    end;
     
    procedure TMacroList.SetCapacity(const Value: Integer);
    begin
      FList.Capacity := Value;
    end;
     
    procedure TMacroList.SetCount(const Value: Integer);
    begin
      FList.Count := Value;
    end;
     
    procedure TMacroList.SetFichier(const Value: string);
    begin
      FFichier := Value;
    end;
     
    procedure TMacroList.SetGameApp(const Value: string);
    begin
      FGameApp := Value;
    end;
     
    procedure TMacroList.Sort(Compare: TListSortCompare);
    begin
      FList.Sort(Compare);
    end;
     
     
    function TMacroList.GetOwnsObjects: boolean;
    begin
      Result := FList.OwnsObjects;
    end;
     
    procedure TMacroList.SetOwnsObjects(const Value: boolean);
    begin
      FList.OwnsObjects := Value;
    end;
     
    procedure TMacroList.SortByNames;
    function CompareNames(Item1, Item2: Pointer): Integer;
    begin
      Result := CompareText(TMacro(Item1).MacroName, TMacro(Item2).MacroName);
    end;
    begin
      Sort(@CompareNames);
    end;
     
    end.
    Vas-y, tu peux utiliser sans crainte tu as l'autorisation de l'auteur. C'est sous licence GPL3, donc tu dois normalement garder l'en-tête et me citer (mais je m'en fiche, donc )

    Ensuite, tu peux utiliser ça comme ça, par exemple (une TListBox, 2 TSpeedButton (un pour commencer à "lire" le clavier, l'autre pour arrêter).
    :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    unit UnitMain;
     
    interface
     
    uses
      ...,HotKeyManager;
    type
      TMainForm = class(TForm)           
        sb_start: TSpeedButton;
        sb_Stop: TSpeedButton;   
        LB_Touches: TListBox;
        ImageList1: TImageList; // TimageList avec deux glyphes, une flèche vers le haut, une vers le bas
        procedure sb_startClick(Sender: TObject);
        procedure sb_StopClick(Sender: TObject);
        procedure FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
        procedure FormKeyUp(Sender: TObject; var Key: Word; Shift: TShiftState);   
        procedure LB_TouchesMouseDown(Sender: TObject; Button: TMouseButton;
          Shift: TShiftState; X, Y: Integer);
        procedure LB_TouchesMouseUp(Sender: TObject; Button: TMouseButton;
          Shift: TShiftState; X, Y: Integer);
       procedure LB_TouchesDrawItem(Control: TWinControl; Index: Integer;
          Rect: TRect; State: TOwnerDrawState);
     
      private
        { Déclarations privées }
        FIsrecording: boolean;
        OldTouche:TTouche;
        CurrentMacro: integer;
        { Private declarations }
        procedure UpdateTouchesList;
           procedure SetIsrecording(const Value: boolean);    
        property Isrecording:boolean read FIsrecording write SetIsrecording;    
      public
        { Déclarations publiques }
        MacroList:TMacroList;
        procedure UpdateMacroList;    
      end;
     
    procedure TMainForm.FormCreate(Sender: TObject);
    var AMacro:TMacro;
    begin
      MacroList:=TMacroList.Create;
      AMacro:=TMacro.Create;
     AMacro.MacroName:='New Macro';
     CurrentMacro:=MacroList.Add(AMacro);
    end;
     
    procedure TMainForm.FormDestroy(Sender: TObject);
    begin
      MacroList.Free;
    end;
     
    procedure TMainForm.sb_startClick(Sender: TObject);
    begin  
      Isrecording:=true;
    end;
     
    procedure TMainForm.sb_StopClick(Sender: TObject);
    begin
      Isrecording:=false;  
    end;
     
    procedure TMainForm.FormKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    var ATouche:TTouche;
    begin  
      ATouche.down:=true;
      ATouche.VK_code:=Key;
    //ça, c'est pour contourener le problême de répétition automatique des touches :
      if (Isrecording and not((OldTouche.VK_code=ATouche.VK_code)  
          and (OldTouche.down=ATouche.down))) then
      begin
        OldTouche:=ATouche;
        MacroList.Items[CurrentMacro].Add(ATouche);
        UpdateTouchesList;
        Key := 0;
      end;
    end;
     
    procedure TMainForm.FormKeyUp(Sender: TObject; var Key: Word; Shift: TShiftState);
    var ATouche:TTouche;
    begin 
      ATouche.down:=false;
      ATouche.VK_code:=Key;
    //ça, c'est pour contourener le problême de répétition automatique des touches :
      if (Isrecording and not((OldTouche.VK_code=ATouche.VK_code)
          and (OldTouche.down=ATouche.down))) then
      begin
        OldTouche:=ATouche;
        MacroList.Items[CurrentMacro].Add(ATouche);
        UpdateTouchesList;
        Key := 0;
      end;
    end;
     
     
    //La souris peut aussi être considérée comme un clavier ;)
    procedure TMainForm.LB_TouchesMouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    var ATouche:TTouche;
    begin 
      ATouche.down:=true;
      case Button of
        mbLeft:ATouche.VK_code:=VK_LBUTTON;
        mbRight:ATouche.VK_code:=VK_RBUTTON;
        mbMiddle:ATouche.VK_code:=VK_MBUTTON;
      end;
     
      if (Isrecording and not((OldTouche.VK_code=ATouche.VK_code)
          and (OldTouche.down=ATouche.down))) then
      begin
        OldTouche:=ATouche;
        MacroList.Items[CurrentMacro].Add(ATouche);
        UpdateTouchesList;
      end;
    end;
     
    procedure TMainForm.LB_TouchesMouseUp(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    var ATouche:TTouche;
    begin 
      ATouche.down:=false;
      case Button of
        mbLeft:ATouche.VK_code:=VK_LBUTTON;
        mbRight:ATouche.VK_code:=VK_RBUTTON;
        mbMiddle:ATouche.VK_code:=VK_MBUTTON;
      end;
      if (Isrecording and not((OldTouche.VK_code=ATouche.VK_code)
          and (OldTouche.down=ATouche.down))) then
      begin
        OldTouche:=ATouche;
        MacroList.Items[CurrentMacro].Add(ATouche);
        UpdateTouchesList;
      end;
    end;
     
    procedure TMainForm.LB_TouchesDrawItem(Control: TWinControl; Index: Integer;
      Rect: TRect; State: TOwnerDrawState);
    var
      Offset: Integer;      { text offset width }
      AMacro:TMacro;
      ATouche:TTouche;
    begin  
      AMAcro:=MacroList.Items[CurrentMacro];
      if index>(AMAcro.Count-1)
      then exit;
      ATouche:=AMAcro.Items[index];
      with (Control as TListBox).Canvas do  { draw on control canvas, not on the form }
      begin
        if (AMacro.IndexUp=-1) then Brush.Color:=clWhite;
        if (Index<AMacro.indexup) then Brush.Color:=clSkyBlue
                                else Brush.Color:=clMoneyGreen;
     
        FillRect(Rect);       { clear the rectangle }
        Offset := 2;          { provide default offset }
     
        if ATouche.down
        then ImageList1.Draw((Control as TListBox).Canvas,Rect.Left + Offset, Rect.Top+2,0,true)
        else ImageList1.Draw((Control as TListBox).Canvas,Rect.Left + Offset, Rect.Top+2,1,true);
     
        Offset := ImageList1.Width + 6;    { add four pixels between bitmap and text}
        TextOut(Rect.Left + Offset, Rect.Top+2, (Control as TListBox).Items[Index])  { display the text }
      end;
    end;
     
    procedure TMainForm.UpdateTouchesList;
    var i:Integer;
         ATouche:TTouche;
         Name:string; 
         AMacro:TMacro;
    begin  
      LB_Touches.Items.BeginUpdate;
      LB_Touches.Clear;
      AMAcro:=MacroList.Items[CurrentMacro];
      AMacro.ComputeIndexUp;
      for i := 0 to AMAcro.Count - 1 do
      begin
        ATouche:=AMAcro.Items[i];
        case ATouche.VK_code of
        VK_LBUTTON:Name:='Mouse Left';
        VK_RBUTTON:Name:='Mouse Right';
        VK_MBUTTON:Name:='Mouse Middle';
        else Name:=HotKeyToText(GetHotKey(0,ATouche.VK_code),true);
        end;
     
        LB_Touches.Items.Add(Name);
      end;
      LB_Touches.Items.EndUpdate;
     
    end;
     
    procedure TMainForm.SetIsrecording(const Value: boolean);
    begin
      if FIsrecording<>value then
      begin
         FIsrecording := Value;     
         sb_Stop.Enabled:=FIsrecording;
         sb_start.Enabled:=not FIsrecording;     
         OldTouche.VK_code:=0;
         LB_Touches.SetFocus;
      end;
    end;
     
     
    end.
    C'est compliqué, mais ça marche (normalement )

  9. #9
    Membre régulier
    Profil pro
    Développeur informatique
    Inscrit en
    Septembre 2009
    Messages
    168
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Septembre 2009
    Messages : 168
    Points : 120
    Points
    120
    Par défaut
    Merci je vais jeter un œil !!
    Je met résolut car je ne pense pas qu'il y est d'autre solutions ! :p

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Probleme de mise en forme avec Listener Keyboard
    Par Kefass dans le forum ActionScript 3
    Réponses: 2
    Dernier message: 11/12/2008, 17h53
  2. probleme avec les hook windows
    Par stone dans le forum Windows
    Réponses: 1
    Dernier message: 30/12/2005, 10h49
  3. Probleme de Hook souris
    Par mandagor dans le forum MFC
    Réponses: 17
    Dernier message: 07/07/2005, 17h12
  4. Réponses: 5
    Dernier message: 08/02/2005, 20h50
  5. [Hook] Clavier Microsoft office keyboard
    Par merzhin dans le forum API, COM et SDKs
    Réponses: 2
    Dernier message: 01/06/2004, 13h59

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo