Ignore:
Timestamp:
Mar 2, 2021, 10:45:43 PM (4 years ago)
Author:
chronos
Message:
  • Modified: Store key bindings in registry.
File:
1 moved

Legend:

Unmodified
Added
Removed
  • trunk/LocalPlayer/UKeyBindings.pas

    r291 r292  
    66
    77uses
    8   Classes, SysUtils, fgl, LCLProc, LCLType, Menus;
     8  Classes, SysUtils, fgl, LCLProc, LCLType, Menus, Registry;
    99
    1010type
     
    2323
    2424  TKeyBindings = class(TFPGObjectList<TKeyBinding>)
     25  public
    2526    function AddItem(const ShortName, FullName: string; ShortCut: TShortCut; ShortCut2: TShortCut = 0): TKeyBinding; overload;
    2627    function AddItem(const ShortName, FullName: string; ShortCutText: string; ShortCutText2: string = ''): TKeyBinding; overload;
    2728    function Search(ShortName: string): TKeyBinding;
    28     procedure LoadFromFile(FileName: string);
    29     procedure SaveToFile(FileName: string);
     29    procedure LoadFromRegistry(RootKey: HKEY; Key: string);
     30    procedure SaveToRegistry(RootKey: HKEY; Key: string);
    3031  end;
    3132
     
    151152end;
    152153
    153 procedure TKeyBindings.LoadFromFile(FileName: string);
    154 var
    155   Lines: TStringList;
     154procedure TKeyBindings.LoadFromRegistry(RootKey: HKEY; Key: string);
     155var
    156156  I: Integer;
    157   KeyBinding: TKeyBinding;
    158 begin
    159   Lines := TStringList.Create;
    160   Lines.NameValueSeparator := '=';
     157  Registry: TRegistry;
     158  Text: string;
     159begin
     160  Registry := TRegistry.Create;
     161  Registry.RootKey := RootKey;
     162  with Registry do
    161163  try
    162     Lines.LoadFromFile(FileName);
    163     for I := 0 to Lines.Count - 1 do begin
    164       KeyBinding := Search(Lines.Names[I]);
    165       if Assigned(KeyBinding) then begin
    166         KeyBinding.ShortCut := TextToShortCut(Lines.ValueFromIndex[I]);
     164    OpenKey(Key, True);
     165    for I := 0 to Count - 1 do begin
     166      Text := '';
     167      if ValueExists(Items[I].ShortName) then begin
     168        Text := ReadString(Items[I].ShortName);
     169        if Pos(',', Text) > 0 then begin
     170          Items[I].ShortCut2 := TextToShortCut(Copy(Text, Pos(',', Text) + 1, MaxInt));
     171          Items[I].ShortCut := TextToShortCut(Copy(Text, 1, Pos(',', Text) - 1));
     172        end else begin
     173          Items[I].ShortCut := TextToShortCut(Text);
     174          Items[I].ShortCut2 := 0;
     175        end;
     176      end else begin
     177        Text := ShortCutToText(Items[I].ShortCut);
     178        if Items[I].ShortCut2 <> 0 then Text := Text + ',' + ShortCutToText(Items[I].ShortCut2);
     179        WriteString(Items[I].ShortName, Text);
    167180      end;
    168181    end;
    169182  finally
    170     FreeAndNil(Lines);
    171   end;
    172 end;
    173 
    174 procedure TKeyBindings.SaveToFile(FileName: string);
    175 var
    176   Lines: TStringList;
     183    Free;
     184  end;
     185end;
     186
     187procedure TKeyBindings.SaveToRegistry(RootKey: HKEY; Key: string);
     188var
    177189  I: Integer;
    178 begin
    179   Lines := TStringList.Create;
    180   Lines.NameValueSeparator := '=';
     190  Registry: TRegistry;
     191  Text: string;
     192begin
     193  Registry := TRegistry.Create;
     194  Registry.RootKey := RootKey;
     195  with Registry do
    181196  try
     197    OpenKey(Key, True);
    182198    for I := 0 to Count - 1 do begin
    183       Lines.Add(TKeyBinding(Items[I]).ShortName + '=' + ShortCutToText(TKeyBinding(Items[I]).ShortCut));
     199      Text := ShortCutToText(Items[I].ShortCut);
     200      if Items[I].ShortCut2 <> 0 then Text := Text + ',' + ShortCutToText(Items[I].ShortCut2);
     201      WriteString(Items[I].ShortName, Text);
    184202    end;
    185     Lines.SaveToFile(FileName);
    186203  finally
    187     FreeAndNil(Lines);
     204    Free;
    188205  end;
    189206end;
Note: See TracChangeset for help on using the changeset viewer.