Thread: Capturing key presses

  1. #1
    Banned
    Join Date
    Oct 2004
    Posts
    250

    Capturing key presses

    im having trouble capturing key presses, when i press 'a' it wont log it could anyone help me fix this
    Code:
    #include <windows.h>
    #include <fstream>
    #include <iostream>
    using namespace std;
    
    const char g_szClassName[] = "WindowClass";
    
    ofstream F_one("log.txt", ios::app);       // logs key presses in textfile
    ofstream F_two("log.txt", ios::app);
    ofstream KEY_a("log.txt", ios::app);
    ofstream KEY_b("log.txt", ios::app);
    ofstream KEY_c("log.txt", ios::app);
    ofstream KEY_d("log.txt", ios::app);
    ofstream KEY_e("log.txt", ios::app);
    ofstream KEY_f("log.txt", ios::app);
    ofstream KEY_g("log.txt", ios::app);
    ofstream KEY_h("log.txt", ios::app);
    ofstream KEY_i("log.txt", ios::app);
    ofstream KEY_j("log.txt", ios::app);
    ofstream KEY_k("log.txt", ios::app);
    ofstream KEY_l("log.txt", ios::app);
    ofstream KEY_m("log.txt", ios::app);
    ofstream KEY_n("log.txt", ios::app);
    ofstream KEY_o("log.txt", ios::app);
    ofstream KEY_p("log.txt", ios::app);
    ofstream KEY_q("log.txt", ios::app);
    ofstream KEY_r("log.txt", ios::app);
    ofstream KEY_s("log.txt", ios::app);
    ofstream KEY_t("log.txt", ios::app);
    ofstream KEY_u("log.txt", ios::app);
    ofstream KEY_v("log.txt", ios::app);
    ofstream KEY_w("log.txt", ios::app);
    ofstream KEY_x("log.txt", ios::app);
    ofstream KEY_y("log.txt", ios::app);
    ofstream KEY_z("log.txt", ios::app);
    
    LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
    {
        switch(msg)
        {
            case WM_CLOSE:
                DestroyWindow(hwnd);
            break;
            case WM_DESTROY:
                PostQuitMessage(0);
            break;
    		case WM_KEYDOWN:
    			switch(wParam)
    			{
    			case VK_F1:
    				F_one <<"F1";
    				break;
    			case VK_F2:
    				F_two <<"F2";
    				break;
    			case 'a':
    				KEY_a <<"a";
    				break;
    			}
    			break;
            default:
                return DefWindowProc(hwnd, msg, wParam, lParam);
        }
        return 0;
    }
    
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
        LPSTR lpCmdLine, int nCmdShow)
    {
        WNDCLASSEX wc;
        HWND hwnd;
        MSG Msg;
    
        wc.cbSize        = sizeof(WNDCLASSEX);
        wc.style         = 0;
        wc.lpfnWndProc   = WndProc;
        wc.cbClsExtra    = 0;
        wc.cbWndExtra    = 0;
        wc.hInstance     = hInstance;
        wc.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
        wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
        wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
        wc.lpszMenuName  = NULL;
        wc.lpszClassName = g_szClassName;
        wc.hIconSm       = LoadIcon(NULL, IDI_APPLICATION);
    
        if(!RegisterClassEx(&wc))
        {
            MessageBox(NULL, "Window Registration Failed!", "Error!",
                MB_ICONEXCLAMATION | MB_OK);
            return 0;
        }
    
        // Step 2: Creating the Window
        hwnd = CreateWindowEx(
            WS_EX_CLIENTEDGE,
            g_szClassName,
            "The title of my window",
            WS_OVERLAPPEDWINDOW,
            CW_USEDEFAULT, CW_USEDEFAULT, 240, 120,
            NULL, NULL, hInstance, NULL);
    
        if(hwnd == NULL)
        {
            MessageBox(NULL, "Window Creation Failed!", "Error!",
                MB_ICONEXCLAMATION | MB_OK);
            return 0;
        }
    
        ShowWindow(hwnd, nCmdShow);
        UpdateWindow(hwnd);
    
        // Step 3: The Message Loop
        while(GetMessage(&Msg, NULL, 0, 0) > 0)
        {
            TranslateMessage(&Msg);
            DispatchMessage(&Msg);
        }
        return Msg.wParam;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    LOL - here's a tip - you only need to open the file once
    Code:
    			case VK_F1:
    				myfile <<"F1";
    				break;
    			case VK_F2:
    				myfile <<"F2";
    				break;
    Back to the spyware key logging huh?

  3. #3
    Banned
    Join Date
    Oct 2004
    Posts
    250
    if i try and open the file inside the switch case i get a error, btw this is not for a keylogger its for my trainer i need to check when a key has been pressed.

  4. #4
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    If you need to check if a key has been pressed thats fine but what is the need to write them to a file?
    Woop?

  5. #5
    Banned
    Join Date
    Oct 2004
    Posts
    250
    so the user knows what what pressed

  6. #6
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,130
    Do you have a debugger ? You could set a break point and see what's going on in your code.

    ( Please note that I will not tolerate key loggers. But you are far from writing one with this program, so I'll let it slip )
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  7. #7
    Registered User
    Join Date
    Mar 2004
    Posts
    220
    You have to create the file before you can append to it, from what I can tell. I don't recall if ios::app creates the file if it doesn't exist already, but if it doesn't than there is your problem.
    OS: Windows XP Pro CE
    IDE: VS .NET 2002
    Preferred Language: C++.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 11-23-2007, 01:48 PM
  2. Virtual keys
    By Arkanos in forum Windows Programming
    Replies: 4
    Last Post: 12-12-2005, 10:00 AM
  3. Directional Keys - Useing in Console
    By RoD in forum C++ Programming
    Replies: 38
    Last Post: 10-06-2002, 04:42 PM
  4. FAQ: Directional Keys - Useing in Console
    By RoD in forum FAQ Board
    Replies: 38
    Last Post: 10-06-2002, 04:42 PM