Custom method for global hotkeys of Delphi Program

Source: Internet
Author: User

Global hotkeys mean that whenProgramWhen the current window is not used, you can also respond by pressing the hotkey.

In the past, we used to compile a keyboard hook DLL to filter all keyboard messages in the system. Of course, we need to write another program to call it. This method is troublesome, but hackers steal the prototype of the keyboard and password.

The method described here is to use the Windows API to directly register the hotkey, in three steps:
1. Add a global atom
2. Register a global hotkey for atom (the corresponding Keyboard Message will be allocated to this program)
3. Write corresponding hotkey Response ProcessingCode

Further, if you need to customize the hotkey, you need to put a standard thotkey control on the interface. This control allows you to enter the hotkey by yourself, but its output is tshortcut, there are several functions in the menus unit (shortcuttokey, shortcuttotext) that can convert tshortcut into a document and other types.

The Code is as follows:

Program code

Uses
..., Menus;

Type
Tform1 = Class (tform)
...
Private
Hotkey_key: word;
Hotkey_shift: word;
Aatom: atom;
Procedure hotkey (var msg: tmessage); message wm_hotkey; // defines the global hotkey message event
...
End;

....

Procedure tform1.formcreate (Sender: tobject );
Begin
Aatom: = globaladdatom ('myhotkey'); // global hotkey ID
Gethotkey (hotkey1.hotkey );
End;

Procedure tform1.formdestroy (Sender: tobject );
Begin
Unregisterhotkey (handle, aatom); // unregister the global hotkey
Globaldeleteatom (aatom );
End;

procedure tform1.hotkey (var msg: tmessage ); // hotkey RESPONSE event
begin
If (MSG. lparamhi = hotkey_key) and (MSG. lparamlo = hotkey_shift) Then // global hotkey
begin
showmessage ('hot key pressed! ');
end;

procedure tform1.button1click (Sender: tobject); // register the hotkey
var
T: tshiftstate;
function shiftstatetoword (shift: tshiftstate): word;
begin
If ssshift in shift then result: = mod_shift else result: = 0;
If ssctrl in shift then result: = result or mod_control;
If ssalt in shift then result: = result or mod_alt;
end;
begin
shortcuttokey (hotkey1.hotkey, hotkey_key, T);
hotkey_shift: = shiftstatetoword (t);
registerhotkey (handle, aatom, hotkey_shift, hotkey_key); // register the global hotkey
end;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.