-1

我可以在 C# WPF KeyEventArgs 中获取此处描述的扫描代码https://www.freepascal.org/docs-html/current/rtl/keyboard/kbdscancode.html吗?

4

1 回答 1

0

您可以为此使用 user32.dll 中的 MapVirtualKey

using System;
using System.Runtime.InteropServices;

public class Program
{
    private const uint MAPVK_VK_TO_VSC = 0;
    private const uint VK_F5 = 116; // https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.keys?view=net-5.0

    [DllImport("user32.dll",
        CallingConvention = CallingConvention.StdCall,
        CharSet = CharSet.Unicode,
        EntryPoint = "MapVirtualKey",
        SetLastError = true,
        ThrowOnUnmappableChar = false)]
    private static extern uint MapVirtualKey(uint uCode, uint uMapType);

    public static void Main()
    {
        var scanCodeForF5 = MapVirtualKey(VK_F5, MAPVK_VK_TO_VSC);
        Console.WriteLine(scanCodeForF5.ToString("X"));
        Console.ReadLine();
    }
}

不幸的是 dotnetfiddle 不允许运行上述代码,但它输出 3F。我相信VK_F5 将被替换(uint)KeyEventArgs.Key为您的情况。

编辑:枚举中的值似乎System.Windows.Input.Key与我的示例中来自命名空间的值不匹配System.Windows.Forms.Keys,因此上述代码将无法KeyEventArgs.Key直接运行。

编辑 2:您可以使用KeyInterop.VirtualKeyFromKeyfromSystem.Windows.Input命名空间将 from 转换System.Windows.Input.KeySystem.Windows.Forms.Keys.

因此,对于您的情况,这应该可行; var scanCodeForF5 = MapVirtualKey(KeyInterop.VirtualKeyFromKey(Key.F5), MAPVK_VK_TO_VSC);

于 2021-02-12T16:03:33.553 回答