In WPF when you make a label like this:
<Label Content="_My Label"/>
Then when you run the app and press the Alt key it will show the “M” underlined.
We have our own custom hotkey Attached Property that allows us to use Ctrl as well as Alt.
Problem is that only Alt will show the underscores.
Is there a way to show the underscore when the Ctrl key is pressed?
NOTE: I do NOT want to send a programmatic Alt KeyPress in the background when Ctrl is pressed. That will just confuse my shortcut system.
Ok! I have got a solution to show the
_for hot-keys without Alt pressed but Ctrl pressed.Here is how to Do it :
Small Code to Press a KeyBoard Key dynamically :
Code to Append and Remove
HotKeyChar:XAML Code for
Button Bt1:Code for
Window.Loadedevent of theMainWindow(e.g.MainWindow1_Loaded) :Code for
Window.KeyDownevent of theMainWindow(e.g.MainWindow1_KeyDown) :Code for
Window.KeyUpevent of theMainWindow(e.g.MainWindow1_KeyUp) :Now, When you start your app the Alt will be pressed once dynamically.
And now every-time you press Ctrl, your
Control.Contentwill be Appended with a_and so theHotKeywill appear underlined!But one remark is that you should create
Control.ContentwithoutHotKeyChar '_'but keep anIndexof where your_will be appended.But keep in mind that if Alt is pressed again in your app, The code will not work anymore. So, you have to press the Alt again to make the code work!
Best way to appending and removing a
HotKeyChar:List<KeyValuePair<int, Control>>to store theIndexof theHotKeyCharand theControl.KeyDownevent just loop through theKeyValuePair<...>in theList<...>..appending the_.KeyUpevent again just loop through theKeyValuePair<...>in theList<...>..removing the_.Hope it Helped!