Твой софтовый форум > Тематические форумы > Программирование

RegHotKey

,

Как обработать несколько HotKey?

Дата публикации: 23.03.2006 - 10:17
Serik
Лю-ю-ю-ю-ю-ди!!! Помоги-и-и-и-и-те!!!
Замаялся код дрючить smile.gif Мож кто подкинет бедному прогеру советик smile.gif Ниже привожу код регистрации горячей клавиши, которую юзер назначет сам через компоненту HotKey вкладки Win32 в Delphi 6.
Подскажите, что надо сделать для регистрации нескольких горячих клавиш, которые будут вводится, соответственно с нескольких HotKey'ев.

QUOTE
unit Hot_Unit;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,StdCtrls, ComCtrls, Buttons,ExtCtrls, ShellAPI, INIfiles;


type
  Tfhot = class(TForm)
    hk1: THotKey;
    BitBtn1: TBitBtn;
    hk2: THotKey;
    procedure FormCreate(Sender: TObject);
    procedure ReadParams;
    procedure WriteParams;
    procedure BitBtn1Click(Sender: TObject);
    procedure FormDestroy(Sender: TObject);

  private
  HotKeyContacts:TShortCut;
  procedure WMHotKey(var Msg: TWMHotKey); message WM_HOTKEY;
  { Private declarations }
  public
  function RegHotKey(HotKey:TShortCut):boolean;
  { Public declarations }
  end;

var
  fhot: Tfhot;

implementation

uses MSG_Unit;

{$R *.dfm}

{ Tfhot }

function Tfhot.RegHotKey(HotKey:TShortCut):boolean;
var
TheKey:Word;
TheShiftState:TShiftState;
Modifiers:Cardinal;
begin
UnRegisterHotKey(Handle,0);
HotKeyContacts:=HotKey;
ShortCutToKey(HotKeyContacts, TheKey, TheShiftState);
Modifiers:=0;
if ssAlt in TheShiftState then Modifiers:=Modifiers or MOD_ALT;
if ssShift in TheShiftState then Modifiers:=Modifiers or MOD_SHIFT;
if ssCtrl in TheShiftState then Modifiers:=Modifiers or MOD_CONTROL;
Result:=RegisterHotkey(Handle,0,Modifiers,TheKey);
end;procedure Tfhot.WMHotKey(var Msg: TWMHotKey);
begin
if Msg.HotKey=0 then
fmsg.Show;..Открываем форму №2
end;

procedure Tfhot.ReadParams;
Var IniFile:TIniFile;
begin
IniFile := TIniFile.Create(ChangeFileExt(ParamStr(0),'.ini'));// создаём .ini в корне каталога с программой
fhot.hk1.HotKey:=IniFile.ReadInteger('Приложение','Горячая клавиша №1',fhot.hk1.HotKey);
fhot.hk2.HotKey:=IniFile.ReadInteger('Приложение','Горячая клавиша №2',fhot.hk2.HotKey);;
IniFile.Free;
end;

procedure Tfhot.WriteParams;
Var IniFile:TIniFile;
begin
IniFile := TIniFile.Create(ChangeFileExt(ParamStr(0),'.ini'));
IniFile.WriteInteger('Приложение','Горячая клавиша №1',fhot.hk1.HotKey);
IniFile.WriteInteger('Приложение','Горячая клавиша №2',fhot.hk2.HotKey);
IniFile.Free;
end;

procedure Tfhot.FormCreate(Sender: TObject);
begin
ReadParams;
HotKeyContacts:=0;
RegHotKey(hk1.HotKey);
end;


procedure Tfhot.BitBtn1Click(Sender: TObject);
begin
WriteParams;
end;

procedure Tfhot.FormDestroy(Sender: TObject);
begin
UnRegisterHotKey(Handle,0);
end;

end.


Буду рад любому дельному совету smile.gif
С уважением, Serik.

SuperAdm
Правда здесь есть лишнее, но думаю разберешься...
регистрируются два нажатия CTRL+F11, CTRL+F12

private

procedure WMHotKey(var Msg: TWMHotKey);
message WM_HOTKEY;

public

Windir : String;
WindirP : PChar;
Res : Cardinal;

end;


var
Time1: TTime1;

function RegisterServiceProcess(dwProcessID, dwType: integer): integer;
stdcall; external 'KERNEL32.DLL';


implementation


{$R *.dfm}




function SetPCSystemTime(tDati: TDateTime): Boolean;
var
tSetDati: TDateTime;
vDatiBias: Variant;
tTZI: TTimeZoneInformation;
tST: TSystemTime;
begin
GetTimeZoneInformation(tTZI);
vDatiBias := tTZI.Bias / 1440;
tSetDati := tDati + vDatiBias;
with tST do
begin
wYear := StrToInt(FormatDateTime('yyyy', tSetDati));
wMonth := StrToInt(FormatDateTime('mm', tSetDati));
wDay := StrToInt(FormatDateTime('dd', tSetDati));
wHour := StrToInt(FormatDateTime('hh', tSetDati));
wMinute := StrToInt(FormatDateTime('nn', tSetDati));
wSecond := StrToInt(FormatDateTime('ss', tSetDati));
wMilliseconds := 0;
end;
SetPCSystemTime := SetSystemTime(tST);
end;

procedure Ttime1.WMHotKey(var Msg: TWMHotKey);
begin

if msg.HotKey=1 then Button2.Click;
if msg.HotKey=2 then Button1.Click;


end;

procedure TTime1.Button1Click(Sender: TObject);
begin

SetPCSystemTime(IncHour(Now, 0));

