CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Aug 2002
    Posts
    212

    Accelerators and edit controls

    I am having trouble with accelerators and typing in edit controls. For example, if the user is typing text in an edit control in the app, and presses the delete key, instead of deleting a character like normal, it sends the ID_EDIT_DELETE command, causing the selected objects to be deleted. How can I fix this? I think I need to override PreTranslateMessage, and check what window has focus before passing it on, but I really don't know what to do in PreTranslateMessage. I would appreciate any help

  2. #2
    Join Date
    May 2004
    Location
    Germany
    Posts
    655

    Re: Accelerators and edit controls

    do you have the DELETE key as accelarator in a accelarator table?

    but if so... you were to override PreTranslateMessage. So you know what to do

    to check if your edit control has the focus, and only TranslateAccelarator if not do something like this:

    Code:
    Dialog/View::PreTranslateMessage(MSG* pMsg)
    {
        //get a pointer to your edit control either in this way:
             CWnd* pwndEdit = GetDlgItem(IDC_MY_EDIT);
       //or you have a member control variable of type CEdit
             CWnd* pwndEdit = &m_ctrlEdit;
    
       //now check the focus of the edit control, and if it doesn't have it do the TranslateAccelarator
      if( GetFocus() == pwndEdit )
         return Dialolg/ViewBaseclass::PreTranslateMessage(pMsg);
      else
      {
             if( !TranslateAccelarator(...) )
                return Dialolg/ViewBaseclass::PreTranslateMessage(pMsg);        
      } 
    }
    there are 10 kinds of people. those who understand binary and those who don't...

    rate a post if you find it usefull, thx
    check out my Firefox/Mozilla Extension: http://urlparams.blogwart.com/

  3. #3
    Join Date
    Aug 2002
    Posts
    212

    Re: Accelerators and edit controls

    That is basically what I came up with, but I couldn't figure out what to pass to TranslateAccelerator when I tried it. Since you left that part out () I took a potshot with this:
    Code:
    BOOL CMainFrame::PreTranslateMessage(MSG* pMsg) 
    {
    	if(GetFocus() && GetFocus()->IsKindOf(RUNTIME_CLASS(CRtsEdView)))
    	{
    		if(!TranslateAccelerator(m_hWnd, NULL, pMsg))
    			return CMDIFrameWnd::PreTranslateMessage(pMsg);
    		return TRUE;
    	}
    	else
    	{
    		return CWnd::PreTranslateMessage(pMsg);
    	}
    }
    I have a crapload of controls that were being screwed up by my accelerators, so I needed to have them only be translated when a certain window is in focus, rather than when a certain window isn't.

    I have no idea how that works (NULL Accelerator table ***?) but it does. Thanks for your help.

  4. #4
    Join Date
    May 2004
    Location
    Germany
    Posts
    655

    Re: Accelerators and edit controls

    i see

    1. do it in your view class not in the mainframe.
    2. make a member var in your view class of type HACCEL (HACCEL m_hAccel)
    3. in OnCreate make LoadAccelerators(NULL, MAKEINTRESOURCE(ID_OF_ACCEL_TABLE_IN_YOUR_RESOURCES))
    4. in PreTranslateMessage: TranslateAccelarator(m_hWnd, m_hAccel, pMsg)

    now it should work
    there are 10 kinds of people. those who understand binary and those who don't...

    rate a post if you find it usefull, thx
    check out my Firefox/Mozilla Extension: http://urlparams.blogwart.com/

  5. #5
    Join Date
    Aug 2002
    Posts
    212

    Re: Accelerators and edit controls

    That makes much more sense, and that is more flexible, thanks. What I posted does work btw, I just couldn't figure out how.

  6. #6
    Join Date
    May 2004
    Location
    Germany
    Posts
    655

    Re: Accelerators and edit controls

    What I posted does work btw, I just couldn't figure out how.
    hm... from that what MSDN says, i doubt that it really works... because of that second NULL parameter
    Quote Originally Posted by MSDN
    [in] Handle to the accelerator table. The accelerator table must have been loaded by a call to the LoadAccelerators function or created by a call to the CreateAcceleratorTable function.
    but ok
    there are 10 kinds of people. those who understand binary and those who don't...

    rate a post if you find it usefull, thx
    check out my Firefox/Mozilla Extension: http://urlparams.blogwart.com/

  7. #7
    Join Date
    Aug 2002
    Posts
    212

    Re: Accelerators and edit controls

    Believe me I have no friggin idea how it works, but I've been using it and it has consistently worked fine. I'm pretty sure its just because TranslateAccelerator returns false right away, and then CMDIFrameWnd::PreTranslateMessage calls TranslateAccelerator properly. Actually I know that is why, you just got me thinking about it.

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured