Thread: fstream file I/O, getting keystrokes

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    6

    fstream file I/O, getting keystrokes

    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);
    		}
    	}
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Although there are valid and legal applications for keyloggers, the forum guidelines clearly state:

    6. Messages relating to cracking, (erroneously called "hacking" by many), copyright violations, or other illegal activities will be deleted. This includes all messages reguarding to keyloggers.

    As such, I am closing this thread.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  2. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM