We help IT Professionals succeed at work.

Edit control problem !

mike_marquet
mike_marquet asked
on
Hi,

I have written a control for keyboard configuration (like the one in visual studio - an edit control). It works fine but have 3 troubles :

- I can not catch the tab key. I never receive the WM_KEYDOWN message when I press it.

- How catch the ESC & ENTER keys so that the parent dialog of the control doesn't destroy.

- When I press a key, the WM_KEYDOWN is generated but I have also a BEEP. How to disable this beep sound.

Thanks a lot for answer.
Mike

P.S. : The control is derived from a CWnd.

Source code :

#include "stdafx.h"
#include "KeyAssignatorEdit.h"

BOOL CKeyAssignatorEdit::m_bRegistered = CKeyAssignatorEdit::Register();

LRESULT CALLBACK CKeyAssignatorEditProc(HWND hWnd, UINT uiMsg, WPARAM wParam, LPARAM lParam)
 {
  switch(uiMsg)
   {
    case WM_NCCREATE :
                       {
                        CKeyAssignatorEdit *pKeyAssignatorEdit = new CKeyAssignatorEdit;
                        ASSERT(pKeyAssignatorEdit);
                        BOOL rcCode = pKeyAssignatorEdit->SubclassWindow(hWnd);
                        ASSERT(rcCode);
                        return rcCode;
                       }
   }

  return ::DefWindowProc(hWnd, uiMsg, wParam, lParam);
 }

/////////////////////////////////////////////////////////////////////////////
// CKeyAssignatorEdit : Implementation

CKeyAssignatorEdit::CKeyAssignatorEdit()
 {
  m_bRegistered = FALSE;
 }

CKeyAssignatorEdit::~CKeyAssignatorEdit()
 {
 }

void CKeyAssignatorEdit::Transcode(UINT nChar)
 {
  BOOL bCtrl  = (GetKeyState(VK_CONTROL) >> ((sizeof(SHORT) * 8) - 1)) & 0x1;
  BOOL bAlt   = (GetKeyState(VK_MENU   ) >> ((sizeof(SHORT) * 8) - 1)) & 0x1;
  BOOL bShift = (GetKeyState(VK_SHIFT  ) >> ((sizeof(SHORT) * 8) - 1)) & 0x1;

  TRACE("(CKeyAssignatorEdit) Transcode(nChar = %u) : bCtrl = %d , bAlt = %d , bShift = %d\n", nChar, bCtrl, bAlt, bShift);

  CString strText;
  CString str;

  if (bCtrl) strText += "Ctrl+";

  if (bAlt) strText += "Alt+";

  if (bShift) strText += "Shift+";

 
  if (nChar == VK_ESCAPE) strText += "Esc"; // NEVER RECEIVED BECAUSE PARENT IS DESTROYED

  else if (nChar == VK_SCROLL) strText += "Scroll Lock";

  else if (nChar == VK_PAUSE) strText += "Pause";

  else if (nChar == VK_TAB) strText += "Tab"; // IS NEVER RECEIVED

  else if (nChar == VK_LWIN) strText += "Left Windows";

  else if (nChar == VK_RWIN) strText += "Right Windows";

  else if (nChar == VK_APPS) strText += "Applications";

  else if (nChar == VK_RETURN) strText += "Enter"; // RECEIVED BUT PARENT IS DIRECTLY DESTROYED

  else if (nChar == VK_SPACE) strText += "Space";

  else if (nChar == VK_INSERT) strText += "Ins";

  else if (nChar == VK_DELETE) strText += "Del";

  else if (nChar == VK_HOME) strText += "Home";

  else if (nChar == VK_END) strText += "End";

  else if (nChar == VK_PRIOR) strText += "Page Up";

  else if (nChar == VK_NEXT) strText += "Page Down";

  else if (nChar == VK_UP) strText += "Up Arrow";

  else if (nChar == VK_DOWN) strText += "Down Arrow";

  else if (nChar == VK_LEFT) strText += "Left Arrow";

  else if (nChar == VK_RIGHT) strText += "Right Arrow";

  else if (nChar == VK_NUMLOCK) strText += "Num Lock";

  else if (nChar == VK_DIVIDE) strText += "Num /";

  else if (nChar == VK_MULTIPLY) strText += "Num *";

  else if (nChar == VK_SUBTRACT) strText += "Num -";

  else if (nChar == VK_ADD) strText += "Num +";

  else if (nChar == VK_DECIMAL) strText += "Del";

  else if (nChar == VK_NUMPAD0) strText += "Ins";

  else if (nChar == VK_NUMPAD1) strText += "End";

  else if (nChar == VK_NUMPAD2) strText += "Down Arrow";

  else if (nChar == VK_NUMPAD3) strText += "Page Down";

  else if (nChar == VK_NUMPAD4) strText += "Left Arrow";

  else if (nChar == VK_NUMPAD5) strText += "NumPad Clear";

  else if (nChar == VK_NUMPAD6) strText += "Right Arrow";

  else if (nChar == VK_NUMPAD7) strText += "Home";

  else if (nChar == VK_NUMPAD8) strText += "Up Arrow";

  else if (nChar == VK_NUMPAD9) strText += "Page Up";

  else if (nChar >= VK_F1 && nChar <= VK_F12)
   {
    str.Format("F%d", nChar - VK_F1 + 1);
   
    strText += str;
   }

  else if (nChar >= 'A' && nChar <= 'Z') strText += nChar;

 
  SetWindowText(strText);

  Invalidate();
 }

