[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Get keypresses

Posted on 1998-09-14
17
Medium Priority
?
211 Views
Last Modified: 2010-04-06
I would like to know what I need to do in a program to be able to find out which key was pressed, not just i that program, but it should be able to log the keystrokes generally from windows. I probably need to trap some Win32 calls, but how do I do that ???

Thanks in advance
0
Comment
Question by:carceri
[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
  • 8
  • 4
  • 4
  • +1
17 Comments
 
LVL 5

Expert Comment

by:JimBob091197
ID: 1339676
Hi

You need to use the SetWindowsHookEx API call.  For the hook to work for all apps it must be in a DLL.  I have written a sample of a system-wide key hook.  I can e-mail it to you if you provide your e-mail address.

Regards,
JB
0
 

Author Comment

by:carceri
ID: 1339677
Yes please, my e-mail address is michael@casproctr.com
0
 
LVL 10

Expert Comment

by:viktornet
ID: 1339678
Hello all!

JimBob, would you mind sending it to me too???
If you decided to send it my e-mail is viktor@megsinet.net

ThanQ
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 5

Expert Comment

by:JimBob091197
ID: 1339679
Hi Carceri & Viktor.

I have e-mailed the sample to both of you.

Cheers,
JB
0
 
LVL 10

Expert Comment

by:viktornet
ID: 1339680
Thanks a lot! I'll take a look at it immediately!

Regards,
Viktor Ivanov
0
 
LVL 10

Expert Comment

by:viktornet
ID: 1339681
Hello Jim :-)

It works perfectly. Can you just tell me what does this line of code means....???
You've defined a type as follows...

type
  TSetHook = procedure;
{...}
begin
{...}
TSetHook(pProc)(hKeyHook);//not sure for the names but it looked like this...
{...}
Thanks!

Regards,
Viktor Ivanov
0
 
LVL 5

Expert Comment

by:JimBob091197
ID: 1339682
Hi Viktor,

pProc is a pointer to function in the DLL.  I get pProc from:
    pProc := GetProcAddress(hLib, 'SetHook');

I need to call that function, so I need to cast the pointer to a function.  I declare the function type with:
    type
      TSetHook = function (AHook: Integer): Integer;

Then I can type-cast the pointer to the function type:
    TSetHook(pProc)

hKeyHook is the parameter I pass into the function:
    TSetHook(pProc)(hKeyHook);

This is like calling "SomeFunction(hKeyHook)" except that "SomeFunction" is "TSetHook(pProc)".

Am I making sense???  :-)
JB
0
 
LVL 10

Expert Comment

by:viktornet
ID: 1339683
Oh, so now I got it! Thanks for clearing things up ;-)

Regards,
Viktor Ivanov
0
 
LVL 5

Expert Comment

by:JimBob091197
ID: 1339684
;-)
0
 
LVL 1

Expert Comment

by:BlackDeath
ID: 1339685
Hi, JimBob.

this looks quite cool.
i'd like to have that sample too.
would you be so kind as to send it to me?
andreas.naguschewski@vt.siemens.de

tia,

Black Death

0
 
LVL 5

Expert Comment

by:JimBob091197
ID: 1339686
Hi Black Death

I have e-mailed it to you too.

Cheers,
JB
0
 
LVL 5

Expert Comment

by:JimBob091197
ID: 1339687
Hi Viktor & Carceri

Black Death has been investigating the duplicate key stroke messages and has the following replacement for the KeyHookProc in the DLL:

function KeyHookProc(Code: Integer; wParam: WParam; lParam: LParam): Integer; stdcall;
type
  TShowProc = procedure;
var
  f: TextFile;
begin
  // not if hKeyHook is above 0 but -
  // if Code is less than zero, one has to pass the msg to CallNextHookEx without
  // further processing and should return the value returned
  // if HC_NOREMOVE, an app has posted PeekMessage with PM_NOREMOVE !
  // win32.hlp - when testing this, key appeared on screen when typed in word, but
  // are being protocolled just once. i had no further testing, but my feeling is that this
  // must be it.
  if (Code < 0) or (Code = HC_NOREMOVE) then begin
    Result := CallNextHookEx(hKeyHook, Code, wParam, lParam);
    Exit;
  end;
  Result := 0;
  case wParam of
    Ord('A')..Ord('Z'), Ord('a')..Ord('z'):
      begin
        AssignFile(f, 'C:\Test.log');
        if (FileExists('C:\Test.log')) then
          Append(f)
        else
          Rewrite(f);
//        Write(f, Chr(wParam) + ',' + IntToStr(lParam) + ' ');
        Write(f, Chr(wParam));
        CloseFile(f);
      end;
  end;
end;

Thanks Black.

Cheers,
JB

0
 

Author Comment

by:carceri
ID: 1339688
Whoa, a lot of comments :-)

Thanks JimBob !
0
 
LVL 5

Expert Comment

by:JimBob091197
ID: 1339689
You're welcome.  Does it help you?

JB
0
 

Author Comment

by:carceri
ID: 1339690
Oh yes, that's just what I needed !
0
 
LVL 5

Accepted Solution

by:
JimBob091197 earned 400 total points
ID: 1339691
Good, then I'll post this as an answer.

Thanks again to Black Death.

Cheers,
JB
0
 

Author Comment

by:carceri
ID: 1339692
Thanks !
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Suggested Courses

650 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