|
|
|
@ -7,6 +7,8 @@ |
|
|
|
namespace Perspex.Input |
|
|
|
{ |
|
|
|
using System; |
|
|
|
using System.Collections.Generic; |
|
|
|
using System.Linq; |
|
|
|
using Perspex.Interactivity; |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
@ -14,6 +16,20 @@ namespace Perspex.Input |
|
|
|
/// </summary>
|
|
|
|
public class AccessKeyHandler : IAccessKeyHandler |
|
|
|
{ |
|
|
|
/// <summary>
|
|
|
|
/// Defines the AccessKeyPressed attached event.
|
|
|
|
/// </summary>
|
|
|
|
public static readonly RoutedEvent<RoutedEventArgs> AccessKeyPressedEvent = |
|
|
|
RoutedEvent.Register<RoutedEventArgs>( |
|
|
|
"AccessKeyPressed", |
|
|
|
RoutingStrategies.Bubble, |
|
|
|
typeof(AccessKeyHandler)); |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// The registered access keys.
|
|
|
|
/// </summary>
|
|
|
|
private List<Tuple<string, IInputElement>> registered = new List<Tuple<string, IInputElement>>(); |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// The window to which the handler belongs.
|
|
|
|
/// </summary>
|
|
|
|
@ -53,17 +69,58 @@ namespace Perspex.Input |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Handles the Alt/F10 keys being pressed in the window.
|
|
|
|
/// Registers an input element to be associated with an access key.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="accessKey">The access key.</param>
|
|
|
|
/// <param name="element">The input element.</param>
|
|
|
|
public void Register(char accessKey, IInputElement element) |
|
|
|
{ |
|
|
|
var existing = this.registered.FirstOrDefault(x => x.Item2 == element); |
|
|
|
|
|
|
|
if (existing != null) |
|
|
|
{ |
|
|
|
this.registered.Remove(existing); |
|
|
|
} |
|
|
|
|
|
|
|
this.registered.Add(Tuple.Create(accessKey.ToString().ToUpper(), element)); |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Unregisters the access keys associated with the input element.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="element">The input element.</param>
|
|
|
|
public void Unregister(IInputElement element) |
|
|
|
{ |
|
|
|
foreach (var i in this.registered.Where(x => x.Item2 == element).ToList()) |
|
|
|
{ |
|
|
|
this.registered.Remove(i); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Handles the Alt key being pressed in the window.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="sender">The event sender.</param>
|
|
|
|
/// <param name="e">The event args.</param>
|
|
|
|
protected virtual void OnPreviewKeyDown(object sender, KeyEventArgs e) |
|
|
|
{ |
|
|
|
if (e.Key == Key.LeftAlt || e.Key == Key.F10) |
|
|
|
if (e.Key == Key.LeftAlt) |
|
|
|
{ |
|
|
|
this.owner.ShowAccessKeys = this.showingAccessKeys = true; |
|
|
|
e.Handled = true; |
|
|
|
} |
|
|
|
else if ((KeyboardDevice.Instance.Modifiers & ModifierKeys.Alt) != 0) |
|
|
|
{ |
|
|
|
var text = e.Text.ToUpper(); |
|
|
|
var focus = this.registered |
|
|
|
.Where(x => x.Item1 == text && x.Item2.IsEffectivelyVisible) |
|
|
|
.FirstOrDefault()?.Item2; |
|
|
|
|
|
|
|
if (focus != null) |
|
|
|
{ |
|
|
|
focus.RaiseEvent(new RoutedEventArgs(AccessKeyPressedEvent)); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
@ -73,13 +130,22 @@ namespace Perspex.Input |
|
|
|
/// <param name="e">The event args.</param>
|
|
|
|
protected virtual void OnPreviewKeyUp(object sender, KeyEventArgs e) |
|
|
|
{ |
|
|
|
if (e.Key == Key.LeftAlt || e.Key == Key.F10) |
|
|
|
switch (e.Key) |
|
|
|
{ |
|
|
|
if (this.showingAccessKeys && this.MainMenu != null) |
|
|
|
{ |
|
|
|
case Key.LeftAlt: |
|
|
|
if (this.showingAccessKeys && this.MainMenu != null) |
|
|
|
{ |
|
|
|
this.MainMenu.OpenMenu(); |
|
|
|
e.Handled = true; |
|
|
|
} |
|
|
|
|
|
|
|
break; |
|
|
|
|
|
|
|
case Key.F10: |
|
|
|
this.owner.ShowAccessKeys = this.showingAccessKeys = true; |
|
|
|
this.MainMenu.OpenMenu(); |
|
|
|
e.Handled = true; |
|
|
|
} |
|
|
|
break; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|