Главная страницаОбратная связьКарта сайта

Создаём системную горячую клавишу


{ 
  The following example demonstrates registering hot keys with the 
  system to globally trap keys. 
} 

unit Unit1; 

interface 

uses 
  Windows, Messages, Forms, Dialogs; 

type 
  TForm1 = class(TForm) 
    procedure FormCreate(Sender: TObject); 
    procedure FormDestroy(Sender: TObject); 
  private 
    // Hotkey Ids 
    id1, id2, id3, id4: Integer; 
    procedure WMHotKey(var Msg: TWMHotKey); message WM_HOTKEY; 
    { Privat-Declarations} 
  public 
    { Public-Declarations} 
  end; 

var 
  Form1: TForm1; 

implementation 

{$R *.DFM} 

// Trap Hotkey Messages 
procedure TForm1.WMHotKey(var Msg: TWMHotKey); 
begin 
  if Msg.HotKey = id1 then 
    ShowMessage("Ctrl + A was pressed !"); 
  if Msg.HotKey = id2 then 
    ShowMessage("Ctrl + Alt + R was pressed !"); 
  if Msg.HotKey = id3 then 
    ShowMessage("Win + F4 was pressed !"); 
  if Msg.HotKey = id4 then 
    ShowMessage("Print Screen was pressed !"); 
end; 


procedure TForm1.FormCreate(Sender: TObject); 
  // Different Constants from Windows.pas 
const 
  MOD_ALT = 1; 
  MOD_CONTROL = 2; 
  MOD_SHIFT = 4; 
  MOD_WIN = 8; 
  VK_A = 65; 
  VK_R = 82; 
  VK_F4 = 115; 
begin 
  // Register Hotkey Ctrl + A 
  id1 := GlobalAddAtom("Hotkey1"); 
  RegisterHotKey(Handle, id1, MOD_CONTROL, VK_A); 

  // Register Hotkey Ctrl + Alt + R 
  id2 := GlobalAddAtom("Hotkey2"); 
  RegisterHotKey(Handle, id2, MOD_CONTROL + MOD_Alt, VK_R); 

  // Register Hotkey Win + F4 
  id3 := GlobalAddAtom("Hotkey3"); 
  RegisterHotKey(Handle, id3, MOD_WIN, VK_F4); 

  // Globally trap the Windows system key "PrintScreen" 
  id4 := GlobalAddAtom("Hotkey4"); 
  RegisterHotKey(Handle, id4, 0, VK_SNAPSHOT); 
end; 

// Unregister the Hotkeys 
procedure TForm1.FormDestroy(Sender: TObject); 
begin 
  UnRegisterHotKey(Handle, id1); 
  UnRegisterHotKey(Handle, id2); 
  UnRegisterHotKey(Handle, id3); 
  UnRegisterHotKey(Handle, id4); 
end; 

end. 

{ 
  RegisterHotKey fails if the keystrokes specified for the hot key have 
  already been registered by another hot key. 
   
  Windows NT4 and Windows 2000/XP: The F12 key is reserved for use by the 
  debugger at all times, so it should not be registered as a hot key. Even 
  when you are not debugging an application, F12 is reserved in case a 
  kernel-mode debugger or a just-in-time debugger is resident. 
}


Обсудить статью на форуме


Если Вас заинтересовала или понравилась информация по разработке на Delph - "Создаём системную горячую клавишу", Вы можете поставить закладку в социальной сети или в своём блоге на данную страницу:

Так же Вы можете задать вопрос по работе этого модуля или примера через форму обратной связи, в сообщение обязательно указывайте название или ссылку на статью!
   


Copyright © 2008 - 2023 Дискета.info