Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. CDialog ON_WM_KEYDOWN event

CDialog ON_WM_KEYDOWN event

Scheduled Pinned Locked Moved C / C++ / MFC
help
6 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    chevu
    wrote on last edited by
    #1

    Hi, I am using ON_WM_KEYDOWN event for keyboard shortcuts in my application. And below is function to handle this keydown action.

    void CRearPanel::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
    {
    if(nChar == 112)
    {
    if(((CMainFrame*)AfxGetMainWnd())->GetRearPanel()->m_hWnd != NULL)
    ((CMainFrame*)AfxGetMainWnd())->GetRearPanel()->ShowWindow(SW_HIDE);
    if(((CMainFrame*)AfxGetMainWnd())->GetHWSimulatorDlg()->m_hWnd != NULL)
    ((CMainFrame*)AfxGetMainWnd())->GetHWSimulatorDlg()->ShowWindow(SW_HIDE);
    }
    else if(nChar == 113)
    {
    ((CMainFrame*)AfxGetMainWnd())->OnViewHWSimulator();

    	if(((CMainFrame\*)AfxGetMainWnd())->GetRearPanel()->m\_hWnd != NULL)
    		((CMainFrame\*)AfxGetMainWnd())->GetRearPanel()->ShowWindow(SW\_HIDE);
    }
    else if(nChar == 114)
    {
    	((CMainFrame\*)AfxGetMainWnd())->OnViewRearpanel();
    	::SetFocus(((CMainFrame\*)AfxGetMainWnd())->GetRearPanel()->m\_hWnd);
    
    	if(((CMainFrame\*)AfxGetMainWnd())->GetHWSimulatorDlg()->m\_hWnd != NULL)
    		((CMainFrame\*)AfxGetMainWnd())->GetHWSimulatorDlg()->ShowWindow(SW\_HIDE);
    }
    
    CDialog::OnKeyDown(nChar, nRepCnt, nFlags);
    

    }

    Now let me explain my problem, Here I am having one main document window which is coming up initially. And I have another 2 dialog window which should be open on keydown event. From main document window I am able to get key down event and functionality working properly. In Those 2 dialog I put same events, and there start my problem. First time when it(dialog) comes up its not catching keydown event. If I close that dialog and then open it again then I am able to catch it. I found on net some where to put below code,

    BOOL CRearPanel::PreTranslateMessage(MSG* pMsg)
    {
    if( pMsg->message == WM_KEYDOWN )
    {
    ::TranslateMessage(pMsg);
    ::DispatchMessage(pMsg);
    return (1);
    }
    return CDialog::PreTranslateMessage(pMsg);
    }

    But I dint find it helpful. Can any one help me for this issue.

    N C 2 Replies Last reply
    0
    • C chevu

      Hi, I am using ON_WM_KEYDOWN event for keyboard shortcuts in my application. And below is function to handle this keydown action.

      void CRearPanel::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
      {
      if(nChar == 112)
      {
      if(((CMainFrame*)AfxGetMainWnd())->GetRearPanel()->m_hWnd != NULL)
      ((CMainFrame*)AfxGetMainWnd())->GetRearPanel()->ShowWindow(SW_HIDE);
      if(((CMainFrame*)AfxGetMainWnd())->GetHWSimulatorDlg()->m_hWnd != NULL)
      ((CMainFrame*)AfxGetMainWnd())->GetHWSimulatorDlg()->ShowWindow(SW_HIDE);
      }
      else if(nChar == 113)
      {
      ((CMainFrame*)AfxGetMainWnd())->OnViewHWSimulator();

      	if(((CMainFrame\*)AfxGetMainWnd())->GetRearPanel()->m\_hWnd != NULL)
      		((CMainFrame\*)AfxGetMainWnd())->GetRearPanel()->ShowWindow(SW\_HIDE);
      }
      else if(nChar == 114)
      {
      	((CMainFrame\*)AfxGetMainWnd())->OnViewRearpanel();
      	::SetFocus(((CMainFrame\*)AfxGetMainWnd())->GetRearPanel()->m\_hWnd);
      
      	if(((CMainFrame\*)AfxGetMainWnd())->GetHWSimulatorDlg()->m\_hWnd != NULL)
      		((CMainFrame\*)AfxGetMainWnd())->GetHWSimulatorDlg()->ShowWindow(SW\_HIDE);
      }
      
      CDialog::OnKeyDown(nChar, nRepCnt, nFlags);
      

      }

      Now let me explain my problem, Here I am having one main document window which is coming up initially. And I have another 2 dialog window which should be open on keydown event. From main document window I am able to get key down event and functionality working properly. In Those 2 dialog I put same events, and there start my problem. First time when it(dialog) comes up its not catching keydown event. If I close that dialog and then open it again then I am able to catch it. I found on net some where to put below code,

      BOOL CRearPanel::PreTranslateMessage(MSG* pMsg)
      {
      if( pMsg->message == WM_KEYDOWN )
      {
      ::TranslateMessage(pMsg);
      ::DispatchMessage(pMsg);
      return (1);
      }
      return CDialog::PreTranslateMessage(pMsg);
      }

      But I dint find it helpful. Can any one help me for this issue.

      N Offline
      N Offline
      Nelek
      wrote on last edited by
      #2

      Do you want the dialogs to execute anything when key down? or.. Is the parent window the one that has to execute anything on the key down from dialog?

      Regards. -------- M.D.V. ;) If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about? Help me to understand what I'm saying, and I'll explain it better to you Rating helpfull answers is nice, but saying thanks can be even nicer.

      C 1 Reply Last reply
      0
      • N Nelek

        Do you want the dialogs to execute anything when key down? or.. Is the parent window the one that has to execute anything on the key down from dialog?

        Regards. -------- M.D.V. ;) If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about? Help me to understand what I'm saying, and I'll explain it better to you Rating helpfull answers is nice, but saying thanks can be even nicer.

        C Offline
        C Offline
        chevu
        wrote on last edited by
        #3

        well currently I have set same shortcut action in main window and 2 dialog. I mean for all window action for F1 F2 F3 should be same. I thought of passing action msg to main window back, but for that also those dialog should get event.. Is there any work around?

        modified on Tuesday, February 2, 2010 10:33 PM

        C 1 Reply Last reply
        0
        • C chevu

          well currently I have set same shortcut action in main window and 2 dialog. I mean for all window action for F1 F2 F3 should be same. I thought of passing action msg to main window back, but for that also those dialog should get event.. Is there any work around?

          modified on Tuesday, February 2, 2010 10:33 PM

          C Offline
          C Offline
          chevu
          wrote on last edited by
          #4

          Hi i think i got the problem. But I dont know how to solve it. Here the dialog I am talking have internally different dialog attached into their body. So focus is not fixed sometimes its that in main dialog sometimes in child dialog. Is there any way that for 3 keys F1, F2, F3 took event in mainframe or main application class. I dont want to pass this 3 key down event to any further dialog,

          1 Reply Last reply
          0
          • C chevu

            Hi, I am using ON_WM_KEYDOWN event for keyboard shortcuts in my application. And below is function to handle this keydown action.

            void CRearPanel::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
            {
            if(nChar == 112)
            {
            if(((CMainFrame*)AfxGetMainWnd())->GetRearPanel()->m_hWnd != NULL)
            ((CMainFrame*)AfxGetMainWnd())->GetRearPanel()->ShowWindow(SW_HIDE);
            if(((CMainFrame*)AfxGetMainWnd())->GetHWSimulatorDlg()->m_hWnd != NULL)
            ((CMainFrame*)AfxGetMainWnd())->GetHWSimulatorDlg()->ShowWindow(SW_HIDE);
            }
            else if(nChar == 113)
            {
            ((CMainFrame*)AfxGetMainWnd())->OnViewHWSimulator();

            	if(((CMainFrame\*)AfxGetMainWnd())->GetRearPanel()->m\_hWnd != NULL)
            		((CMainFrame\*)AfxGetMainWnd())->GetRearPanel()->ShowWindow(SW\_HIDE);
            }
            else if(nChar == 114)
            {
            	((CMainFrame\*)AfxGetMainWnd())->OnViewRearpanel();
            	::SetFocus(((CMainFrame\*)AfxGetMainWnd())->GetRearPanel()->m\_hWnd);
            
            	if(((CMainFrame\*)AfxGetMainWnd())->GetHWSimulatorDlg()->m\_hWnd != NULL)
            		((CMainFrame\*)AfxGetMainWnd())->GetHWSimulatorDlg()->ShowWindow(SW\_HIDE);
            }
            
            CDialog::OnKeyDown(nChar, nRepCnt, nFlags);
            

            }

            Now let me explain my problem, Here I am having one main document window which is coming up initially. And I have another 2 dialog window which should be open on keydown event. From main document window I am able to get key down event and functionality working properly. In Those 2 dialog I put same events, and there start my problem. First time when it(dialog) comes up its not catching keydown event. If I close that dialog and then open it again then I am able to catch it. I found on net some where to put below code,

            BOOL CRearPanel::PreTranslateMessage(MSG* pMsg)
            {
            if( pMsg->message == WM_KEYDOWN )
            {
            ::TranslateMessage(pMsg);
            ::DispatchMessage(pMsg);
            return (1);
            }
            return CDialog::PreTranslateMessage(pMsg);
            }

            But I dint find it helpful. Can any one help me for this issue.

            C Offline
            C Offline
            chevu
            wrote on last edited by
            #5

            Sorry to bother you guys.. I got solution.. Keyboard messages/accelerators handling in MFC dialog based applications[^]

            N 1 Reply Last reply
            0
            • C chevu

              Sorry to bother you guys.. I got solution.. Keyboard messages/accelerators handling in MFC dialog based applications[^]

              N Offline
              N Offline
              Nelek
              wrote on last edited by
              #6

              You were not bothering, and sorry about the absence (I was pretty busy last days). Glad that you found a solution :)

              Regards. -------- M.D.V. ;) If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about? Help me to understand what I'm saying, and I'll explain it better to you Rating helpfull answers is nice, but saying thanks can be even nicer.

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups