none
Is it possible to set a ctrl hotkey for a winforms button and get the same behavior as an alt hotkey?

    Question

  • It's easy to set up an alt hotkey for a winform button by adding an & in front of the hotkey character in the text property of the button.  I'd like to add ctrl hotkeys for buttons and get exactly the same behavior.

    It's easy enough to override ProcessCmdKey to execute certain button actions, but this doesn't show the user what the hotkeys are for buttons when you press the ctrl key.  Ideally, there would be a way to assign certain hotkeys to buttons, but don't see a way to do this.  Would the best approach be to extend Button and add additional hotkey behavior?

    I'm using .net 4.0, VS 2010.

    Thanks for any help!

    Friday, July 23, 2010 6:02 PM

Answers

  • So you want to start using Ctrl accelerators because you ran out of Alt accelerators!? If that's the case, then I don't think you want to emulate the regular accelerator behavior exactly, because then you won't be able to differentiate between a Ctrl accelerator and an Alt accelerator. You should probably just handle the KeyDown/KeyUp events on your form and check for keyboard shortcuts that way. You'll be able to determine the modifier as well as the key from the event arguments.

    Also, how are you going to visually show what the Ctrl accelerator is for a button? Again, you can't just underline the letter like normal because then it will confused for a normal Alt accelerator.

    Saturday, July 24, 2010 2:04 AM

All replies

  • I don't understand why you'd want to do this. People have been using Alt for accelerators for 25+ years (at least the people who actually use keyboard accelerators, which is already a very small percentage of total users). That's been the convention since the birth of Windows. And now you want to come along and change it up for your program? That will only serve to confuse your users.

    In conclusion, there's a reason why there is no easy way to set up Ctrl accelerators.

    Besides, Ctrl shortcuts are reserved for menu shortcuts.

    Hope this helps.

    Friday, July 23, 2010 6:23 PM
  • Yes I agree on the convention, but I've been spefically asked to do this by a client.  They have a screen with over 50 buttons on it (don't ask), and specifically asked for control hotkeys so they don't have to hit <alt><shift>special characters.  The buttons would also look less appealing to them with special characters displayed on them.
    Friday, July 23, 2010 6:49 PM
  • Would the best approach be to extend Button and add additional hotkey behavior?
    They have a screen with over 50 buttons on it (don't ask)
    1. Yes (see #2)
    2. OMG. What's the point when you're nearly at half of all the keys on the keyboard. Good luck to you!

    -Scosby

    Friday, July 23, 2010 9:00 PM
  • So you want to start using Ctrl accelerators because you ran out of Alt accelerators!? If that's the case, then I don't think you want to emulate the regular accelerator behavior exactly, because then you won't be able to differentiate between a Ctrl accelerator and an Alt accelerator. You should probably just handle the KeyDown/KeyUp events on your form and check for keyboard shortcuts that way. You'll be able to determine the modifier as well as the key from the event arguments.

    Also, how are you going to visually show what the Ctrl accelerator is for a button? Again, you can't just underline the letter like normal because then it will confused for a normal Alt accelerator.

    Saturday, July 24, 2010 2:04 AM
  • Yes, the client asked for Ctrl accelerators due to the sheer number of buttons and accelerator keys needed.  I would have to override the accelerator key behavior so that when you depress the ctrl key, you would see the ctrl accelerators (underline text), and when you depress the alt key, you would see the alt accelerators.  After key up, the accelerator (underline) text would need to disappear to avoid the confusion you mention.

    In any case, I'm going to try to convice the client to go with a different approach.  I'm doing my homework to confirm that Ctrl accelerators are not only non standard, but a good bit of extra work (cost) to implement.

    Monday, July 26, 2010 3:46 PM
  • Link to related post in Stack Overflow:

    Tying Ctrl hotkeys to Windows Forms buttons

    Thursday, January 03, 2013 5:53 PM