1

I want to add a keyboard shortcut to an MFC SDI application in Visual Studio 2010. Here is what I do:

  1. Add the desired key as virtual key to the Accelerator Ressource (e.g. VK_SPACE with ID ID_NEWGAME)
  2. Connect an event handler for COMMAND to ID_NEWGAME in class CMyGameView
  3. Test by trying to stop inside the event handler using the debugger

Result: The program does not react when I hit the space key.

In Visual Studio 6.0, however, the same procedure works as expected.

What am I missing?

chessweb
  • 4,613
  • 5
  • 27
  • 32
  • Are you using the new MFC classes? It could be the same problem as the one in [this unanswered question of mine](http://stackoverflow.com/q/6568610/385646) – MikMik Apr 20 '12 at 10:59
  • @MikMik - see my answer below. I think it's the same problem. – Redeye Apr 20 '12 at 11:04
  • @Redeye: Right. It's the same problem. What bugs me is that menus can also be customized, but if you add new ones in code, they are not overwritten with the ones in the registry. The same should be possible with accelerators. I know, there could be collisions between the new ones and the ones customized by the user, but when there are no collisions? – MikMik Apr 20 '12 at 11:12

1 Answers1

3

I have a hunch you're experiencing a problem I've also come across - when you create an SDI application using the latest version of MFC, by default the application loads the accelerator table from the registry at runtime therefore overwriting anything that you've specified in the resource table. I posted a solution in a previous thread here which I think is the same as what you're seeing. Hope that helps.

Community
  • 1
  • 1
Redeye
  • 1,582
  • 1
  • 14
  • 22
  • Thanks. I added your solution theApp.GetKeyboardManager()->ResetAll(); to CMyGameApp::InitInstance() and it works. What kind of side effects do I have to expect? – chessweb Apr 20 '12 at 11:15
  • That the user cannot efectively customize the accelerators. I mean, any customization the user does will be undone the next time they run the program. – MikMik Apr 20 '12 at 11:30