Регистрация горячих системных клавиш
Регистрация горячих системных клавиш
Регистрация горячих системных клавиш {the following example demonstrates registering hotkeys with the system to globally trap keys} unit unit1; interface uses windows, messages, sysutils, classes, graphics, controls, forms, dialogs; type tform1 = class(tform) procedure formcreate(sender: tobject); procedure formdestroy(sender: tobject); private { private declarations } id1, id2, id3, id4: integer; procedure wmhotkey(var msg: twmhotkey); message wm_hotkey; 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 pressed !'); if msg.hotkey = id2 then showmessage('ctrl + alt + r pressed !'); if msg.hotkey = id3 then showmessage('win + f4 pressed !'); if msg.hotkey = id4 then showmessage('print screen 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 = $41; vk_r = $52; vk_f4 = $73; 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); globaldeleteatom(id1); unregisterhotkey(handle, id2); globaldeleteatom(id2); unregisterhotkey(handle, id3); globaldeleteatom(id3); unregisterhotkey(handle, id4); globaldeleteatom(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. }