Article View: borland.public.cppbuilder.nativeapi
Article #3211Re: How to use RegisterHotKey?
From: JD
Date: Sun, 30 Jul 2006 10:26
Date: Sun, 30 Jul 2006 10:26
96 lines
3219 bytes
3219 bytes
"bashar-t" <bashar-t@scs-net.org> wrote:
>
>How to use RegisterHotKey?
>How to make a global hotkey in Borland C++ builder 2006 or bcb6?
>please simple example.
Bob posted a link to a simple groups google. From that link,
I found this in about 2 minutes:
<start>
In the main forms OnCreate handler assign the hotkey:
If not RegisterHotkey( Handle, 1, MOD_ALT or MOD_SHIFT, VK_F9 ) Then
ShowMessage('Unable to assign Alt-Shift-F9 as hotkey.');
In the main forms OnClose event remove the handler:
UnRegisterHotkey( Handle, 1 );
Add a handler for the WM_HOTKEY message to the form:
private // form declaration
Procedure WMHotkey( Var msg: TWMHotkey );
message WM_HOTKEY;
Procedure TForm1.WMHotkey( Var msg: TWMHotkey );
Var
S: String;
Begin
If msg.hotkey = 1 Then Begin
...do stuff
End;
End;
<end>
That translates to (with minor modifications):
#define MY_HOTKEY 0x0000 //0x0000 through 0xBFFF are valid
private: // User declarations
bool Registered;
MESSAGE void __fastcall WMHotKey( TWMHotKey &Message );
public:
BEGIN_MESSAGE_MAP
VCL_MESSAGE_HANDLER( WM_HOTKEY, TWMHotKey, WMHotKey )
END_MESSAGE_MAP( TForm )
__fastcall TForm1(TComponent* Owner);
__fastcall ~TForm1();
//-------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner)
{
Registered = ::RegisterHotkey( Handle,
MY_HOTKEY,
MOD_ALT | MOD_SHIFT,
VK_F9 );
if( ! Registered )
{
ShowMessage( "Unable to assign Alt-Shift-F9 as hotkey." );
}
}
//-------------------------------------------------------------
__fastcall TForm1::~TForm1()
{
if( Registered )
{
::UnRegisterHotkey( Handle, MY_HOTKEY );
}
}
//-------------------------------------------------------------
MESSAGE void __fastcall TForm1::WMHotKey( TWMHotKey &Message )
{
TForm::Dispatch( &Message );
if( Message.HotKey == MY_HOTKEY )
{
// There you go.
}
}
//-------------------------------------------------------------
10 minutes total. You should have been able to do that
yourself with what Bob gave you.
~ JD
Technical Details
Message-ID:
<44cc8958$1@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!g2news2.google.com!news2.google.com!news.germany.com!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mailReferences:
<44cc51b5$1@newsgroups.borland.com>Show Full Headers
Complete Article Headers
X-Google-Thread: 10cc67,d51d8a23582c0543 X-Google-Attributes: gid10cc67,public X-Google-Language: ENGLISH,ASCII-7-bit From: "JD" <nospam@nospam.com> Sender: "JD" <nospam@nospam.com> Reply-To: "JD" <nospam@nospam.com> Subject: Re: How to use RegisterHotKey? Newsgroups: borland.public.cppbuilder.nativeapi X-User-Info: 152.163.100.201 X-Forwarded-For: 172.145.95.179 References: <44cc51b5$1@newsgroups.borland.com> NNTP-Posting-Host: 207.105.83.66 Message-ID: <44cc8958$1@newsgroups.borland.com> Date: 30 Jul 2006 03:26:32 -0700 X-Trace: newsgroups.borland.com 1154255192 207.105.83.66 (30 Jul 2006 03:26:32 -0700) Lines: 97 Path: arc1-ca.pugleaf.net!g2news2.google.com!news2.google.com!news.germany.com!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail