Solved

HOTKEY FOR MY APPLICATION

Posted on 2004-04-04
11
402 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to fill array with TArray.Create? 14 79
oracle global variables 4 63
Tembedded WB animatid gifs not animated on some pcs 2 73
Delphi and Access based Enumeration 9 47
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…
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…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

895 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

12 Experts available now in Live!

Get 1:1 Help Now