0

I need to record keystrokes (if user pressed 'ABCS', I wand that saved in a string) if the user switches to a certain window which is name is AAA, the windows is not of the current application, but can be of any other one.

I guess I know how to get the current window name, its something like that:

#include <windows.h>

// ...

TCHAR buf[255];
GetWindowText(GetForegroundWindow(), buf, 255);

But how to record the keys, I have no idea.

Thanks,

snoofkin
  • 8,725
  • 14
  • 49
  • 86
  • I've often wondered if we should close questions like this because they seem to be nefarious. – John Dibling Nov 12 '10 at 18:50
  • 18
    @John: You're joking right? We're here to help people solve programming problems, not judge peoples intentions. – Benjamin Lindley Nov 12 '10 at 18:53
  • 3
    it has a good purpose, not all keyloggers have to be malicious – snoofkin Nov 12 '10 at 18:53
  • 4
    @PigBen: It was just a vovalization of what I was thinking. I'm sure others have thought it as well. I didnt vote to close. – John Dibling Nov 12 '10 at 18:55
  • You can check the answers in [this thread](http://stackoverflow.com/q/3672533/238902 "c++ get other windows messages") as well – default Nov 13 '10 at 11:22
  • @soulsurfers - "not all keyloggers are malicious"? Really? I'd like to hear of one. – Tim Mar 14 '11 at 14:11
  • @Tim okay, I made a keylogger to record the most used words when typing a string of characters to create a auto-correct for my desktop. – Pookie Sep 01 '16 at 06:19

3 Answers3

4

Probably the easiest way is to use the AttachThreadInput function.

Use GetWindowThreadProcessId to get a thread id from a window handle.

Nick Dandoulakis
  • 42,588
  • 16
  • 104
  • 136
2

Check out this sample! http://www.planetsourcecode.com/vb/scripts/ShowCode.asp?txtCodeId=11920&lngWId=3

Iraklis
  • 810
  • 5
  • 14
0

This may not be what you are looking for, but just in case you are trying to create "hotkeys", Win32 has direct support for it - see RegisterHotkey

And there is SetWindowsHookEx

Vatsan
  • 1,163
  • 9
  • 15