?
Solved

HOTKEY FOR MY APPLICATION

Posted on 2004-04-04
11
Medium Priority
?
427 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
  • +2
11 Comments
 
LVL 11

Accepted Solution

by:
shaneholmes earned 416 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
Industry Leaders: 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 412 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 412 total points
ID: 10776084
It's in messages unit
0

Featured Post

Industry Leaders: 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

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month12 days, 18 hours left to enroll

777 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