BOOL CKeyAssignatorEdit::Register()
 {
  WNDCLASS stWC;

  stWC.style         = CS_GLOBALCLASS | CS_HREDRAW | CS_VREDRAW;
  stWC.lpfnWndProc   = CKeyAssignatorEditProc;
  stWC.cbClsExtra    = 0;
  stWC.cbWndExtra    = 0;
  stWC.hInstance     = NULL;
  stWC.hIcon         = NULL;
  stWC.hCursor       = ::LoadCursor(NULL, IDC_ARROW);
  stWC.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
  stWC.lpszMenuName  = NULL;
  stWC.lpszClassName = _T(CKEYASSIGNATOREDIT_CLASSNAME);

  if (!::RegisterClass(&stWC))
   {
    ASSERT(FALSE);
    return FALSE;
   }

  return TRUE;
 }

BEGIN_MESSAGE_MAP(CKeyAssignatorEdit, CWnd)
      //{{AFX_MSG_MAP(CKeyAssignatorEdit)
      ON_WM_CREATE()
      ON_WM_NCDESTROY()
      ON_WM_SETFOCUS()
      ON_WM_KILLFOCUS()
      ON_WM_KEYDOWN()
      ON_WM_SYSKEYDOWN()
      ON_WM_PAINT()
      ON_WM_CHAR()
      ON_WM_KEYUP()
      //}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CKeyAssignatorEdit message handlers

void CKeyAssignatorEdit::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
 {
  //CWnd::OnChar(nChar, nRepCnt, nFlags);
 }

int CKeyAssignatorEdit::OnCreate(LPCREATESTRUCT lpCreateStruct)
 {
  if (CWnd::OnCreate(lpCreateStruct) == -1)      return -1;

  ModifyStyleEx(0, WS_EX_CLIENTEDGE | WS_EX_NOPARENTNOTIFY);

  SetWindowPos(NULL, 0, 0, 0, 0, SWP_FRAMECHANGED | SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);

  SetFont(CFont::FromHandle((HFONT)GetStockObject(DEFAULT_GUI_FONT)), FALSE);

  return 0;
 }

void CKeyAssignatorEdit::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
 {
  Transcode(nChar);
 }

void CKeyAssignatorEdit::OnKeyUp(UINT nChar, UINT nRepCnt, UINT nFlags)
 {
  //CWnd::OnKeyUp(nChar, nRepCnt, nFlags);
 }

void CKeyAssignatorEdit::OnKillFocus(CWnd* pNewWnd)
 {
  DestroyCaret();
 }

void CKeyAssignatorEdit::OnNcDestroy()
 {
  CWnd::OnNcDestroy();
      
  // Make sure the window was destroyed
  ASSERT(NULL == m_hWnd);

  // Destroy this object since it won't be destroyed otherwise
  delete this;
 }

void CKeyAssignatorEdit::OnPaint()
 {
  CString str;

  GetWindowText(str);

  CPaintDC dc(this);

  dc.SelectObject(GetStockObject(DEFAULT_GUI_FONT));

  dc.TextOut(0, 0, str);
 }

void CKeyAssignatorEdit::OnSetFocus(CWnd* pOldWnd)
 {
  ::CreateCaret(GetSafeHwnd(), NULL, 1, 13);

  SetCaretPos(CPoint(1,1));

  ShowCaret();
 }

void CKeyAssignatorEdit::OnSysKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
 {
  Transcode(nChar);
 }
Comment
Watch Question

Commented:
Hi mike_marquet,

I have an answer for your first two questions. They are the same for either case. The reason that you seemingly don't receive [ENTER]-, [TAB]- and [ESC]-key-messages is, that those keystrokes are translated into another message(s) BEFORE your message handler is called and can handle those. The only place where you can receive those messages is in the PreTranslateMessage( MSG* pMsg )-function. Override this function in your derived control class and intercept the desired keydown-messages.
As for the last question a bit of additional information would be helpful to find out, what the problem is.

regards,
.:fl0yd:.
mike_marquet,

These questions are still open and our records show you logged in recently. Please resolve them appropriately as soon as possible. Continued disregard of your open questions will result in the force/acceptance of a comment as an answer; other actions affecting your account may also be taken. I will revisit these questions in approximately seven (7) days. Please note that the recommended minimum for an "Easy" question is 50 points.

http://experts-exchange.com/jsp/qShow.jsp?qid=11532221
http://experts-exchange.com/jsp/qShow.jsp?qid=11701138
http://experts-exchange.com/jsp/qShow.jsp?qid=11841838
http://experts-exchange.com/jsp/qShow.jsp?qid=11928102
http://experts-exchange.com/jsp/qShow.jsp?qid=11931818
http://experts-exchange.com/jsp/qShow.jsp?qid=11996498
http://experts-exchange.com/jsp/qShow.jsp?qid=11998538
http://experts-exchange.com/jsp/qShow.jsp?qid=12011459
http://experts-exchange.com/jsp/qShow.jsp?qid=12039459
http://experts-exchange.com/jsp/qShow.jsp?qid=20005590
http://experts-exchange.com/jsp/qShow.jsp?qid=20006135
http://experts-exchange.com/jsp/qShow.jsp?qid=20071202
http://experts-exchange.com/jsp/qShow.jsp?qid=20084832
http://experts-exchange.com/jsp/qShow.jsp?qid=20091412
http://experts-exchange.com/jsp/qShow.jsp?qid=20093693
http://experts-exchange.com/jsp/qShow.jsp?qid=20095151
http://experts-exchange.com/jsp/qShow.jsp?qid=20099190
http://experts-exchange.com/jsp/qShow.jsp?qid=20099158
http://experts-exchange.com/jsp/qShow.jsp?qid=20100338
http://experts-exchange.com/jsp/qShow.jsp?qid=20107694
http://experts-exchange.com/jsp/qShow.jsp?qid=20114039
http://experts-exchange.com/jsp/qShow.jsp?qid=20071199
http://experts-exchange.com/jsp/qShow.jsp?qid=20116007
http://experts-exchange.com/jsp/qShow.jsp?qid=20128382
http://experts-exchange.com/jsp/qShow.jsp?qid=20137131
http://experts-exchange.com/jsp/qShow.jsp?qid=20155355
http://experts-exchange.com/jsp/qShow.jsp?qid=20192497
http://experts-exchange.com/jsp/qShow.jsp?qid=20193603
http://experts-exchange.com/jsp/qShow.jsp?qid=20238400
http://experts-exchange.com/jsp/qShow.jsp?qid=20239471
http://experts-exchange.com/jsp/qShow.jsp?qid=20245231
http://experts-exchange.com/jsp/qShow.jsp?qid=20253153

EXPERTS: Please leave your thoughts on this question here.

Thanks,

Netminder
Community Support Moderator
Experts Exchange

Commented:
hi there,
   what was wrong with my answer? didn't my hint help you? so why is it you rate it as 'B'? like i said, to help you with your 3rd problem you ought to supply more information. i certainly won't debug your code -- this is an information center not a humanized debugging facility. only hint i can give you is, that a BEEP usually signals a (keyboard-)input that cannot be handled.

.:fl0yd:.

Explore More ContentExplore courses, solutions, and other research materials related to this topic.