Einzelnen Beitrag anzeigen

bluescreen25

Registriert seit: 27. Nov 2005
Ort: Geldern
229 Beiträge
 
Delphi 7 Enterprise
 
#2

Re: Hotkey speichern laden nutzen

  Alt 16. Feb 2008, 21:10
Damit wenigstens einer geantwortet hat, hier meine funktionierende Fassung: (relevante Teile)
(Die Tasten des Ziffernblocks gehen nicht, in ShortCutToText scheint ein Bug zu sein)

Speichern von Hotkeys wie folgt:

Delphi-Quellcode:
procedure TForm1.SpeicherShortCuts;
begin
  Ini.WriteString('Shortcuts','Shortcut_1',ShortCutToText(HotKey1.HotKey));
  Ini.WriteString('Shortcuts','Shortcut_2',ShortCutToText(HotKey2.HotKey));
  Ini.WriteString('Shortcuts','Shortcut_3',ShortCutToText(HotKey3.HotKey));
end;
Laden wie folgt:

Delphi-Quellcode:
uses
 Shortcuts;

var
 Shortcut_1,Shortcut_2,Shortcut_3: TShortCut;
...
...
procedure TForm1.FormCreate(Sender: TObject);
begin
...
  SetupHotkey(self);
  LadeShortCuts;
...
end;


procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
...
  ShortCut_Unregister;
...
end;


// Hotkey aus Shortcut übernehmen
procedure TForm1.SetupHotkey(Sender: TObject);
begin
  ShortCut_SetupHotkey(Sender); //Unit Shortcuts
end;


procedure TForm1.LadeShortCuts;
begin
  Shortcut_1 := TextToShortCut(Ini.ReadString('Shortcuts','Shortcut_1',''));
  Shortcut_2 := TextToShortCut(Ini.ReadString('Shortcuts','Shortcut_2',''));
  Shortcut_3 := TextToShortCut(Ini.ReadString('Shortcuts','Shortcut_3',''));
end;


// Hotkey gedrückt
procedure TForm1.WMHotkey( var msg: TWMHotkey ) ;
begin
  ShortCut_WMHotkey(msg); //Unit Shortcuts
end;
als Globale Shortcuts einrichten und nutzen:

Delphi-Quellcode:
unit Shortcuts;
...
...
procedure ShortCut_SetupHotkey(Sender: TObject);
function SetHotkey(aHandle:THandle; HotkeyID:cardinal; Shortcut:TShortcut; AutoRegister:boolean=true):boolean;
procedure ShortCut_WMHotkey( var msg: TWMHotkey) ;
procedure ShortCut_Unregister;
...
...
const
  Hotkey_ID1 = 1;
  Hotkey_ID2 = 2;
  Hotkey_ID3 = 3;
...
...

procedure ShortCut_SetupHotkey(Sender: TObject);
begin
  SetHotkey(Form1.Handle,1,ShortCut_1);
  SetHotkey(Form1.Handle,2,ShortCut_2);
  SetHotkey(Form1.Handle,3,ShortCut_3);
end;


// Hotkey registrieren
function SetHotkey(aHandle:THandle; HotkeyID:cardinal; Shortcut:TShortcut; AutoRegister:boolean=true):boolean;
var
 Key: Word;
 Shift:TShiftstate;
 Modifiers:integer;
begin
  result:=false;
  if Shortcut = 0 then exit;

  Modifiers:=0;
  Unregisterhotkey(aHandle,HotkeyID);

  ShortCutToKey(ShortCut, Key, Shift); // unit Menus;
  if ssCtrl in Shift then Modifiers:=MOD_CONTROL;
  if ssAlt in Shift then Modifiers:=Modifiers or MOD_ALT;
  if ssShift in Shift then Modifiers:=Modifiers or MOD_SHIFT;
  if AutoRegister then
   result:=RegisterHotKey(aHandle, HotkeyID, Modifiers, Key)
  else result:=true;
end;


procedure ShortCut_WMHotkey( var msg: TWMHotkey ) ;
begin
  if msg.HotKey = Hotkey_ID1 then
  begin
   //do something
  end;
  
  if msg.HotKey = Hotkey_ID2 then
  begin
   //do something
  end;

  if msg.HotKey = Hotkey_ID3 then
  begin
   //do something
  end;
end;


procedure ShortCut_Unregister;
begin
  UnregisterHotkey(Form1.handle,Hotkey_ID1);
  UnregisterHotkey(Form1.handle,Hotkey_ID2);
  UnregisterHotkey(Form1.handle,Hotkey_ID3);
end;

Ich hoffe, ich habe nix vergessen.

Gruß, bluescreen25
...und ich dachte, Delphi ist ein Programmgenerator mit nur einem Button......tzzz
  Mit Zitat antworten Zitat