Solved

Programatically disable an accelerator key (then re-enable it later)

Posted on 2001-09-15
11
577 Views
Last Modified: 2013-11-20
Hi - I've got an accelerator defined so that a commonly used menu item is fired when the user presses the space bar.  At times, I'd like to disable it, then reenable it at a later point. How can I do this? The accelerator is defined under IDR_MAINFRAME.

Thanks,
Marcus
0
Comment
Question by:marcus78
11 Comments
 
LVL 9

Expert Comment

by:ShaunWilde
Comment Utility
why not use a simple flag that you put in the handler for the space bar being pressed.

eg pseudocode

OnSpacebar() // okay not a proper handler but you get the drift
{
  if (bProcess)
  {
    // do menu action
  }
}
0
 

Author Comment

by:marcus78
Comment Utility
Hi - it's not quite a simple as that.  The accelerator is mapped to a function in my document class.  In one of my views, i have a list control where the user can edit some text.  The problem is that when i start editing the text, if i press the space bar, (to actually put a space in the text i'm typing), the handler in the document is fired before it gets to the view. so once the document handles it, even if i use the code below, the space character is stifled before it gets to the view. Hence, the space character won't show up.  So, if i can disable the accelerator when the user begins editing the item in the list control, the message won't get sent to the document.

At least, this is my take on it.  Can you think of anything else?

Thanks,
Marcus
0
 
LVL 2

Expert Comment

by:shilpa_Jb
Comment Utility
Hi
why don't u try using Hook key methods where in u can provide ur own functionality and as well retain the standard feature. Try searching in MSDN for hook key's u will find some samples there.
Hope it helps u.
-shilpa
0
 

Author Comment

by:marcus78
Comment Utility
Shilpa - Will do.  I'll try this out and get back to you.

Thanks!
0
 
LVL 9

Expert Comment

by:ShaunWilde
Comment Utility
how do you edit the text in a list control? - In my xperience a hook should be the last resort not the first
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 3

Expert Comment

by:krispols
Comment Utility
you could also use pretranslate message
0
 
LVL 9

Accepted Solution

by:
ShaunWilde earned 50 total points
Comment Utility
okay I have it handle the onbeginlabeledit and onendlabeledit messages and then manipualte the acceleartor table there

void CSpacebarView::OnBeginlabeledit(NMHDR* pNMHDR, LRESULT* pResult)
{
     LV_DISPINFO* pDispInfo = (LV_DISPINFO*)pNMHDR;
     // TODO: Add your control notification handler code here

    HACCEL haccelOld = ((CFrameWnd*)(AfxGetApp()->m_pMainWnd))->m_hAccelTable;

     // get accelerator data
     LPACCEL lpaccelNew;
     int cAccelerators = CopyAcceleratorTable(
          haccelOld, NULL, 0);

     lpaccelNew = (LPACCEL) LocalAlloc(LPTR,
          cAccelerators * sizeof(ACCEL));
     
     if (lpaccelNew != NULL)
     {
          CopyAcceleratorTable(haccelOld, lpaccelNew,
               cAccelerators);
     }
     
     // find our accelerator
     for (UINT i = 0; (i < (UINT) cAccelerators); i++)
     {
          if (lpaccelNew[i].cmd == ID_SPACE) // this is the ID of out handler
          {
               lpaccelNew[i].key = 0; // change its key to be 0
          }
     }
     
     // Create the new accelerator table, and
     // destroy the old one.
     
     DestroyAcceleratorTable(haccelOld);
     ((CFrameWnd*)(AfxGetApp()->m_pMainWnd))->m_hAccelTable = CreateAcceleratorTable(lpaccelNew,
          cAccelerators);
     
     LocalFree(lpaccelNew);  
           
     *pResult = 0;
}

void CSpacebarView::OnEndlabeledit(NMHDR* pNMHDR, LRESULT* pResult)
{
     LV_DISPINFO* pDispInfo = (LV_DISPINFO*)pNMHDR;
     // TODO: Add your control notification handler code here
    HACCEL haccelOld = ((CFrameWnd*)(AfxGetApp()->m_pMainWnd))->m_hAccelTable;

     // get accelerator data
     LPACCEL lpaccelNew;
     
     int cAccelerators = CopyAcceleratorTable(
          haccelOld, NULL, 0);

     lpaccelNew = (LPACCEL) LocalAlloc(LPTR,
          cAccelerators * sizeof(ACCEL));
     
     if (lpaccelNew != NULL)
     {
          CopyAcceleratorTable(haccelOld, lpaccelNew,
               cAccelerators);
     }
     
     // find our key    
     for (UINT i = 0; (i < (UINT) cAccelerators); i++)
     {
          if (lpaccelNew[i].cmd == ID_SPACE) // thuis is the id of th handler
          {
               lpaccelNew[i].key = VK_SPACE;  // change it back to being VK_SPACE
          }
     }
     
     // Create the new accelerator table, and
     // destroy the old one.
     
     DestroyAcceleratorTable(haccelOld);
     ((CFrameWnd*)(AfxGetApp()->m_pMainWnd))->m_hAccelTable = CreateAcceleratorTable(lpaccelNew,
          cAccelerators);

     LocalFree(lpaccelNew);

     *pResult = 0;
}
 
0
 

Author Comment

by:marcus78
Comment Utility
Shilpa - I agree with ShaunWilde - after reading about it some more, a hook seems rather much for this.

ShaunWilde - this sounds like what I'm looking for.  I'll give it a go, and get back to you soon.  Thanks!

Marcus
0
 
LVL 9

Expert Comment

by:ShaunWilde
Comment Utility
> I'll give it a go, and get back to you soon

the project I used to get this solution was an MFC SDI app with a ListView as the CView derived class
0
 

Author Comment

by:marcus78
Comment Utility
ShaunWilde - That's exactly what I needed.  That solved my problem completly!

Thanks,
Marcus
0
 
LVL 9

Expert Comment

by:ShaunWilde
Comment Utility
okay :)
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Decoding 32 bit binary streams 6 30
Strange date stored 22 100
repeatSeparator  java  challenge 13 56
zeroMAx challenge 20 76
This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now