Accelrator keys not working
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: Accelrator keys not working

  1. #1
    Join Date
    Apr 2001
    Location
    San Diego CA
    Posts
    378

    Accelrator keys not working

    Accelrator keys do not work if the focus is on a button that is placed on the toolbar.

    I can catch all the accelrator events via PreTranslateMesage of the main frame like below and handle them individually but that seems like more work and bad design.
    Code:
    // Handle CTRL + I
    if (pMsg->message == WM_KEYDOWN && GetAsyncKeyState(VK_CONTROL) < 0 && (pMsg->wParam == 'i' || pMsg->wParam == 'I'))
    				// Export to Image File trap
    ExportToImageFile();
    
    //Handle ALT+S			
    if (pMsg->message == WM_SYSKEYDOWN && GetKeyState(VK_MENU) < 0 && (pMsg->wParam == 's' || pMsg->wParam == 'S'))
    				// Enter Setup
    				Setup();
    //Handle ALT + R
    if (pMsg->message == WM_SYSKEYDOWN && GetAsyncKeyState(VK_MENU) < 0 && (pMsg->wParam == 'r' || pMsg->wParam == 'R'))
    				// Enter Run
    				Run();
    First issue:
    On the toolbar are three buttons and when these buttons get the focus ( like the user clicked on them) then it seems like the messages are routed to CButton but not to the mainframe.

    I overrode the pretranslate message of the Buttons class like this

    Code:
    BOOL CColorButton::PreTranslateMessage(MSG* pMsg) 
    {
    	if(m_hAccel)
    	{
    		if(::TranslateAccelerator(AfxGetMainWnd()->GetSafeHwnd(), m_hAccel, pMsg))
    		return TRUE;
    	}
    	return CButton::PreTranslateMessage(pMsg);
    }
    But still the accelrator keys do not work.

    Second issue:
    We have two views seperated by a splitter. The right side view is derived from a CView and the left side view is derived from a formview. If the CView has focus then the Shortcut keys like ALT + M etc present on CFormView do not work. They work perfectly if the focus is on the CFormview. So here there are two cases when the keyboard shotcuts don't work.. if the focus is on the toolbar buttons(custom) or the focus is on the CView pane.

    Any suggestions on overcoming this issue will be greatly appriciated

  2. #2
    Join Date
    Feb 2002
    Posts
    5,758
    One solution is to override PreTranslateMessage() on all the child windows and reflect the events to the parent window.

    Kuphryn

  3. #3
    Join Date
    Apr 2001
    Location
    San Diego CA
    Posts
    378
    I could! but I am looking for a way by which I can route the message to MainFrame and it would handle the accelrator and shortcut keys correctly.

    For the splitter with panes I think I could possibly when CView has focus handle the message by routing it to the form view

    Please suggest a way by which I can route messages to the mainframe sucessfully

  4. #4
    Join Date
    Feb 2002
    Posts
    5,758
    One solution is TranslateAccelerator().

    Kuphryn

  5. #5
    Join Date
    Apr 2001
    Location
    San Diego CA
    Posts
    378
    Can you see any reason why TranslateAccelrator is not working when I overrode it in PretranslateMessage
    Code:
    BOOL CColorButton::PreTranslateMessage(MSG* pMsg) 
    {
    	if(m_hAccel)
    	{
    		if(::TranslateAccelerator(AfxGetMainWnd()->GetSafeHwnd(), m_hAccel, pMsg))
    		return TRUE;
    	}
    	return CButton::PreTranslateMessage(pMsg);
    }

  6. #6
    Join Date
    Feb 2002
    Posts
    5,758
    How is m_hAccel initialized?

    Kuphryn

  7. #7
    Join Date
    Apr 2001
    Location
    San Diego CA
    Posts
    378
    I initialize m_hAccel like this

    m_hAccel = ::LoadAccelerators(AfxGetResourceHandle(),
    MAKEINTRESOURCE( IDR_MAINFRAME ) );
    if (!m_hAccel)
    MessageBox("The accelerator table was not loaded");


    Still the menu does not get activated when the ALT key + F ( file menu should activate )

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center