213 lines
5.3 KiB
C#
213 lines
5.3 KiB
C#
namespace Engine.Input;
|
||
|
||
public enum KeyboardButtonCode
|
||
{
|
||
A,
|
||
B,
|
||
C,
|
||
D,
|
||
E,
|
||
F,
|
||
G,
|
||
H,
|
||
I,
|
||
J,
|
||
K,
|
||
L,
|
||
M,
|
||
N,
|
||
O,
|
||
P,
|
||
Q,
|
||
R,
|
||
S,
|
||
T,
|
||
U,
|
||
V,
|
||
W,
|
||
X,
|
||
Y,
|
||
Z,
|
||
|
||
D1,
|
||
D2,
|
||
D3,
|
||
D4,
|
||
D5,
|
||
D6,
|
||
D7,
|
||
D8,
|
||
D9,
|
||
D0,
|
||
|
||
Ctrl,
|
||
Alt,
|
||
Shift,
|
||
|
||
Up,
|
||
Down,
|
||
Left,
|
||
Right,
|
||
|
||
Escape,
|
||
Enter,
|
||
Space,
|
||
Tab,
|
||
Backspace,
|
||
Delete,
|
||
Insert,
|
||
Home,
|
||
End,
|
||
PageUp,
|
||
PageDown,
|
||
|
||
/// <summary>
|
||
/// Represents the total count of keyboard button codes.
|
||
/// </summary>
|
||
TotalCount = PageDown + 1
|
||
}
|
||
|
||
/// <summary>
|
||
/// Provides helper methods for working with keyboard button codes.
|
||
/// </summary>
|
||
public static class KeyboardButtonCodeHelper
|
||
{
|
||
/// <summary>
|
||
/// Gets a list of all printable keyboard button codes.
|
||
/// </summary>
|
||
/// <returns>A list of printable keyboard button codes.</returns>
|
||
public static List<KeyboardButtonCode> GetAllPrintableKeys()
|
||
{
|
||
return Enum.GetValues<KeyboardButtonCode>().Where(parX => parX.IsPrintableKey()).ToList();
|
||
}
|
||
|
||
/// <summary>
|
||
/// Determines whether a keyboard button code is printable.
|
||
/// </summary>
|
||
/// <param name="parKey">The keyboard button code to check.</param>
|
||
/// <returns>True if the key is printable; otherwise, false.</returns>
|
||
public static bool IsPrintableKey(this KeyboardButtonCode parKey)
|
||
{
|
||
return parKey is >= KeyboardButtonCode.A and <= KeyboardButtonCode.Z
|
||
or >= KeyboardButtonCode.D1 and <= KeyboardButtonCode.D0 or KeyboardButtonCode.Space;
|
||
}
|
||
|
||
/// <summary>
|
||
/// Gets the character representation of a keyboard button code based on the current input language.
|
||
/// </summary>
|
||
/// <param name="parKey">The keyboard button code to convert.</param>
|
||
/// <returns>The character representation of the key.</returns>
|
||
public static char GetChar(this KeyboardButtonCode parKey)
|
||
{
|
||
return Engine.Instance.InputHandler!.CurrentInputLanguage.Name switch
|
||
{
|
||
"en-US" => GetEnChar(parKey),
|
||
"ru-RU" => GetRuChar(parKey),
|
||
_ => GetEnChar(parKey)
|
||
};
|
||
}
|
||
|
||
/// <summary>
|
||
/// Gets the character representation of a keyboard button code in English.
|
||
/// </summary>
|
||
/// <param name="parKey">The keyboard button code to convert.</param>
|
||
/// <returns>The character representation of the key.</returns>
|
||
private static char GetEnChar(this KeyboardButtonCode parKey)
|
||
{
|
||
return parKey switch
|
||
{
|
||
KeyboardButtonCode.A => 'A',
|
||
KeyboardButtonCode.B => 'B',
|
||
KeyboardButtonCode.C => 'C',
|
||
KeyboardButtonCode.D => 'D',
|
||
KeyboardButtonCode.E => 'E',
|
||
KeyboardButtonCode.F => 'F',
|
||
KeyboardButtonCode.G => 'G',
|
||
KeyboardButtonCode.H => 'H',
|
||
KeyboardButtonCode.I => 'I',
|
||
KeyboardButtonCode.J => 'J',
|
||
KeyboardButtonCode.K => 'K',
|
||
KeyboardButtonCode.L => 'L',
|
||
KeyboardButtonCode.M => 'M',
|
||
KeyboardButtonCode.N => 'N',
|
||
KeyboardButtonCode.O => 'O',
|
||
KeyboardButtonCode.P => 'P',
|
||
KeyboardButtonCode.Q => 'Q',
|
||
KeyboardButtonCode.R => 'R',
|
||
KeyboardButtonCode.S => 'S',
|
||
KeyboardButtonCode.T => 'T',
|
||
KeyboardButtonCode.U => 'U',
|
||
KeyboardButtonCode.V => 'V',
|
||
KeyboardButtonCode.W => 'W',
|
||
KeyboardButtonCode.X => 'X',
|
||
KeyboardButtonCode.Y => 'Y',
|
||
KeyboardButtonCode.Z => 'Z',
|
||
|
||
KeyboardButtonCode.D1 => '1',
|
||
KeyboardButtonCode.D2 => '2',
|
||
KeyboardButtonCode.D3 => '3',
|
||
KeyboardButtonCode.D4 => '4',
|
||
KeyboardButtonCode.D5 => '5',
|
||
KeyboardButtonCode.D6 => '6',
|
||
KeyboardButtonCode.D7 => '7',
|
||
KeyboardButtonCode.D8 => '8',
|
||
KeyboardButtonCode.D9 => '9',
|
||
KeyboardButtonCode.D0 => '0',
|
||
|
||
KeyboardButtonCode.Space => ' ',
|
||
_ => '\0'
|
||
};
|
||
}
|
||
|
||
/// <summary>
|
||
/// Gets the character representation of a keyboard button code in Russian.
|
||
/// </summary>
|
||
/// <param name="parKey">The keyboard button code to convert.</param>
|
||
/// <returns>The character representation of the key.</returns>
|
||
private static char GetRuChar(this KeyboardButtonCode parKey)
|
||
{
|
||
return parKey switch
|
||
{
|
||
KeyboardButtonCode.A => 'Ф',
|
||
KeyboardButtonCode.B => 'И',
|
||
KeyboardButtonCode.C => 'С',
|
||
KeyboardButtonCode.D => 'В',
|
||
KeyboardButtonCode.E => 'У',
|
||
KeyboardButtonCode.F => 'А',
|
||
KeyboardButtonCode.G => 'П',
|
||
KeyboardButtonCode.H => 'Р',
|
||
KeyboardButtonCode.I => 'Ш',
|
||
KeyboardButtonCode.J => 'О',
|
||
KeyboardButtonCode.K => 'Л',
|
||
KeyboardButtonCode.L => 'Д',
|
||
KeyboardButtonCode.M => 'Ь',
|
||
KeyboardButtonCode.N => 'Т',
|
||
KeyboardButtonCode.O => 'Щ',
|
||
KeyboardButtonCode.P => 'З',
|
||
KeyboardButtonCode.Q => 'Й',
|
||
KeyboardButtonCode.R => 'К',
|
||
KeyboardButtonCode.S => 'Ы',
|
||
KeyboardButtonCode.T => 'Е',
|
||
KeyboardButtonCode.U => 'Г',
|
||
KeyboardButtonCode.V => 'М',
|
||
KeyboardButtonCode.W => 'Ц',
|
||
KeyboardButtonCode.X => 'Ч',
|
||
KeyboardButtonCode.Y => 'Н',
|
||
KeyboardButtonCode.Z => 'Я',
|
||
|
||
KeyboardButtonCode.D1 => '1',
|
||
KeyboardButtonCode.D2 => '2',
|
||
KeyboardButtonCode.D3 => '3',
|
||
KeyboardButtonCode.D4 => '4',
|
||
KeyboardButtonCode.D5 => '5',
|
||
KeyboardButtonCode.D6 => '6',
|
||
KeyboardButtonCode.D7 => '7',
|
||
KeyboardButtonCode.D8 => '8',
|
||
KeyboardButtonCode.D9 => '9',
|
||
KeyboardButtonCode.D0 => '0',
|
||
|
||
KeyboardButtonCode.Space => ' ',
|
||
_ => '\0'
|
||
};
|
||
}
|
||
} |