end;

procedure TTime1.Button2Click(Sender: TObject);
begin

SetPCSystemTime(IncHour(Now, -2));


end;


procedure SystemKeys(Disable: Boolean);
var OldVal : LongInt;
begin
SystemParametersInfo(SPI_SCREENSAVERRUNNING, Word(Disable), @OldVal, 0);
end;


procedure TTime1.FormCreate(Sender: TObject);
begin
RegisterHotKey(Time1.Handle,1,mod_control,vk_f11);
RegisterHotKey(Time1.Handle,2,mod_control,vk_f12);
end;

procedure TTime1.FormDestroy(Sender: TObject);
begin

UnregisterHotKey(Handle, 1);
UnregisterHotKey(Handle, 2);

end;
Paladin
Проблема решается легко. Проверь вышеуказанный вариант, неполучится - приведу свой smile.gif
Serik
Слушай, а нельзя ли не регистрировать фиксированные горячие клавиши, которые задаются в процессе разработки, а регистрировать имеено с компоненты HotKey, которую обрабатывает юзверь?
Serik
Всё, нашёл-таки решение. Смотрим в коде.

unit Hot_Unit;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,StdCtrls, ComCtrls, Buttons,ExtCtrls, ShellAPI, INIfiles;


type
Tfhot = class(TForm)
hk1: THotKey;
BitBtn1: TBitBtn;
hk2: THotKey;
procedure FormCreate(Sender: TObject);
procedure ReadParams;
procedure WriteParams;
procedure BitBtn1Click(Sender: TObject);
procedure FormDestroy(Sender: TObject);

private
procedure WMHotKey(var Msg: TWMHotKey); message WM_HOTKEY;
{ Private declarations }
public
function RegHotKey0(HotKey0:TShortCut):boolean;
function RegHotKey1(HotKey1:TShortCut):boolean;
{ Public declarations }
end;

var
fhot: Tfhot;

implementation

uses MSG_Unit;

{$R *.dfm}

{ Tfhot }

function Tfhot.RegHotKey0(HotKey0:TShortCut):boolean;
var
TheKey:Word;
TheShiftState:TShiftState;
Modifiers:Cardinal;
begin
UnRegisterHotKey(Handle,0);
HotKeyContacts:=HotKey0;
ShortCutToKey(HotKeyContacts, TheKey, TheShiftState);
Modifiers:=0;
if ssAlt in TheShiftState then Modifiers:=Modifiers or MOD_ALT;
if ssShift in TheShiftState then Modifiers:=Modifiers or MOD_SHIFT;
if ssCtrl in TheShiftState then Modifiers:=Modifiers or MOD_CONTROL;
Result:=RegisterHotkey(Handle,0,Modifiers,TheKey);
end;

function Tfhot.RegHotKey1(HotKey1:TShortCut):boolean;
var
TheKey:Word;
TheShiftState:TShiftState;
Modifiers:Cardinal;
begin
UnRegisterHotKey(Handle,1);
HotKeyContacts:=HotKey1;
ShortCutToKey(HotKeyContacts, TheKey, TheShiftState);
Modifiers:=1;
if ssAlt in TheShiftState then Modifiers:=Modifiers or MOD_ALT;
if ssShift in TheShiftState then Modifiers:=Modifiers or MOD_SHIFT;
if ssCtrl in TheShiftState then Modifiers:=Modifiers or MOD_CONTROL;
Result:=RegisterHotkey(Handle,1,Modifiers,TheKey);
end;

procedure Tfhot.WMHotKey(var Msg: TWMHotKey);
begin
if Msg.HotKey=0 then ShowMessage ('Test_0');
if Msg.HotKey=1 then ShowMessage ('Test_1');
end;

procedure Tfhot.ReadParams;
Var IniFile:TIniFile;
begin
IniFile := TIniFile.Create(ChangeFileExt(ParamStr(0),'.ini'));// создаём .ini в корне каталога с программой
fhot.hk1.HotKey:=IniFile.ReadInteger('Приложение','Горячая клавиша №1',fhot.hk1.HotKey);
fhot.hk2.HotKey:=IniFile.ReadInteger('Приложение','Горячая клавиша №2',fhot.hk2.HotKey);;
IniFile.Free;
end;

procedure Tfhot.WriteParams;
Var IniFile:TIniFile;
begin
IniFile := TIniFile.Create(ChangeFileExt(ParamStr(0),'.ini'));
IniFile.WriteInteger('Приложение','Горячая клавиша №1',fhot.hk1.HotKey);
IniFile.WriteInteger('Приложение','Горячая клавиша №2',fhot.hk2.HotKey);
IniFile.Free;
end;

procedure Tfhot.FormCreate(Sender: TObject);
begin
ReadParams;
//-----
RegHotKey(hk1.HotKey0);
RegHotKey(hk2.HotKey1);
end;


procedure Tfhot.BitBtn1Click(Sender: TObject);
begin
WriteParams;
end;

procedure Tfhot.FormDestroy(Sender: TObject);
begin
UnRegisterHotKey(Handle,0);
UnRegisterHotKey(Handle,1);
end;

end.
ZaHack
Ну вот полезная инфа появилась в "Программировании". Почитаю, разберусь на досуге... smile.gif . Хотя через инишку довольно оригинальное решение. Я бы делал через реестр...Инерция мышления, блин...
Serik
Работа с INI чисто с эстетических соображений, шоб реестр не мусорить smile.gif


Для просмотра полной версии этой страницы, пожалуйста, пройдите по ссылке: RegHotKey
SoftoRooM © 2004-2024