Solved

Detecting a KeyPress

Posted on 2001-06-22
4
173 Views
Last Modified: 2010-04-06
hi,
how do i get a key that was pressed iven if my application doesn't have focus.
I down't want any asm source of keyboard hooks(down't work while directx apps are runing).


Thanks

_LR_

PS. I would like some source.
0
Comment
Question by:_lr_
  • 2
4 Comments
 
LVL 9

Expert Comment

by:ITugay
ID: 6218510
Hi _lr_,

try this code, I wrote it long time ago but hope it still works. It show pressed keys and name of form where it was pressed.

-----
Igor

type
  TForm1 = class(TForm)
    Listbox1: TListBox;
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  public
    procedure WMHotKey(var Msg : TWMHotKey); message WM_HOTKEY;
    procedure RegKeys;
    procedure UnRegKeys;
  end;                    

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.RegKeys;
var I : integer;
begin
   for I:=0 to 255 do
   if I<>16 then // skip [Shift]
   begin
     RegisterHotKey(Handle,I,0,I);
     RegisterHotKey(Handle,I+256,MOD_SHIFT,I);
   end;
end;

procedure TForm1.UnRegKeys;
var I : integer;
begin
  for I:=0 to 511 do
  UnregisterHotKey(Handle,I);
end;

procedure TForm1.WMHotKey(var Msg : TWMHotKey);
var NameBuf  : array[0..1000] of char;
    FormName : string;
    Key      : string;
    Ch       : char;
begin
   SetLength(FormName,GetWindowText(GetForegroundWindow,@NameBuf,1000));
   Move(NameBuf,pointer(FormName)^,length(FormName));

   if MSG.HotKey > 255 then
   begin
     MSG.HotKey:=MSG.HotKey-256;
     Key:='[Shift]+';
   end else Key:='';
   
   Ch:=Char(MapVirtualKey(MSG.HotKey,2));
   if Ch = ' ' then Key:=Key+'[ ]' else
   if Ch in [#33..#127]
      then Key:=Key+Ch
      else Key:=Key+'['+IntToStr(Msg.HotKey)+']';
   Listbox1.Items.Add(Key+' '+FormName);
   Listbox1.ItemIndex:=Listbox1.Items.Count-1;
   UnregKeys; // avoid recursion
   keybd_event(MSG.HotKey,MSG.HotKey,0,0);
   RegKeys;
end;


procedure TForm1.FormCreate(Sender: TObject);
begin                                    
   RegKeys;
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
   UnregKeys;
end;

end.
0
 

Author Comment

by:_lr_
ID: 6218925
ITugay,
that doesn't work when there is a directx fulscreen app runing. I tested with DaveMBMX and it didn't work, and quake2 crached when i tired to load it.

_LR_
0
 
LVL 9

Expert Comment

by:ITugay
ID: 6218984
May be, may be.
I never try it for DirectX fullscreen applications. I'm think you should use some DirectX functions instead of API's. Sorry, I'm not experienced in this area.
-----
Igor.

0
 
LVL 3

Accepted Solution

by:
f15iaf earned 100 total points
ID: 6219288
use getasynckeystate function On Ttimer component each 1/1000 seconds
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
add combobox item based on numbers 9 124
How to call a form that is in a DLL  from an application? 13 65
LAN or WAN ? 11 82
Firemonkey android show image from resource ? 1 33
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
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…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

896 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

13 Experts available now in Live!

Get 1:1 Help Now