WM_KEYDOWN from a editbox? Get the 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.

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?
When you press a key and release it you get: WM_KEYDOWN, WM_CHAR and WM_KEYUP. You need to check out the WM_CHAR.
I tested that and WM_CHAR works as long you dont select/sets the focus to the EDIT-box, like WM_KEYDOWN/UP :(

1
2
3
4
5
6
7
8
CreateWindowEx(0,
"EDIT",
"PRESS ANY KEY",
ES_READONLY | ES_MULTILINE | ES_CENTER | WS_CHILD | WS_VISIBLE,
91,59 ,149, 15, hwnd, (HMENU)IDC_KEY, GetModuleHandle(NULL), NULL);

case WM_CHAR:
            MessageBox(hwnd,"TEST", "TEST", MB_OK | MB_ICONINFORMATION);
The windows proceedure for controls is provided by Windows itself and each control does it's own thing with the messages and pass notification codes specific to that control to the parent window.

You cannot directly get the WM_XXXX messages as you are hoping to do - wihout

1. Possibly creating a keyboard hook

OR

2. Subclassing the control

Thank you very much =) It works like a charm now.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
LRESULT CALLBACK newEditProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) {
switch (message) {
	case WM_KEYDOWN:
        MessageBox(hwnd,"KEY", "KEY", MB_OK | MB_ICONINFORMATION);
        return 0;
    case WM_LBUTTONDOWN:
    case WM_MBUTTONDOWN:
    case WM_XBUTTONDOWN:
    case WM_RBUTTONDOWN:
        SendMessage(HWND_KEY,WM_SETTEXT,0,(LPARAM)"GIVEFOCUS!!!");
        SetFocus(HWND_KEY);
        return 0;
	default:
		if (oldEditProcedure) {
			return CallWindowProc (oldEditProcedure, hwnd, message, wParam, lParam);
		} else {
		return DefWindowProc (hwnd, message, wParam, lParam);
		}
	}
}


I have one more question about keys if you're up to it, maybe not so much keys as hotkeys...

Is there any way of controlling the down and up event on a hotkey form the RegisterHotKey()?
Or how would you do it?
Err... if the goal with all those WM_XBUTTOMDOWN messages is to know when the control has received focus, you're better off with WM_SETFOCUS.

Not only is that exactly what WM_SETFOCUS is for, but it also works for things besides mouse clicks (like tabbing to the control, for example)
If you want to handle key presses when focus is in an edit control then add this to your message loop:
(I'm assuming that you named your MSG uMsg, and your window handle hWnd)

if (uMsg.message == WM_KEYDOWN) SendMessage (hWnd, WM_KEYDOWN, uMsg.wParam, uMsg.lParam);

Then just used the WM_KEYDOWN case statement in your window procedure function to do what you want to when a key is pressed.
Last edited on
Do you get the WM_KEYDOWN message in your main window if a child editbox have the keyboard focus??
Not usually, but with the code I posted above you do.
some random dude, thx for the info, it works too...
But how do I identify that it is the editform that sends the wm_keydown? I probably can use getfocus(), but isn't that little bit ugly to use there?

Do use the line before translate or after?

1
2
3
4
5
6
7
    while (GetMessage (&messages, NULL, 0, 0)) {
        TranslateMessage(&messages);
        if (messages.message == WM_KEYDOWN) {
            SendMessage (hwnd, WM_KEYDOWN, messages.wParam, messages.lParam);
        }
        DispatchMessage(&messages);
    }


Disch, no, I need to get the key from the mouse too :) but I probably did it wrong there, it was just for testing that msg


Any good advice for the hotkey button question?
closed account (3pj6b7Xj)
Actually you may need to do something weird I did with my Squibbles game. When the window is dragged the game continues to run while the window is moved around. I acomplished this by inserting a InvalidateRect() just before returning 0 in the window procedure...be warned, this keeps the message pump running in the background even when an edit box or any other control is active......you can capture WM_KEYDOWN this way I believe and act on it.

Try it it might work?
try
1
2
3
4
if (message.message == WM_KEYDONW)
{
       if (GetDlgItem (hWnd, EDIT_CONTROL_ID) == GetFocus ()) SendMessage (hWnd, WM_KEYDOWN, message.wParam, message.lParam);
}
Last edited on
some random dude, thx, I verify the editform in my WindowProcedure now, if I do it in the GetMessage () loop I still need to verify it later on in the WindowProcedure =)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
    while (GetMessage (&messages, NULL, 0, 0)) {
        TranslateMessage(&messages);
        if (messages.message == WM_KEYDOWN) {
            SendMessage (hwnd, WM_KEYDOWN, messages.wParam, messages.lParam);
        }
        DispatchMessage(&messages);
    }




case WM_KEYDOWN:
            if(GetDlgCtrlID(GetFocus())==IDC_KEY) {
                SendMessage(HWND_KEY,WM_SETTEXT,0,(LPARAM)"test");
                return 0;
            } else {
                return DefWindowProc (hwnd, message, wParam, lParam);
            }


mrfaosfx, I will try that! But I'm afraid that it maybe will drain cpu-power?
But Il try it anyway =)
Last edited on
closed account (3pj6b7Xj)
right about the cpu power but if you control it the right way you should have no problems i believe. of course, there are still other ways, there is always a way :)
Topic archived. No new replies allowed.