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. Handling KeyBoard messages/accelerators handling in MFC dialog based applications

Handling KeyBoard messages/accelerators handling in MFC dialog based applications

Scheduled Pinned Locked Moved C / C++ / MFC
c++sysadmindata-structuresdebugginglearning
13 Posts 4 Posters 2 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.
  • F Offline
    F Offline
    ForNow
    wrote on last edited by
    #1

    I saw an article about this on the codeproject by

    Nish Nishant

    and it seems pretty simple first let me post his code

    BOOL CPreTransTestApp::ProcessMessageFilter(int code, LPMSG lpMsg)
    {
    if(m_haccel)
    {
    if (::TranslateAccelerator(m_pMainWnd->m_hWnd, m_haccel, lpMsg))
    return(TRUE);
    }

    return CWinApp::ProcessMessageFilter(code, lpMsg);
    

    }

    Now let me expain my scenrio I am running a client server TCPI /IP program the server is a z/os mainframe and the client is a MFC C\C++ windows based program which displays data from the mainframe I can have up to 4 modeless dialog boxes which display data in a rich edit their pointers live are my derived CWinApp

    CDBGRApp

    I did all the front end work created the accelarator in my resource file had it as selection in my menu "MENUITEM" and put the appropriate message map and message handler in derived CDialog CProgDebug I then inserted the following code into my derived CWinAppp CDBGRApp

    OOL CDBGRApp::ProcessMessageFilter(int code, LPMSG lpMsg)
    {

    if (m\_haccel)
    {
    	if (debugger\[0\] == NULL);
    	else
    	{
    		if (debugger\[0\]->m\_hWnd == NULL);
    		else
    		{
    		::TranslateAccelerator(debugger\[0\]->m\_hWnd, m\_haccel, lpMsg);
    		return(TRUE);
    		}
    
    	}
    }
    	return CWinApp::ProcessMessageFilter(code, lpMsg);
    }
    

    debugger[0] is the first of an array defined as

    CProgDebug \*debugger\[4\];
    

    After I created the CProgDebug with it rich edit the window didnt seem to take any input mouse or keyboard Nish in his code didnt specify that after doing TranslateAccelrator and would have to do TranslateMessage and DispatchMessage from the msg structure as I assume that is being taken care of somewhere down the line by the frameWork

    G L 2 Replies Last reply
    0
    • F ForNow

      I saw an article about this on the codeproject by

      Nish Nishant

      and it seems pretty simple first let me post his code

      BOOL CPreTransTestApp::ProcessMessageFilter(int code, LPMSG lpMsg)
      {
      if(m_haccel)
      {
      if (::TranslateAccelerator(m_pMainWnd->m_hWnd, m_haccel, lpMsg))
      return(TRUE);
      }

      return CWinApp::ProcessMessageFilter(code, lpMsg);
      

      }

      Now let me expain my scenrio I am running a client server TCPI /IP program the server is a z/os mainframe and the client is a MFC C\C++ windows based program which displays data from the mainframe I can have up to 4 modeless dialog boxes which display data in a rich edit their pointers live are my derived CWinApp

      CDBGRApp

      I did all the front end work created the accelarator in my resource file had it as selection in my menu "MENUITEM" and put the appropriate message map and message handler in derived CDialog CProgDebug I then inserted the following code into my derived CWinAppp CDBGRApp

      OOL CDBGRApp::ProcessMessageFilter(int code, LPMSG lpMsg)
      {

      if (m\_haccel)
      {
      	if (debugger\[0\] == NULL);
      	else
      	{
      		if (debugger\[0\]->m\_hWnd == NULL);
      		else
      		{
      		::TranslateAccelerator(debugger\[0\]->m\_hWnd, m\_haccel, lpMsg);
      		return(TRUE);
      		}
      
      	}
      }
      	return CWinApp::ProcessMessageFilter(code, lpMsg);
      }
      

      debugger[0] is the first of an array defined as

      CProgDebug \*debugger\[4\];
      

      After I created the CProgDebug with it rich edit the window didnt seem to take any input mouse or keyboard Nish in his code didnt specify that after doing TranslateAccelrator and would have to do TranslateMessage and DispatchMessage from the msg structure as I assume that is being taken care of somewhere down the line by the frameWork

      G Offline
      G Offline
      Graham Breach
      wrote on last edited by
      #2

      You missed out a test - you should only return TRUE if TranslateAccelerator() succeeds. By the way, your if(something); else is quite confusing.

      F 1 Reply Last reply
      0
      • F ForNow

        I saw an article about this on the codeproject by

        Nish Nishant

        and it seems pretty simple first let me post his code

        BOOL CPreTransTestApp::ProcessMessageFilter(int code, LPMSG lpMsg)
        {
        if(m_haccel)
        {
        if (::TranslateAccelerator(m_pMainWnd->m_hWnd, m_haccel, lpMsg))
        return(TRUE);
        }

        return CWinApp::ProcessMessageFilter(code, lpMsg);
        

        }

        Now let me expain my scenrio I am running a client server TCPI /IP program the server is a z/os mainframe and the client is a MFC C\C++ windows based program which displays data from the mainframe I can have up to 4 modeless dialog boxes which display data in a rich edit their pointers live are my derived CWinApp

        CDBGRApp

        I did all the front end work created the accelarator in my resource file had it as selection in my menu "MENUITEM" and put the appropriate message map and message handler in derived CDialog CProgDebug I then inserted the following code into my derived CWinAppp CDBGRApp

        OOL CDBGRApp::ProcessMessageFilter(int code, LPMSG lpMsg)
        {

        if (m\_haccel)
        {
        	if (debugger\[0\] == NULL);
        	else
        	{
        		if (debugger\[0\]->m\_hWnd == NULL);
        		else
        		{
        		::TranslateAccelerator(debugger\[0\]->m\_hWnd, m\_haccel, lpMsg);
        		return(TRUE);
        		}
        
        	}
        }
        	return CWinApp::ProcessMessageFilter(code, lpMsg);
        }
        

        debugger[0] is the first of an array defined as

        CProgDebug \*debugger\[4\];
        

        After I created the CProgDebug with it rich edit the window didnt seem to take any input mouse or keyboard Nish in his code didnt specify that after doing TranslateAccelrator and would have to do TranslateMessage and DispatchMessage from the msg structure as I assume that is being taken care of somewhere down the line by the frameWork

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        if (debugger[0] == NULL);
        else
        {

        What is wrong with

        	if (debugger\[0\] != NULL)
            {
        
        F D 2 Replies Last reply
        0
        • L Lost User

          if (debugger[0] == NULL);
          else
          {

          What is wrong with

          	if (debugger\[0\] != NULL)
              {
          
          F Offline
          F Offline
          ForNow
          wrote on last edited by
          #4

          got a compile error when it I did that but I had semi colon after the open paren so the logic should work regardless I'll work to change it thanks

          L 1 Reply Last reply
          0
          • F ForNow

            got a compile error when it I did that but I had semi colon after the open paren so the logic should work regardless I'll work to change it thanks

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            ForNow wrote:

            I had semi colon after the open paren

            :omg:

            1 Reply Last reply
            0
            • G Graham Breach

              You missed out a test - you should only return TRUE if TranslateAccelerator() succeeds. By the way, your if(something); else is quite confusing.

              F Offline
              F Offline
              ForNow
              wrote on last edited by
              #6

              I am changing that so if I get a zero from from

              TranslateAccelerator(

              that means (and can you confirm this) it wasnt one the key strokes I defined right not necessarly an error I would get a zero from TranslateAccelerator maybe thats the problem in my logic

              G 1 Reply Last reply
              0
              • L Lost User

                if (debugger[0] == NULL);
                else
                {

                What is wrong with

                	if (debugger\[0\] != NULL)
                    {
                
                D Offline
                D Offline
                Dave Kreskowiak
                wrote on last edited by
                #7

                Not to mention the semi-colons on the end of the if line, essentially making it entirely useless.

                Asking questions is a skill CodeProject Forum Guidelines Google: C# How to debug code Seriously, go read these articles.
                Dave Kreskowiak

                L 1 Reply Last reply
                0
                • D Dave Kreskowiak

                  Not to mention the semi-colons on the end of the if line, essentially making it entirely useless.

                  Asking questions is a skill CodeProject Forum Guidelines Google: C# How to debug code Seriously, go read these articles.
                  Dave Kreskowiak

                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #8

                  Yes, I have never seen that done deliberately before.

                  1 Reply Last reply
                  0
                  • F ForNow

                    I am changing that so if I get a zero from from

                    TranslateAccelerator(

                    that means (and can you confirm this) it wasnt one the key strokes I defined right not necessarly an error I would get a zero from TranslateAccelerator maybe thats the problem in my logic

                    G Offline
                    G Offline
                    Graham Breach
                    wrote on last edited by
                    #9

                    Here's the Windows docs page: TranslateAcceleratorA function (winuser.h) - Win32 apps | Microsoft Docs[^] The function returns nonzero when it successfully translates the accelerator, which is when you should not pass the message through to the default handler. Your code is bypassing the default handler entirely when your h_accel and debug variables are set (without checking if the message was a translated keycode).

                    F 1 Reply Last reply
                    0
                    • G Graham Breach

                      Here's the Windows docs page: TranslateAcceleratorA function (winuser.h) - Win32 apps | Microsoft Docs[^] The function returns nonzero when it successfully translates the accelerator, which is when you should not pass the message through to the default handler. Your code is bypassing the default handler entirely when your h_accel and debug variables are set (without checking if the message was a translated keycode).

                      F Offline
                      F Offline
                      ForNow
                      wrote on last edited by
                      #10

                      thanks if so this code by Nish

                      BOOL CPreTransTestApp::ProcessMessageFilter(int code, LPMSG lpMsg)
                      {
                      if(m_haccel)
                      {
                      if (::TranslateAccelerator(m_pMainWnd->m_hWnd, m_haccel, lpMsg))
                      return(TRUE);
                      }

                      return CWinApp::ProcessMessageFilter(code, lpMsg);
                      

                      }

                      is incorrect this code from Microsoft docs is correct

                      MSG msg;
                      BOOL bRet;

                      while ( (bRet = GetMessage(&msg, (HWND) NULL, 0, 0)) != 0)
                      {
                      if (bRet == -1)
                      {
                      // handle the error and possibly exit
                      }
                      else
                      {
                      // Check for accelerator keystrokes.

                          if **(!**TranslateAccelerator( 
                                  hwndMain,      // handle to receiving window 
                                  haccel,        // handle to active accelerator table 
                                  &msg))         // message data 
                          {
                              TranslateMessage(&msg); 
                              DispatchMessage(&msg); 
                          } 
                      } 
                      

                      }

                      as it has not TranlateAccelterator wouldnt you agree

                      G 1 Reply Last reply
                      0
                      • F ForNow

                        thanks if so this code by Nish

                        BOOL CPreTransTestApp::ProcessMessageFilter(int code, LPMSG lpMsg)
                        {
                        if(m_haccel)
                        {
                        if (::TranslateAccelerator(m_pMainWnd->m_hWnd, m_haccel, lpMsg))
                        return(TRUE);
                        }

                        return CWinApp::ProcessMessageFilter(code, lpMsg);
                        

                        }

                        is incorrect this code from Microsoft docs is correct

                        MSG msg;
                        BOOL bRet;

                        while ( (bRet = GetMessage(&msg, (HWND) NULL, 0, 0)) != 0)
                        {
                        if (bRet == -1)
                        {
                        // handle the error and possibly exit
                        }
                        else
                        {
                        // Check for accelerator keystrokes.

                            if **(!**TranslateAccelerator( 
                                    hwndMain,      // handle to receiving window 
                                    haccel,        // handle to active accelerator table 
                                    &msg))         // message data 
                            {
                                TranslateMessage(&msg); 
                                DispatchMessage(&msg); 
                            } 
                        } 
                        

                        }

                        as it has not TranlateAccelterator wouldnt you agree

                        G Offline
                        G Offline
                        Graham Breach
                        wrote on last edited by
                        #11

                        They are both correct. The first one skips the default handler if the accelerator has been translated. The second one calls the default handler if the accelerator has not been translated.

                        F 1 Reply Last reply
                        0
                        • G Graham Breach

                          They are both correct. The first one skips the default handler if the accelerator has been translated. The second one calls the default handler if the accelerator has not been translated.

                          F Offline
                          F Offline
                          ForNow
                          wrote on last edited by
                          #12

                          thank you so much for your patience me However the way mine is set is INCORRECT as I dont have an IF testing for the validity of the TranlateAccelarator and that is why my keyboard gets locked becasue I havr return TRUE for all

                          G 1 Reply Last reply
                          0
                          • F ForNow

                            thank you so much for your patience me However the way mine is set is INCORRECT as I dont have an IF testing for the validity of the TranlateAccelarator and that is why my keyboard gets locked becasue I havr return TRUE for all

                            G Offline
                            G Offline
                            Graham Breach
                            wrote on last edited by
                            #13

                            Yes, that's right.

                            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