Solved

HOTKEY FOR MY APPLICATION

Posted on 2004-04-04
11
401 Views
Last Modified: 2010-04-05
I like to make hotkey for my application.
My application is quiz game so I want  "F9"  to play and  "F10"  to stop it.
0
Comment
Question by:sal1150
  • 2
  • 2
  • 2
  • +2
11 Comments
 
LVL 11

Accepted Solution

by:
shaneholmes earned 104 total points
ID: 10753472
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 );
  Begin
    If msg.hotkey = 1 Then
   Begin
    //do something
   End;
  End;


Shane
0
 
LVL 17

Expert Comment

by:mokule
ID: 10753501
Other possibility is to set ShortCut property for MenuItem or Action if You are using Menus or AcionList
0
 
LVL 11

Expert Comment

by:shaneholmes
ID: 10753596

  private
    { Private declarations }
    Procedure WMHotkey( Var msg: TWMHotkey ); message WM_HOTKEY;



Procedure TForm1.WMHotkey( Var msg: TWMHotkey );
begin
 if msg.hotkey = 1 Then
 begin
  //Start Game
  ShowMessage('Start Game');
 end;
 if msg.hotkey = 2 Then
 begin
  //End Game
  ShowMessage('End Game');
 end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
 If not RegisterHotkey( Form1.Handle, 1, MOD_ALT or MOD_SHIFT, VK_F9 ) Then
    ShowMessage('Unable to assign F9 as hotkey.');
If not RegisterHotkey( Form1.Handle, 2, MOD_ALT or MOD_SHIFT, VK_F10 ) Then
    ShowMessage('Unable to assign F10 as hotkey.');
end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  UnRegisterHotkey( Form1.Handle, 1 );
  UnRegisterHotkey( Form1.Handle, 2 );
end;

SHane
0
 
LVL 11

Expert Comment

by:ZhaawZ
ID: 10753602
You can also use Form's onShortCut event.

procedure TForm1.FormShortCut(var Msg: TWMKey; var Handled: Boolean);
begin
case msg.CharCode of
  120 : begin { F9 pressed }; Handled := true; end;
  121 : begin { F10 pressed }; Handled := true; end;
end;
end;
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 33

Assisted Solution

by:Slick812
Slick812 earned 103 total points
ID: 10754774
Hot keys are set quit often, so you can NEVER tell what hot key has been set by a onload program or Word shorkcut or many other stuff..  so you might  cycle through the F  keys to get one that has NOT been set

procedure TForm1.FormShow(Sender: TObject);  
var
i: Integer;
NoHot: Boolean;
begin
if not  First then Exit;
NoHot := False;
if not RegisterHotKey(Handle, 222 {ID number from 0 to 49150}, 0, VK_F9) then
  begin
  NoHot := True;
  for i := 122 downto 113 do
    begin
    if RegisterHotKey(Handle, 222, 0, i) then
      begin
      ShowMessage('A New HotKey was set as F'+IntToStr(i-111));
      NoHot := False;
      Break;
      end;
    end;
  end;
 
if NoHot then
for i := 123 downto 113 do
  begin
  if RegisterHotKey(Handle, 222, MOD_SHIFT, i) then
    begin
    ShowMessage('A New HotKey was set as SHIFT + F'+IntToStr(i-111));
    NoHot := False;
    Break;
    end;
  end;
if NoHot then
ShowMessage('ERROR - NO Hot Key was Allowed');
end;
0
 

Author Comment

by:sal1150
ID: 10772063
I tray to applay Shane code but I get this error messge:-

[Error] Unit1.pas(111): Undeclared identifier: 'TWMHotkey'
0
 

Author Comment

by:sal1150
ID: 10775783
which unit shoud i use?
0
 
LVL 17

Assisted Solution

by:mokule
mokule earned 103 total points
ID: 10776084
It's in messages unit
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now