Solved

HOTKEY FOR MY APPLICATION

Posted on 2004-04-04
11
408 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
LVL 34

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

733 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