Solved

HOTKEY FOR MY APPLICATION

Posted on 2004-04-04
11
412 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 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
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!

 
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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
This video Micro Tutorial shows how to password-protect PDF files with free software. Many software products can do this, such as Adobe Acrobat (but not Adobe Reader), Nuance PaperPort, and Nuance Power PDF, but they are not free products. This vide…

726 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