Not:

Hook Hotkey The system hotkey

The hotkey () function defines a system-wide hotkey.

BOOL RegisterHotKey (HWND HWND, inTID, UINT fsModifiers, UINT vK);

Parameter Description:

HWnd: handle of the window for receiving the WM_HOTKEY message. Registerhotkey If this parameter is NULL, the WM_HOTKEY message passed to the calling thread must be processed in the message loop.

Id: identifies the hotkey. Other hotkeys in the calling thread cannot use the same identifier. The application function must define a value in the 0x0000-0xBFFf range. A shared dynamic link library (DLL) must define a value in the 0xC000-0xFFFF range as returned by the GlobalAddAtom function. To avoid conflicts with hotkeys defined by other dynamically linked libraries, a DLL must use the GlobalAddAtom function to obtain the identifier of the hotkey.

FsModifoers: Defines the key that must be pressed along with the key defined by the nVirtKey parameter in order to produce the WM_HOTKEY message. This parameter can be a combination of the following values:

MOD_ALT: You can press any Alt key. MOD_CONTROL: Any Ctrl key can be pressed.

MOD_SHIFT: Any Shift key can be pressed.

MOD_WIN: Any Windows key can be pressed. These keys can be logged using Microsoft Windows logs.

Vk: defines the virtual key code of a hotkey.

Return value: Returns a non-O value if the function call succeeds. If the function call fails, the return value is 0. To get more error information, call the GetLastError function.

Note: When a key is accepted, the system looks for a match among all hotkeys. Once a matching hotkey is found, the WM_HOTKEY message is passed to the message queue of the thread that registered the hotkey. The message is delivered to the queue head, so it is removed in the next message loop. This function cannot associate a hotkey with a window created by another thread

To come.

If a keystroke defined for one hotkey is defined by another hotkey, the RegisterHotKey function call fails. registerhotkey

If the hWnd parameter identifier window has registered a hotkey with the same identifier defined for the ID parameter, the new values of the fsModifiers and VK parameters will replace the previously defined values for these parameters.

Windows CE: Windows CE 2.0 and above supports an additional flag bit for the parameter fsModifiers. Called MOD_KEYUP.

If MOD_KEYUP is set, the window will send a WM_HOTKEY message when a key is pressed or thrown.

RegisterHotKey can be used to register hotkeys between programs.

Quick check: Windows NT: 3.1 and later; Windows: 95 or later; Windows CE: Not supported. Header file: winuser.h; Library file: user32.lib.

UnregisterHotKey()

= = = = = = = = = = = = = = = = = = = = = = = = = = =

Function This function is used to inject and eliminate hot keys in the system.

BOOL UnregisterHotKey(HWND HWND, int ID);

Arguments: Arguments to this function have roughly the same meaning as the two arguments to RegisiterHotKey.

