You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

129 lines
4.2 KiB
C#

#if WINDOWS
using Microsoft.UI.Xaml.Input;
using Windows.System;
#endif
namespace CommanderApp.Services;
public class KeyboardService : IKeyboardService
{
public event EventHandler<KeyPressedEventArgs> KeyPressed;
public void SetupKeyboardHandling(ContentPage page)
{
#if WINDOWS
SetupWindowsKeyboardHandling(page);
#elif MACCATALYST
SetupMacKeyboardHandling(page);
#endif
}
#if WINDOWS
private void SetupWindowsKeyboardHandling(ContentPage page)
{
try
{
if (page.Handler?.PlatformView is Microsoft.UI.Xaml.FrameworkElement frameworkElement)
{
frameworkElement.KeyDown += OnWindowsKeyDown;
}
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine($"Windows keyboard setup error: {ex.Message}");
}
}
private void OnWindowsKeyDown(object sender, KeyRoutedEventArgs e)
{
var key = e.Key switch
{
VirtualKey.W => "w",
VirtualKey.S => "s",
VirtualKey.A => "a",
VirtualKey.D => "d",
VirtualKey.Space => " ",
VirtualKey.Enter => "enter",
VirtualKey.F5 => "f5",
VirtualKey.F6 => "f6",
VirtualKey.F7 => "f7",
VirtualKey.F8 => "f8",
VirtualKey.F10 => "f10",
VirtualKey.H => "h",
_ => null
};
if (key != null)
{
KeyPressed?.Invoke(this, new KeyPressedEventArgs { Key = key, Platform = "Windows" });
e.Handled = true;
}
}
#endif
#if MACCATALYST
private void SetupMacKeyboardHandling(ContentPage page)
{
try
{
if (page.Handler?.PlatformView is UIKit.UIView uiView)
{
var keyHandler = new MacKeyHandler(OnMacKeyPressed);
keyHandler.Frame = uiView.Bounds;
keyHandler.AutoresizingMask = UIKit.UIViewAutoresizing.All;
uiView.AddSubview(keyHandler);
}
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine($"Mac keyboard setup error: {ex.Message}");
}
}
private void OnMacKeyPressed(string key)
{
KeyPressed?.Invoke(this, new KeyPressedEventArgs { Key = key, Platform = "Mac" });
}
public class MacKeyHandler : UIKit.UIView
{
private readonly Action<string> _keyHandler;
public MacKeyHandler(Action<string> keyHandler)
{
_keyHandler = keyHandler;
this.BecomeFirstResponder();
}
public override bool CanBecomeFirstResponder => true;
public override UIKit.UIKeyCommand[] KeyCommands => new[]
{
UIKit.UIKeyCommand.Create((Foundation.NSString)"w", (UIKit.UIKeyModifierFlags)0, new ObjCRuntime.Selector("handleW:")),
UIKit.UIKeyCommand.Create((Foundation.NSString)"s", (UIKit.UIKeyModifierFlags)0, new ObjCRuntime.Selector("handleS:")),
UIKit.UIKeyCommand.Create((Foundation.NSString)"a", (UIKit.UIKeyModifierFlags)0, new ObjCRuntime.Selector("handleA:")),
UIKit.UIKeyCommand.Create((Foundation.NSString)"d", (UIKit.UIKeyModifierFlags)0, new ObjCRuntime.Selector("handleD:")),
UIKit.UIKeyCommand.Create((Foundation.NSString)" ", (UIKit.UIKeyModifierFlags)0, new ObjCRuntime.Selector("handleSpace:")),
UIKit.UIKeyCommand.Create((Foundation.NSString)"h", (UIKit.UIKeyModifierFlags)0, new ObjCRuntime.Selector("handleH:"))
};
[Foundation.Export("handleW:")]
void HandleW(UIKit.UIKeyCommand cmd) => _keyHandler?.Invoke("w");
[Foundation.Export("handleS:")]
void HandleS(UIKit.UIKeyCommand cmd) => _keyHandler?.Invoke("s");
[Foundation.Export("handleA:")]
void HandleA(UIKit.UIKeyCommand cmd) => _keyHandler?.Invoke("a");
[Foundation.Export("handleD:")]
void HandleD(UIKit.UIKeyCommand cmd) => _keyHandler?.Invoke("d");
[Foundation.Export("handleSpace:")]
void HandleSpace(UIKit.UIKeyCommand cmd) => _keyHandler?.Invoke(" ");
[Foundation.Export("handleH:")]
void HandleH(UIKit.UIKeyCommand cmd) => _keyHandler?.Invoke("h");
}
#endif
}