In CApplView I create a Login Form.

void CApplView::Login_Entry(CDC *pDC) {
  ..
  if (pUserid == NULL) {
    pUserid = new CEdit;
    pUserid->Create(WS_CHILD|WS_VISIBLE|WS_BORDER, CRect(x,y1,w,h), this, IDC_USER);
    pPasswd = new CEdit;
    pPasswd->Create(WS_CHILD|WS_VISIBLE|WS_BORDER, CRect(x,y2,w,h), this, IDC_PASS);
    pbLogin = new CButton(); pbLogin->Create("Login",WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON, CRect(x,y3,w,h), this, IDB_LOGIN);
  }
}

How do I add code to set the focus on pPasswd when TAB or RETN is entered in pUserid?
Something like...

OnKeyDown (in pUserid)
  if ((char == VK_RETURN) || (char == VK_TAB)) {
    pPasswd->SetFocus();
  }

I cannot figure out how to incorporate the event in the MESSAGE MAP.

Dani AI

Generated

A couple of practical ways to solve this for (and answering about VC6): OnKeyDown in the view will not fire while the edit has focus. Two simple options:

  • Subclass the edit (derive from CEdit) and handle WM_KEYDOWN there — clean and local to the control.
  • Avoid a new class by overriding CView::PreTranslateMessage in your view and intercepting the keystrokes sent to the pUserid control. This is quick for one-off focus changes.

Example (PreTranslateMessage approach):

BOOL CApplView::PreTranslateMessage(MSG* pMsg)
{
    if (pMsg->message == WM_KEYDOWN &&
        (pMsg->wParam == VK_RETURN || pMsg->wParam == VK_TAB))
    {
        if (pUserid && ::IsWindow(pUserid->m_hWnd) && pMsg->hwnd == pUserid->m_hWnd)
        {
            pPasswd->SetFocus();
            return TRUE; // consume key so default handling doesn't run
        }
    }
    return CView::PreTranslateMessage(pMsg);
}

Notes and troubleshooting: make sure your edits are created with WS_TABSTOP if you want normal Tab navigation instead of manual handling. If VK_RETURN seems not to arrive in WM_KEYDOWN, check WM_CHAR as well. Always verify pointers and window handles (::IsWindow) before calling SetFocus, and call the base PreTranslateMessage for unhandled messages. If you actually want Tab to become a character inside the edit, subclass and override WM_GETDLGCODE to return DLGC_WANTTAB.

Recommended Answers

All 2 Replies

You want to add a "Event Handler" for the event of pressing a Tab key or Return key?
What Compiler of VC are you using?

You want to add a "Event Handler" for the event of pressing a Tab key or Return key?

Not exactly. The event handler would be similar to that second bit of code I include above.

I can include, in my MESSAGE MAP,
ON_WM_KEYDOWN()

and write a handler function..
void CApplView::OnKeyDown (UINT nChar, UINT nRepCnt, UINT nFlags) {...}

That will work to capture a key stroke when the main window has focus. I want it to capture the key stroke when pUserid has the focus. I know I can do this by creating a new class derived from CEdit, but I thought there must be a simpler way.

What Compiler of VC are you using?

I'm using Visual C++ 6.0

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.