Keyboard & TextBox WM_KEYDOWN

This is a discussion on Keyboard & TextBox WM_KEYDOWN within the Windows Programming forums, part of the Platform Specific Boards category; Hello Does anyone know how to properly associate key messages using the WM_KEYDOWN case in the message procedure? This is ...

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    67

    Question Keyboard & TextBox WM_KEYDOWN

    Hello

    Does anyone know how to properly associate key messages using the WM_KEYDOWN case in the message procedure?
    This is what I have so far (relevant code colored in green):

    Code:
    #include <windows.h>
    #define TextBoxDefined   101
    
    // Message Queue Commands to be defined later
    LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
    
    //WinMain() instead of main()
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
    {
     //Window class
        WNDCLASSEX wc;
        //Window class parameters
        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); //Executable's desktop icon
        wc.hCursor       = LoadCursor(NULL, IDC_ARROW); //Cursor
        wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1); //Window color
        wc.lpszMenuName  = NULL; //No menu
        wc.lpszClassName = "WindowClass"; //???
        wc.hIconSm       = LoadIcon(NULL, IDI_APPLICATION); //Window icon? , Sm = small?
    
        if(!RegisterClassEx(&wc)) //error handling
        {
            MessageBox(NULL, "Registration Failure", "Error", MB_OK); //Okay button
            return 0; //return fail
        }
    
        ////////////////////////////////////////////////////////////////////////////
    
     //The actual window
        HWND MyParentWindow;
        //The window's parameters
        MyParentWindow = CreateWindowEx(
            0, //Style
            "WindowClass",
            "Title", //Title
            WS_OVERLAPPEDWINDOW, //minimize, maximize, close
            0,//position x in pixels relative to screen
            0,//position y in pixels relative to screen
            500,//size x dimension in pixels
            500,//size y dimension in pixels
            NULL, //MyParentWindow is the parent window and it does not have a parent, thus NULL
            NULL, //no menu was defined
            hInstance, //???
            NULL); //???
    
        if(MyParentWindow == NULL) //error handling
        {
            MessageBox(NULL, "MyParentWindow Creation Failure", "Error", MB_OK); //okay button
            return 0;
        }
    
        ShowWindow(MyParentWindow, nCmdShow); //show the window called "MyParentWindow"
        UpdateWindow(MyParentWindow); //update the window called "MyParentWindow"
    
        ////////////////////////////////////////////////////////////////////////////
    
     // Message loop
        MSG Msg;
        //GetMessage() gets a message from message queue.
        while(GetMessage(&Msg, NULL, 0, 0) > 0) //while there are more than 0 messages?
        {
            TranslateMessage(&Msg); //???
            DispatchMessage(&Msg); //???
        }
        return Msg.wParam; //return "wParam" of Msg???
    }
    
    //Messages to be interpreted
    LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
    {
        switch(msg) // if&else commands to follow when interpreting the current message in queue
        {
            case WM_CREATE:
            {
                HWND TextBox;
                TextBox = CreateWindowEx(WS_EX_CLIENTEDGE,
                                       "EDIT",
                                       "Predefined Text",
                                       WS_CHILD | WS_VISIBLE | WS_VSCROLL | WS_HSCROLL | ES_MULTILINE | ES_AUTOVSCROLL | ES_AUTOHSCROLL,
                                       0,
                                       0,
                                       0,
                                       0,
                                       hwnd,
                                       (HMENU)TextBoxDefined,
                                       GetModuleHandle(NULL),
                                       NULL);
                if(TextBox == NULL)
                    MessageBox(hwnd, "Text Box creation failed", "Error", MB_OK | MB_ICONERROR);
    
                HFONT hfDefault;
                hfDefault = (HFONT)GetStockObject(DEFAULT_GUI_FONT);
    
                SendMessage(TextBox, WM_SETFONT, (WPARAM)hfDefault, MAKELPARAM(FALSE,0));
            }
            break;
            
            case WM_SIZE:
            {
                HWND TextBox;
                TextBox = GetDlgItem(hwnd, TextBoxDefined);
                
                RECT FullWindow;
                GetClientRect(hwnd, &FullWindow);
                SetWindowPos(TextBox, //"TextBox" is being positioned
                             NULL,
                             0,
                             0,
                             FullWindow.right,
                             FullWindow.bottom,
                             SWP_NOZORDER);
            }
            break;
    
            case WM_KEYDOWN:
                switch (wParam)
                {
                    case VK_CONTROL: //if control is pressed
                    {
                        switch(wParam)
                        {
                            case VK_DELETE: //if delete is pressed
                            {
                                //if CTRL DEL are pressed
                                SetDlgItemText(hwnd, TextBoxDefined, "Blah Blah Blah"); //write BlahBlahBlah
                                return 0;
                            }
                            break;
                        }
                        return 0;
                    }
                    break;
                }
                return 0;
            break;
            
            case WM_CLOSE:
                DestroyWindow(hwnd);
            break;
            case WM_DESTROY:
                PostQuitMessage(0);
            break;
            default:
                return DefWindowProc(hwnd, msg, wParam, lParam);
        }
        return 0;
    }
    I am using Dev C++ and the code should compile as is.
    It opens up a window with a textbox. The textbox has predefined text.
    I was wondering how I could either retrieve the text currently in the textbox, or set new text onto it after the user pressed something like CTRL+DEL.
    I tried a WM_COMMAND (for a menu bar) and it successfully set the text. It also switched on the wParam, which I believe is the correct Param for WM_KEYDOWN too.
    The code compiles and yet it doesn't do what I want, which is weird. I'm sure it's incomplete somehow.

    Any leads would help

    Anything with concrete examples would be greatly appreciated.
    Thanks in advance!

  2. #2
    Registered User
    Join Date
    Jan 2009
    Location
    Australia
    Posts
    375
    Your delete case will never be reached because wParam cannot be equal to VK_CONTROL and VK_DELETE at the same time. I suggest a flag variable which you can set when control is pressed, and then when the delete key is pressed you can check to see if the flag is set.

    This also entails processing the WM_KEYUP message (to unset the flag variable).

  3. #3
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    1,625
    Use LOWORD and HIWORD with wParam
    Devoted my life to programming...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 03-02-2009, 08:33 AM
  2. Detecting keyboard and mouse in linux
    By sameer.nalwade in forum C Programming
    Replies: 3
    Last Post: 11-22-2008, 04:24 AM
  3. Keyboard port using other that a keyboard
    By antoinelac in forum C++ Programming
    Replies: 4
    Last Post: 06-12-2008, 03:46 PM
  4. Virtual keys
    By Arkanos in forum Windows Programming
    Replies: 4
    Last Post: 12-12-2005, 10:00 AM
  5. Game Design Topic #2 - Keyboard or Mouse?
    By TechWins in forum Game Programming
    Replies: 4
    Last Post: 10-08-2002, 04:34 PM

Tags for this Thread


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