UnitFormMain.h private: ATOM HotKeyStart, HotKeyEnd; void __fastcall WndProc(Messages::TMessage &Message); UnitFormMain.cpp void __fastcall TFormMain::FormCreate(TObject *Sender) { HotKeyStart = GlobalAddAtom("HotKeyStart"); HotKeyEnd = GlobalAddAtom("HotKeyEnd"); // Register HotKey RegisterHotKey(Handle, HotKeyStart, MOD_CONTROL, VK_F2); RegisterHotKey(Handle, HotKeyEnd, MOD_CONTROL, VK_F3); } //--------------------------------------------------------------------------- void __fastcall TFormMain::WndProc(Messages::TMessage &Message) { if (Message.Msg == WM_HOTKEY) { // LParamHi: VirtualKey // LParamLo: key-modifier if (Message.LParamHi == VK_F2) Start(); Else if (message.lparamhi == VK_F3) End(); } TForm::WndProc(Message); } //--------------------------------------------------------------------------- void __fastcall TFormMain::FormDestroy(TObject *Sender) { // Unregister HotKey if(HotKeyStart) UnregisterHotKey(Handle, HotKeyStart); GlobalDeleteAtom(HotKeyStart); if(HotKeyEnd) UnregisterHotKey(Handle, HotKeyEnd); GlobalDeleteAtom(HotKeyEnd); }Copy the code

Demo2:

void __fastcall TForm1::FormCreate(TObject *Sender)

{

int tid;

strList=new TStringList();

tid=GlobalFindAtom(AnsiString(“MyHotkey”).c_str());

if(tid==0)

{

id=GlobalAddAtom(AnsiString(“MyHotkey”).c_str());

}

else

{

id=tid;

}

strList->Add(IntToStr(id));

RegisterHotKey(Handle, id, MOD_CONTROL, VK_F1);

}

//—————————————————————————



void __fastcall TForm1::FormDestroy(TObject *Sender)

{

UnregisterHotKey(Handle,StrToInt(strList->Strings[0]));

GlobalDeleteAtom(StrToInt(strList->Strings[0]));

}

//—————————————————————————

void __fastcall TForm1::ApplicationEvents1Message(tagMSG &Msg,

bool &Handled)

{

if(Msg.message==WM_HOTKEY && ((int)Msg.wParam)==StrToInt(strList->Strings[0]))

{

//SetForegroundWindow(Handle);

//ShowMessage(“Ok”);

this->Visible=!this->Visible;

}

}

 

Ahift-ctrl-alt-

Ahift-ctrl-alt-

Ahift-ctrl-alt-

 

demo3:

unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, Buttons, ComCtrls; type TForm1 = class(TForm) BitBtn1: TBitBtn; HotKey1: THotKey; procedure BitBtn1Click(Sender: TObject); procedure FormCreate(Sender: TObject); private procedure wmhotkey(var Msg: TMessage); message WM_HOTKEY; { Private declarations } public { Public declarations } end; var Form1: TForm1; Key, Shift: Word; {TODO: keystroke} implementation {$R *.dfm} function ShiftStateToWord(Shift:TShiftState):Word; begin Result:= 0; If ssShift in Shift then Result :=MOD_SHIFT; if ssCtrl in Shift then Result :=Result or MOD_CONTROL; if ssAlt in Shift then Result:=Result or MOD_ALT; end; procedure ShortCutToKey(ShortCut: TShortCut; var Key: Word; var Shift: TShiftState); begin Key := ShortCut and not (scShift + scCtrl + scAlt); Shift := []; if ShortCut and scShift <> 0 then Include(Shift, ssShift); if ShortCut and scCtrl <> 0 then Include(Shift, ssCtrl); if ShortCut and scAlt <> 0 then Include(Shift, ssAlt); end; procedure TForm1.BitBtn1Click(Sender: TObject); var T: TShiftState; begin ShortCutToKey(HotKey1.HotKey, Key, T); Shift := ShiftStateToWord(T); RegisterHotKey(Handle, GlobalAddAtom('MyHotKey') - $C000, Shift, Key); end; procedure TForm1.wmhotkey(var Msg: TMessage); begin if (Msg.LparamLo = Shift) and (Msg.LParamHi = Key) then ShowMessage('OK '); end; procedure TForm1.FormCreate(Sender: TObject); begin RegisterHotKey(Handle, GlobalAddAtom('hotkey'), MOD_WIN, VK_SPACE); //space end; end.Copy the code

 

demo4:

unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, ComCtrls; type TForm1 = class(TForm) HotKey1: THotKey; Memo1: TMemo; CheckBox1: TCheckBox; CheckBox2: TCheckBox; CheckBox3: TCheckBox; CheckBox4: TCheckBox; procedure FormCreate(Sender: TObject); procedure CheckBox1Click(Sender: TObject); end; var Form1: TForm1; implementation {$R *.dfm} procedure TForm1.CheckBox1Click(Sender: TObject); begin HotKey1.Modifiers := []; if CheckBox1.Checked then HotKey1.Modifiers := HotKey1.Modifiers + [hkShift]; if CheckBox2.Checked then HotKey1.Modifiers := HotKey1.Modifiers + [hkCtrl]; if CheckBox3.Checked then HotKey1.Modifiers := HotKey1.Modifiers + [hkAlt]; if CheckBox4.Checked then HotKey1.Modifiers := HotKey1.Modifiers + [hkExt]; end; procedure TForm1.FormCreate(Sender: TObject); begin Memo1.Clear; Memo1.Align := alTop; Memo1.ScrollBars := ssVertical; Memo1.lines.add ('1, you're not seeing TEdit, it's a HotKey control for shortcuts; '); Memo1.lines. Add('2, after activating, type a few letters to try; '); Memo1. Lines. The Add (' 3, press Ctrl + *; * represents any key; '); Memo1. Lines. The Add (' 4, press Ctrl + Alt + x; '); Memo1.lines. Add('5, Shift+Ctrl+* or Shift+Alt+*; '); Memo1.lines. Add('6, Shift+Ctrl+Alt+* can also; '); Memo1.lines. Add('7, but Shift+* can't; Because hotkey. InvalidKeys disables it by default; '); Memo1.lines.add ('8, but we can open it with hotkey.modifiers '); CheckBox1.Caption := 'hkShift'; CheckBox2.Caption := 'hkCtrl'; CheckBox3.Caption := 'hkAlt'; CheckBox4.Caption := 'hkExt'; CheckBox1.Checked := False; CheckBox2.Checked := False; CheckBox3.Checked := True; CheckBox4.Checked := False; CheckBox2.OnClick := CheckBox1.OnClick; CheckBox3.OnClick := CheckBox1.OnClick; CheckBox4.OnClick := CheckBox1.OnClick; end; end.Copy the code

 

BCB KEY/C++ emulated virtual keyboard KEY table

Keyboard VK key list

/* Virtual Keys, Standard Set*/

VK_LBUTTON                                      0x01

VK_RBUTTON                                     0x02

VK_CANCEL                                    0x03

VK_MBUTTON                                     0x04

#define VK_LBUTTON 0x01

#define VK_RBUTTON 0x02

#define VK_CANCEL         0x03    //Ctrl + Break

#define VK_MBUTTON 0x04 /* NOT contiguous with L & RBUTTON */

#define VK_BACK 0x08 //Backspace key

#define VK_TAB 0x09 //Tab key

#define VK_CLEAR          0x0C

#define VK_RETURN 0x0D

#define VK_SHIFT          0x10

#define VK_CONTROL        0x11

#define VK_MENU 0x12

#define VK_PAUSE          0x13

#define VK_CAPITAL 0x14 //Caps Lock

#define VK_KANA           0x15

#define VK_HANGEUL        0x15 /* old name – should be here for compatibility */

#define VK_HANGUL         0x15

#define VK_JUNJA          0x17

#define VK_FINAL          0x18

#define VK_HANJA          0x19

#define VK_KANJI          0x19

#define VK_ESCAPE 0x1B

#define VK_CONVERT        0x1C

#define VK_NONCONVERT     0x1D

#define VK_ACCEPT         0x1E

#define VK_MODECHANGE     0x1F

#define VK_SPACE 0x20 // space

#define VK_PRIOR 0x21 //Page Up

#define VK_NEXT 0x22 //Page Down key

#define VK_END 0x23

#define VK_HOME 0x24 //Home key

#define VK_LEFT 0x25

#define VK_UP             0x26

#define VK_RIGHT          0x27

#define VK_DOWN           0x28

#define VK_SELECT         0x29

#define VK_PRINT          0x2A

#define VK_EXECUTE        0x2B

#define VK_SNAPSHOT 0x2C //Print Screen

#define VK_INSERT 0x2D

#define VK_DELETE 0x2E

#define VK_HELP           0x2F

/* VK_0 thru VK_9 are the same as ASCII ‘0’ thru ‘9’ (0x30 – 0x39) */

/* VK_A thru VK_Z are the same as ASCII ‘A’ thru ‘Z’ (0x41 – 0x5A) */

#define VK_LWIN 0x5B

#define VK_RWIN 0x5C

#define VK_APPS 0x5D //AppsKey

#define VK_NUMPAD0 0x60

#define VK_NUMPAD1        0x61

#define VK_NUMPAD2        0x62

#define VK_NUMPAD3        0x63

#define VK_NUMPAD4        0x64

#define VK_NUMPAD5        0x65

#define VK_NUMPAD6        0x66

#define VK_NUMPAD7        0x67

#define VK_NUMPAD8        0x68

#define VK_NUMPAD9        0x69

#define VK_MULTIPLY 0x6A

#define VK_ADD 0x6B

#define VK_SEPARATOR 0x6C //

#define VK_SUBTRACT 0x6D //

#define VK_DECIMAL 0x6E

#define VK_DIVIDE         0x6F

#define VK_F1 0x70

#define VK_F2             0x71

#define VK_F3             0x72

#define VK_F4             0x73

#define VK_F5             0x74

#define VK_F6             0x75

#define VK_F7             0x76

#define VK_F8             0x77

#define VK_F9             0x78

#define VK_F10            0x79

#define VK_F11            0x7A

#define VK_F12            0x7B

#define VK_F13            0x7C

#define VK_F14            0x7D

#define VK_F15            0x7E

#define VK_F16            0x7F

#define VK_F17            0x80

#define VK_F18            0x81

#define VK_F19            0x82

#define VK_F20            0x83

#define VK_F21            0x84

#define VK_F22            0x85

#define VK_F23            0x86

#define VK_F24            0x87

#define VK_NUMLOCK 0x90 //Num Lock key

#define VK_SCROLL 0x91 //Scroll Lock key

/ *

* VK_L* & VK_R* – left and right Alt, Ctrl and Shift virtual keys.

* Used only as parameters to GetAsyncKeyState() and GetKeyState().

* No other API or message will distinguish left and right keys in this way.

* /

#define VK_LSHIFT          0xA0

#define VK_RSHIFT          0xA1

#define VK_LCONTROL        0xA2

#define VK_RCONTROL        0xA3

#define VK_LMENU           0xA4

#define VK_RMENU           0xA5

#if(WINVER >= 0x0400)

#define VK_PROCESSKEY      0xE5

#endif /* WINVER >= 0x0400 */

#define VK_ATTN            0xF6

#define VK_CRSEL           0xF7

#define VK_EXSEL           0xF8

#define VK_EREOF           0xF9

#define VK_PLAY            0xFA

#define VK_ZOOM            0xFB

#define VK_NONAME          0xFC

#define VK_PA1             0xFD

#define VK_OEM_CLEAR       0xFE

 

 

How to register global hotkeys (c++builder)

Well, I’ll give you a routine that includes the use of ResiterHotKey and the use of message handling, so you can take a look. This application has a form Form1, on which a hotkey Ctrl+F11 is registered. From now on, whenever the application is running, wherever you press Ctrl+F11, a message box will pop up. //Unit.H //————————————————————————— #ifndef Unit1H #define Unit1H //————————————————————————— #include

#include

#include

#include
//————————————————————————— class TForm1 : public TForm { __published: // IDE-managed Components void __fastcall FormCreate(TObject *Sender); void __fastcall FormDestroy(TObject *Sender); Void __fastCall WMHotKey(TMessage &Msg); private: // User declarations // this function is a message handler that is connected to the specified message via the VCL_MESSAGE_HANDLER macro. public: // User declarations __fastcall TForm1(TComponent* Owner); BEGIN_MESSAGE_MAP (WM_HOTKEY) {start (WM_HOTKEY) {start (WM_HOTKEY); Pressing a hotkey emits this message to the specified window VCL_MESSAGE_HANDLER(WM_HOTKEY, TMessage, WMHotKey); END_MESSAGE_MAP(TForm); }; //————————————————————————— extern PACKAGE TForm1 *Form1; //————————————————————————— #endif //Unit1.CPP //————————————————————————— #include

#pragma hdrstop #include “Unit1.h” //————————————————————————— #pragma package(smart_init) #pragma resource “*.dfm” TForm1 *Form1; //————————————————————————— __fastcall TForm1::TForm1(TComponent* Owner) : TForm (the Owner) {} / / — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — / / this is the realization of the message processing function void __fastcall TForm1::WMHotKey(TMessage &Msg) { SetForegroundWindow(Handle); ShowMessage(“Hot Key Registered!” ); } //————————————————————————— void __fastcall TForm1::FormCreate(TObject *Sender) { RegisterHotKey(Handle, 0x1000, MOD_CONTROL, VK_F11); Handle is a window Handle, 0x1000 is an ID number, set the unique value of this hotkey in the thread //, and the third parameter indicates which system keys to press. The last one is the key of virtual key code specific can see MSDN help} / / / / — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — void __fastcall TForm1::FormDestroy(TObject *Sender) { UnregisterHotKey(Handle, 0x1000); // Deregister hotkey 0x1000