Handling arrow key in cedit control of mfc

899 Views Asked by At

I'm making matlab-like command window using cedit control of mfc.

For example, after I input several commands, I want to display old command using an arrow key (specifically the up key).

I succeeded in displaying old commands, but failed to locate the cursor on the end of this command. The reason seems that the arrow key was input one more time after I locate the cursor in the end of this command.

Here is detailed situation.

  • First I input command 'play'
  • and then Play!.. message pops up.
  • and in the next command prompt I hit '↑' key
  • and I succeded the old command 'play' streamed automatically,

However, my cursor go up to upper line.

@ play

Play!.. | (← cursor located here..)

@ play| (← I want to locate the cursor here, after hitting '↑' key)

This is my code:

class CEditCommand::CEdit
{
public:
    virtual BOOL PreTranslateMessage(MSG* pMsg);
}

BOOL CEditCommand::PreTranslateMessage(MSG* pMsg)
{
    if (pMsg->message == WM_KEYDOWN)
    {
        if (pMsg->wParam == VK_UP)
        {
            int nS = 0; nE =0;
            GetSel(nS, nE);
            int nLineIndex = LineIndex();
            CString str = m_CommandHistory[m_nCommandIndex];
            m_nCommandIndex--;
            SetSel(nLineIndex, nE);
            ReplaceSel(str);
            SetSel(0, -1);
            SetSel(-1, -1);
        }
    }
}

I don't know why '↑' key would be pushed again after executing PreTranslateMessage. Does anyone have an idea about this?

1

There are 1 best solutions below

1
On

Your edit control will still get the up-arrow message, so you will need to return TRUE in CEditCommand::PreTranslateMessage() for both WM_KEYDOWN and WM_KEYUP when pMsg->wParam == VK_UP