JD2022-TU1/main/tools/framework/JD.Core/EngineKeyCodeConverter.cs

82 lines
2.9 KiB
C#

using System.Windows.Input;
namespace JD.Core
{
/// <summary>
/// Helper class to convert the key codes in C# to the matching key code for the UAF engine.
/// Check InputAdapter.h in engine for the correct input codes.
/// </summary>
public class EngineKeyCodeConverter
{
static EngineKeyCodeConverter()
{
Instance = new EngineKeyCodeConverter();
}
public static EngineKeyCodeConverter Instance { get; }
protected EngineKeyCodeConverter()
{
}
public uint ConvertKeyCode(Key key)
{
switch (key)
{
case Key.Space: return 32;
case Key.Escape: return 257;
case Key.Up: return 283;
case Key.Down: return 284;
case Key.Left: return 285;
case Key.Right: return 286;
case Key.LeftShift: return 287;
case Key.RightShift: return 288;
case Key.LeftCtrl: return 289;
case Key.RightCtrl: return 290;
case Key.LeftAlt: return 291;
case Key.RightAlt: return 292;
case Key.Tab: return 293;
case Key.Enter: return 294;
case Key.Back: return 295;
case Key.Insert: return 296;
case Key.Delete: return 297;
case Key.PageUp: return 298;
case Key.PageDown: return 299;
case Key.Home: return 300;
case Key.End: return 301;
case Key.Divide: return 312;
case Key.Multiply: return 313;
case Key.Subtract: return 314;
case Key.Add: return 315;
case Key.Decimal: return 316;
case Key.PrintScreen: return 319;
default:
{
if (key >= Key.D0 && key <= Key.D9)
return (uint)(48 + key - Key.D0);
if (key >= Key.A && key <= Key.Z)
return (uint)(65 + key - Key.A);
if (key >= Key.F1 && key <= Key.F24)
return (uint)(258 + key - Key.F1);
if (key >= Key.NumPad0 && key <= Key.NumPad9)
return (uint)(302 + key - Key.NumPad0);
return (uint)key;
}
}
}
public uint ConvertModifiersKeyCode(ModifierKeys modifiers)
{
uint modifiersCode = 0;
if ((modifiers & ModifierKeys.Control) == ModifierKeys.Control)
modifiersCode += 1;
if ((modifiers & ModifierKeys.Alt) == ModifierKeys.Alt)
modifiersCode += 2;
if ((modifiers & ModifierKeys.Shift) == ModifierKeys.Shift)
modifiersCode += 4;
return modifiersCode;
}
}
}