Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to make the _ appear for hotkeys without the Alt key?

Tags:

c#

.net

wpf

hotkeys

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.

like image 957
Vaccano Avatar asked May 10 '12 23:05

Vaccano


People also ask

What does Alt l do?

In Microsoft Excel, pressing Alt + L opens the Developer tab in the Ribbon. After pressing the shortcut, you can press an additional key to select an option in the Developer tab. For example, after pressing Alt + L , you could press V to open Visual Basic for Applications.

What does Windows Alt R do?

There is a keyboard shortcut that allows you to instantly start or stop recording your screen. Simply press the Windows + Alt + R keys at the same time, and the recording will start. To stop recording, press the same combination again: Win + Alt + R.

What is Alt Mac?

On North American Mac keyboards, a key on the bottom row is labeled both Alt and Option. This is the closest thing the Mac offers to the Windows Alt key. In many situations, keyboard shortcuts that involve the Alt key in Windows use the Option key on the Mac.

How do you program keyboard?

To reassign a keyConnect the keyboard that you want to configure. Select the Start button, and then select Microsoft Mouse and Keyboard Center. From the displayed list of key names, select the key that you want to reassign. In the command list of the key that you want to reassign, select a command.


1 Answers

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 :

//<summary>
//Function to Perform a Keyboard KeyPress.
//</summary>
void PressKey(Key KeyboardKey)
{
    KeyEventArgs args = new KeyEventArgs(Keyboard.PrimaryDevice,
    Keyboard.PrimaryDevice.ActiveSource, 0, Key.LeftAlt);
    args.RoutedEvent = Keyboard.KeyDownEvent;
    InputManager.Current.ProcessInput(args);
}

Code to Append and Remove HotKeyChar :

//<summary>
//Function to Append a HotKeyChar to a Content of a Control.
//</summary>
void AppendHotKeyChar(ContentControl Ctrl, int KeyIndex)
{
    if (Ctrl.Content.ToString().Substring(KeyIndex, 1) != "_")
    {
        Ctrl.Content = "_" + Ctrl.Content;
    }
}
//<summary>
//Function to Remove a HotKeyChar to a Content of a Control.
//</summary>
void RemoveHotKeyChar(ContentControl Ctrl, int KeyIndex)
{
    if (Ctrl.Content.ToString().Substring(KeyIndex, 1) == "_")
    {
        Ctrl.Content = Ctrl.Content.ToString().Remove(KeyIndex, 1);
    }
}

XAML Code for Button Bt1 :

<Button x:Name="Bt1" Content="Button" HorizontalAlignment="Left" Margin="169,97,0,0" VerticalAlignment="Top" Width="75"/>

Code for Window.Loaded event of the MainWindow (e.g. MainWindow1_Loaded) :

PressKey(Key.LeftAlt);

Code for Window.KeyDown event of the MainWindow (e.g. MainWindow1_KeyDown) :

if (e.Key == Key.LeftCtrl)
{
    AppendHotKey(Bt1, 0);
}

Code for Window.KeyUp event of the MainWindow (e.g. MainWindow1_KeyUp) :

if (e.Key == Key.LeftCtrl)
{
    RemoveHotKey(Bt1, 0);
}

Now, When you start your app the Alt will be pressed once dynamically.

And now every-time you press Ctrl, your Control.Content will be Appended with a _ and so the HotKey will appear underlined! But one remark is that you should create Control.Content without HotKeyChar '_' but keep an Index of 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 :

  • Create an instance of List<KeyValuePair<int, Control>> to store the Index of the HotKeyChar and the Control.
  • And now in the KeyDown event just loop through the KeyValuePair<...> in the List<...>..appending the _.
  • In the KeyUp event again just loop through the KeyValuePair<...> in the List<...>..removing the _.

Hope it Helped!

like image 158
Writwick Avatar answered Oct 02 '22 23:10

Writwick