WM_KEYDOWN from a editbox? Get the VK_key?

This is a discussion on WM_KEYDOWN from a editbox? Get the VK_key? within the Windows Programming forums, part of the Platform Specific Boards category; Hi I am trying to get the specific key that's pressed when a editbox have the focus or gets the ...

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    1

    Question WM_KEYDOWN from a editbox? Get the VK_key?

    Hi

    I am trying to get the specific key that's pressed when a editbox have
    the focus or gets the focus.
    I tried the WM_KEYDOWN/UP, but that isn't sent when a editbox have the
    focus...

    Why I am trying to get the key is because I want to display the key
    (VK_) in the editbox even if its mouse key, alt, ctrl and so on.
    But only when the user select the box.

    I Googled for some help but couldn't find anything more than setting
    up a hook(?) and use low level keyhook(?), but that's probably to
    complicated for me so I ask here, is there a easier / better way to do
    this?

  2. #2
    Registered User
    Join Date
    Jun 2008
    Posts
    54
    Quote Originally Posted by zypronix View Post
    Hi,

    I'm trying to get the key presses sent to an edit control. I tried getting them through the WM_KEYDOWN/UP messages, but those messages aren't sent to the windows procedure callback I expected them to be sent to.

    I'm trying to intercept the key presses because I want to print their "VK_" identifiers in the edit control, but only when the user has selected the box.

    I Googled for help, but couldn't find anything aside from something about hooks, which I couldn't understand. Is there a better, easier way to do what I'm attempting to do without using hooks?
    One alternative to using hooks would be to superclass the "edit" control wndclass, like this:
    Code:
    WNDCLASS wndclass;
    WNDPROC OldWndProc;
    HINSTANCE hInst;
    
    /* ... */
    
    hInst = GetModuleHandle(0);
    GetClassInfo(hInst, TEXT("edit"), &wndclass);
    OldWndProc = wndclass.lpfnWndProc;
    wndclass.lpfnWndProc = MyEditBoxProc;
    wndclass.lpszClassName = TEXT("myedit");
    wndclass.hInstance = hInst;
    RegisterClass(&wndclass);
    GetClassInfo() fills the supplied WNDCLASS structure with the edit control's default stuff, including its pointer to its default window procedure. You're going to switch its default window procedure function out for your own, but before you do you save the pointer to the default function so you can still call it to handle all the other stuff you expect an edit control to magically handle for you. You also give your superclass a new name and assign it some other stuff, then register it just like any other WNDCLASS structure.

    Code:
    LRESULT CALLBACK MyEditBoxProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
    {
    
    	switch (msg)
    	{
             case WM_KEYDOWN:
            /* Catch and process keypresses here */
                    return 0;
    	default:
    		break;
    	}
    
    	return CallWindowProc(OldWndProc, hwnd, msg, wparam, lparam);
    }
    To create an edit control which uses MyEditBoxProc, you use TEXT("myedit") for the lpClassName in your CreateWindow call instead of TEXT("edit").
    Last edited by Boxknife; 02-04-2011 at 02:28 AM.

  3. #3
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,855
    Try looking at WM_KILLFOCUS msgs (where the wParam == edit controls HWND showing the edit just got focus).
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by novacain View Post
    Try looking at WM_KILLFOCUS msgs (where the wParam == edit controls HWND showing the edit just got focus).
    He's going to have to superclass the window as described... You can't get at the keyboard except right inside the window with focus.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Transparent, Editbox, HOLLOW_BRUSH, Marking text?
    By Dampy in forum Windows Programming
    Replies: 6
    Last Post: 09-22-2008, 07:17 PM
  2. Replies: 2
    Last Post: 08-25-2005, 03:09 PM
  3. Create two buttons that interact with an editbox
    By Arkanos in forum C++ Programming
    Replies: 4
    Last Post: 07-11-2005, 01:17 AM
  4. editbox problem
    By tyouk in forum Windows Programming
    Replies: 7
    Last Post: 10-05-2004, 09:14 PM
  5. Editbox problems
    By Devil Panther in forum Windows Programming
    Replies: 9
    Last Post: 07-27-2003, 04:12 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