How do you get keyboard handling and input from Windows API?

This is a discussion on How do you get keyboard handling and input from Windows API? within the Windows Programming forums, part of the Platform Specific Boards category; I've been wondering....Like I've tried the simple switch statement followed by cases with WM_KEYDOWN as the variable and in the ...

  1. #1
    Banned
    Join Date
    Jan 2011
    Posts
    5

    How do you get keyboard handling and input from Windows API?

    I've been wondering....Like I've tried the simple switch statement followed by cases with WM_KEYDOWN as the variable and in the cases I put each key like VK_LEFT or VK_RIGHT but they don't do any thing.

    Like for example I wanted a pop up message box and I tried the switch statement like this:

    switch(WM_KEYDOWN)
    {
    case VK_RIGHT:
    MessageBox(NULL, "Alert", "You pressed right", MB_OK); break;
    }

    ^^It compiles but the input does nada^^

  2. #2
    Password:
    Join Date
    Dec 2009
    Location
    NC
    Posts
    587
    It's a little more complicated than that. You have to register a window, and it's callback. When a key is pressed while your window has focus, it sends a WM_KEYDOWN to your callback, and you process it from there. Windows API can be a pain...

  3. #3
    Banned
    Join Date
    Jan 2011
    Posts
    5
    No, I know how to register a window and it's callback and such.

    After doing all that and using the switch statement following hwnd as the callback to the arguments in the window it still doesn't work.

    If you want the code I can give it to you.

  4. #4
    Password:
    Join Date
    Dec 2009
    Location
    NC
    Posts
    587
    The code would help.

    I also suggest you switch IDEs. I recommend Code Blocks. It's is still being actively developed. Dev-C++'s last release was 5 years ago.

  5. #5
    Banned
    Join Date
    Jan 2011
    Posts
    5
    #include <windows.h>

    /* Declare Windows procedure */
    LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);

    /* Make the class name into a global variable */
    char szClassName[ ] = "WindowsApp";

    int WINAPI WinMain (HINSTANCE hThisInstance,
    HINSTANCE hPrevInstance,
    LPSTR lpszArgument,
    int nFunsterStil)

    {
    HWND hwnd; /* This is the handle for our window */
    MSG messages; /* Here messages to the application are saved */
    WNDCLASSEX wincl; /* Data structure for the windowclass */

    /* The Window structure */
    wincl.hInstance = hThisInstance;
    wincl.lpszClassName = szClassName;
    wincl.lpfnWndProc = WindowProcedure; /* This function is called by windows */
    wincl.style = CS_DBLCLKS; /* Catch double-clicks */
    wincl.cbSize = sizeof (WNDCLASSEX);

    /* Use default icon and mouse-pointer */
    wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
    wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
    wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
    wincl.lpszMenuName = NULL; /* No menu */
    wincl.cbClsExtra = 0; /* No extra bytes after the window class */
    wincl.cbWndExtra = 0; /* structure or the window instance */
    /* Use Windows's default color as the background of the window */
    wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;

    /* Register the window class, and if it fails quit the program */
    if (!RegisterClassEx (&wincl))
    return 0;

    /* The class is registered, let's create the program*/
    hwnd = CreateWindowEx (
    0, /* Extended possibilites for variation */
    szClassName, /* Classname */
    "Windows App", /* Title Text */
    WS_OVERLAPPEDWINDOW, /* default window */
    CW_USEDEFAULT, /* Windows decides the position */
    CW_USEDEFAULT, /* where the window ends up on the screen */
    544, /* The programs width */
    375, /* and height in pixels */
    HWND_DESKTOP, /* The window is a child-window to desktop */
    NULL, /* No menu */
    hThisInstance, /* Program Instance handler */
    NULL /* No Window Creation data */
    );

    /* Make the window visible on the screen */
    ShowWindow (hwnd, nFunsterStil);

    /* Run the message loop. It will run until GetMessage() returns 0 */
    while (GetMessage (&messages, NULL, 0, 0))
    {
    /* Translate virtual-key messages into character messages */
    TranslateMessage(&messages);
    /* Send message to WindowProcedure */
    DispatchMessage(&messages);
    }

    /* The program return-value is 0 - The value that PostQuitMessage() gave */
    return messages.wParam;
    }


    /* This function is called by the Windows function DispatchMessage() */

    LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
    switch (message) /* handle the messages */
    {
    case WM_DESTROY:
    PostQuitMessage (0); /* send a WM_QUIT to the message queue */
    break;
    default: /* for messages that we don't deal with */
    return DefWindowProc (hwnd, message, wParam, lParam);
    }
    switch(hwnd)
    {
    case VK_RIGHT:
    MessageBox(NULL, "Alert", "You pressed right", MB_OK); break;
    case VK_LEFT:
    MessageBox(NULL, "Alert", "You pressed left", MB_OK); break;
    }

    return 0;
    }


    It doesn't work. What am I doing wrong?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Windows API functions and exceptions
    By IceDane in forum C++ Programming
    Replies: 9
    Last Post: 12-09-2009, 02:34 PM
  2. handling user input
    By rodrigorules in forum C++ Programming
    Replies: 4
    Last Post: 11-12-2009, 05:21 AM
  3. Console application - input handling
    By Something in forum C++ Programming
    Replies: 4
    Last Post: 06-09-2007, 02:19 PM
  4. Handling keyboard input in a dialog procedure.
    By ganonl in forum Windows Programming
    Replies: 2
    Last Post: 08-23-2003, 11:14 AM
  5. Handling input from cin
    By Zarkon in forum C++ Programming
    Replies: 1
    Last Post: 12-17-2001, 07:03 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21