XFE Git
XFE Studio Git
Git 首页 全局搜索
XFE 主站 文档 NuGet

XFESystem

【Rust】XFE系统,基于Rust和汇编

公开
关注 0 Fork 0 Star 0
UTF-8
using XFESystem.Desktop;
using XFESystem.NativeKernel;

namespace XFESystem.NativeRuntime.ConsoleApplication;

public readonly record struct ConsoleKey(TerminalShellKey Key, string? Text = null);

/// <summary>PS/2 keyboard translation for the console; window-manager and browser shortcuts do not exist here.</summary>
public static class ConsoleKeyboard
{
    public static ConsoleKey? Translate(NativeInputReport report)
    {
        if (report.Kind != Ps2Decoder.KeyDown) return null;
        if ((report.Modifiers & (Ps2Decoder.Alt | Ps2Decoder.Super)) != 0) return null;
        if ((report.Modifiers & Ps2Decoder.Control) != 0)
            return report.KeyCode == 0x2E ? new(TerminalShellKey.Cancel) : null;
        if (report.Unicode is >= 32 and <= 0x7E) return new(TerminalShellKey.Character, ((char)report.Unicode).ToString());
        TerminalShellKey? key = report.KeyCode switch
        {
            0x1C or 0x11C => TerminalShellKey.Enter, 0x0E => TerminalShellKey.Backspace,
            0x53 or 0x153 => TerminalShellKey.Delete, 0x4B or 0x14B => TerminalShellKey.Left,
            0x4D or 0x14D => TerminalShellKey.Right, 0x48 or 0x148 => TerminalShellKey.Up,
            0x50 or 0x150 => TerminalShellKey.Down, 0x47 or 0x147 => TerminalShellKey.Home,
            0x4F or 0x14F => TerminalShellKey.End, 0x52 or 0x152 => TerminalShellKey.Insert,
            0x01 => TerminalShellKey.Cancel, _ => null
        };
        return key is { } mapped ? new(mapped) : null;
    }
}