Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correct display of media buttons in HotKeyBox #2814

Merged
merged 5 commits into from
Jan 27, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,6 @@
Margin="{StaticResource ColumnMargin}">
<Label Style="{DynamicResource DescriptionHeaderStyle}" Content="HotKeyBox" />
<Controls:HotKeyBox Margin="{StaticResource ControlMargin}"
Controls:ControlsHelper.ContentCharacterCasing="Upper"
AreModifierKeysRequired="{Binding ElementName=ModifierKeysRequired, Path=IsChecked}"
HotKey="{Binding HotKey, ValidatesOnDataErrors=True, UpdateSourceTrigger=PropertyChanged, NotifyOnValidationError=True}"
Watermark="Enter hot key" />
Expand Down
21 changes: 16 additions & 5 deletions src/MahApps.Metro/MahApps.Metro.Shared/Controls/HotKeyBox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ public class HotKey : IEquatable<HotKey>
private readonly Key _key;
private readonly ModifierKeys _modifierKeys;

public HotKey(Key key, ModifierKeys modifierKeys)
public HotKey(Key key, ModifierKeys modifierKeys = ModifierKeys.None)
{
_key = key;
_modifierKeys = modifierKeys;
Expand Down Expand Up @@ -255,12 +255,23 @@ public override string ToString()
}
if ((_modifierKeys & ModifierKeys.Windows) == ModifierKeys.Windows)
{
sb.Append("WINDOWS+");
sb.Append("Windows+");
}
sb.Append(GetLocalizedKeyStringUnsafe(KeyInterop.VirtualKeyFromKey(_key)).ToUpper());
sb.Append(GetLocalizedKeyString(_key));
return sb.ToString();
}

private static string GetLocalizedKeyString(Key key)
{
if (key >= Key.BrowserBack && key <= Key.LaunchApplication2)
{
return key.ToString();
}

var vkey = KeyInterop.VirtualKeyFromKey(key);
return GetLocalizedKeyStringUnsafe(vkey) ?? key.ToString();
}

private static string GetLocalizedKeyStringUnsafe(int key)
{
// strip any modifier keys
Expand All @@ -281,8 +292,8 @@ private static string GetLocalizedKeyStringUnsafe(int key)
scanCode |= 0x1000000;
}

UnsafeNativeMethods.GetKeyNameText((int)scanCode, sb, 256);
return sb.ToString();
var resultLength = UnsafeNativeMethods.GetKeyNameText((int)scanCode, sb, 256);
return resultLength > 0 ? sb.ToString() : null;
}

}
Expand Down
1 change: 1 addition & 0 deletions src/MahApps.Metro/MahApps.Metro/Themes/HotKeyBox.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<Setter Property="BorderThickness" Value="1" />
<Setter Property="Controls:ControlsHelper.FocusBorderBrush" Value="{DynamicResource TextBoxFocusBorderBrush}" />
<Setter Property="Controls:ControlsHelper.MouseOverBorderBrush" Value="{DynamicResource TextBoxMouseOverBorderBrush}" />
<Setter Property="Controls:ControlsHelper.ContentCharacterCasing" Value="Upper" />
<Setter Property="FontFamily" Value="{DynamicResource ContentFontFamily}" />
<Setter Property="FontSize" Value="{DynamicResource ContentFontSize}" />
<Setter Property="MinHeight" Value="26" />
Expand Down