Solved

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

Posted on 2001-09-15
11
637 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
ID: 6485531
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
ID: 6486143
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
ID: 6486659
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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

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

Thanks!
0
 
LVL 9

Expert Comment

by:ShaunWilde
ID: 6489605
how do you edit the text in a list control? - In my xperience a hook should be the last resort not the first
0
 
LVL 3

Expert Comment

by:krispols
ID: 6489768
you could also use pretranslate message
0
 
LVL 9

Accepted Solution

by:
ShaunWilde earned 50 total points
ID: 6490283
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
ID: 6490531
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
ID: 6490825
> 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
ID: 6493504
ShaunWilde - That's exactly what I needed.  That solved my problem completly!

Thanks,
Marcus
0
 
LVL 9

Expert Comment

by:ShaunWilde
ID: 6493633
okay :)
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
deburging in oracle form 12 104
Named range not carried over 10 65
zeroFront challenge 7 111
My project did see openJDK that I installed. What could be the problem 7 151
Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

809 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