Skip to content

Commit eb7cd4b

Browse files
thoemmipunker76
authored andcommitted
Correct display of media buttons in HotKeyBox (#2814)
* more robust localizing of hotkey names * Handle failing GetKeyNameText * GetKeyNameText does not work with media buttons * don't force UpperCase, but make it default and allow user to change * changed misleading method name
1 parent 6c1dd3d commit eb7cd4b

File tree

3 files changed

+17
-6
lines changed

3 files changed

+17
-6
lines changed

src/MahApps.Metro.Samples/MahApps.Metro.Demo/MahApps.Metro.Demo.Shared/ExampleViews/TextExamples.xaml

-1
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,6 @@
346346
Margin="{StaticResource ColumnMargin}">
347347
<Label Style="{DynamicResource DescriptionHeaderStyle}" Content="HotKeyBox" />
348348
<Controls:HotKeyBox Margin="{StaticResource ControlMargin}"
349-
Controls:ControlsHelper.ContentCharacterCasing="Upper"
350349
AreModifierKeysRequired="{Binding ElementName=ModifierKeysRequired, Path=IsChecked}"
351350
HotKey="{Binding HotKey, ValidatesOnDataErrors=True, UpdateSourceTrigger=PropertyChanged, NotifyOnValidationError=True}"
352351
Watermark="Enter hot key" />

src/MahApps.Metro/MahApps.Metro.Shared/Controls/HotKeyBox.cs

+16-5
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ public class HotKey : IEquatable<HotKey>
201201
private readonly Key _key;
202202
private readonly ModifierKeys _modifierKeys;
203203

204-
public HotKey(Key key, ModifierKeys modifierKeys)
204+
public HotKey(Key key, ModifierKeys modifierKeys = ModifierKeys.None)
205205
{
206206
_key = key;
207207
_modifierKeys = modifierKeys;
@@ -255,12 +255,23 @@ public override string ToString()
255255
}
256256
if ((_modifierKeys & ModifierKeys.Windows) == ModifierKeys.Windows)
257257
{
258-
sb.Append("WINDOWS+");
258+
sb.Append("Windows+");
259259
}
260-
sb.Append(GetLocalizedKeyStringUnsafe(KeyInterop.VirtualKeyFromKey(_key)).ToUpper());
260+
sb.Append(GetLocalizedKeyString(_key));
261261
return sb.ToString();
262262
}
263263

264+
private static string GetLocalizedKeyString(Key key)
265+
{
266+
if (key >= Key.BrowserBack && key <= Key.LaunchApplication2)
267+
{
268+
return key.ToString();
269+
}
270+
271+
var vkey = KeyInterop.VirtualKeyFromKey(key);
272+
return GetLocalizedKeyStringUnsafe(vkey) ?? key.ToString();
273+
}
274+
264275
private static string GetLocalizedKeyStringUnsafe(int key)
265276
{
266277
// strip any modifier keys
@@ -281,8 +292,8 @@ private static string GetLocalizedKeyStringUnsafe(int key)
281292
scanCode |= 0x1000000;
282293
}
283294

284-
UnsafeNativeMethods.GetKeyNameText((int)scanCode, sb, 256);
285-
return sb.ToString();
295+
var resultLength = UnsafeNativeMethods.GetKeyNameText((int)scanCode, sb, 256);
296+
return resultLength > 0 ? sb.ToString() : null;
286297
}
287298

288299
}

src/MahApps.Metro/MahApps.Metro/Themes/HotKeyBox.xaml

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
<Setter Property="BorderThickness" Value="1" />
1111
<Setter Property="Controls:ControlsHelper.FocusBorderBrush" Value="{DynamicResource TextBoxFocusBorderBrush}" />
1212
<Setter Property="Controls:ControlsHelper.MouseOverBorderBrush" Value="{DynamicResource TextBoxMouseOverBorderBrush}" />
13+
<Setter Property="Controls:ControlsHelper.ContentCharacterCasing" Value="Upper" />
1314
<Setter Property="FontFamily" Value="{DynamicResource ContentFontFamily}" />
1415
<Setter Property="FontSize" Value="{DynamicResource ContentFontSize}" />
1516
<Setter Property="MinHeight" Value="26" />

0 commit comments

Comments
 (0)