[Unity3D Editor Development] Create a panel in Unity3D where you can view the KeyCode value corresponding to the keyboard at any time to facilitate development.

Recommended reading

Hello everyone, I am a Buddhist engineer ☆The Quiet Little Magic Dragon☆ . I update Unity development skills from time to time. If you find it useful, please remember to click three times.

I. Introduction

During development, you may encounter situations where you need to use the KeyCode value that monitors keyboard input to execute code.

For example:

using System;
using UnityEditor;
using UnityEngine;

public class Test01 : MonoBehaviour
{
    
    
    void Update()
    {
    
    
        if (Input.GetKeyDown(KeyCode.W))
        {
    
    
			 Debug.Log("点击了键盘W");
        }
    }
}

However, if there are some uncommon keys, such as {}[], these KeyCode values ​​are more difficult to view, because the API is like this: you don’t know what the English
Insert image description here
or numbers represent at all, so it was born. Unity makes a keyboard, and then marks the KeyCode value of each key under the keyboard keys to facilitate development.

Let’s take a look at the renderings first:
Insert image description here

Xiao Ming: The keys are not aligned, which makes me suffer from obsessive-compulsive disorder!
Zhang San: It’s not important! unimportant!

2. Text

2-1. Build keyboard key value table

Let's create a new script. It VirtualKeyboardEditor.csdoesn't matter what the name is. The main thing is to inherit EditorWindowthe class and put the script in the Editor folder.

Edit code:

using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;

public class VirtualKeyboardEditor : EditorWindow
{
    
    
    [MenuItem("工具/键盘映射值")]
    static void OpenWindow()
    {
    
    
        GetWindow<VirtualKeyboardEditor>().Show();
    }
    //用于绘制窗口内容
    private void OnGUI()
    {
    
    
    }
}

Then, we need to write a custom class to save the keyboard value name and KeyCode value, as well as the length and width.

// 键盘映射value值
public class VirKeyValue
{
    
    
    public float height;
    public float width;
    public string name;
    public string key;
    public VirKeyValue(string name, string key, float width, float height)
    {
    
    
        this.name = name;
        this.width = width;
        this.height = height;
        this.key = key;
    }
}

Then build the key-value library:

static Dictionary<int, List<VirKeyValue>> dicVirKeys;
    const int width1 = 50;
    const int width2 = 80;
    const int width100 = 100;
    const int width120 = 120;
    const int width140 = 138;
    const int width3 = 300;
    const int height1 = 30;

    private void DataInit()
    {
    
    
        if (dicVirKeys == null)
        {
    
    
            dicVirKeys = new Dictionary<int, List<VirKeyValue>>();
            List<VirKeyValue> tempVirKeys = new List<VirKeyValue>
            {
    
    
                new VirKeyValue("Esc","Esc", width1, height1),
                new VirKeyValue("F1","F1", width1, height1),
                new VirKeyValue("F2","F2", width1, height1),
                new VirKeyValue("F3","F3", width1, height1),
                new VirKeyValue("F4","F4", width1, height1),
                new VirKeyValue("F5","F5", width1, height1),
                new VirKeyValue("F6","F6", width1, height1),
                new VirKeyValue("F7","F7", width1, height1),
                new VirKeyValue("F8","F8", width1, height1),
                new VirKeyValue("F9","F9", width1, height1),
                new VirKeyValue("F10","F10", width1, height1),
                new VirKeyValue("F11","F11", width1, height1),
                new VirKeyValue("F12","F12", width1, height1),
                new VirKeyValue("Print\nScreen","Print", width1, height1),
                new VirKeyValue("Scroll\nLock", "ScrollLockScroll",width1, height1),
                new VirKeyValue("Pause","Pause", width1, height1)
            };
            dicVirKeys.Add(0, tempVirKeys);

            tempVirKeys = new List<VirKeyValue>
            {
    
    
                new VirKeyValue("~\n`","BackQuote", width1, height1),
                new VirKeyValue("!\n1","Alpha1", width1, height1),
                new VirKeyValue("@\n2","Alpha2", width1, height1),
                new VirKeyValue("#\n3","Alpha3", width1, height1),
                new VirKeyValue("$\n4","Alpha4", width1, height1),
                new VirKeyValue("%\n5","Alpha5", width1, height1),
                new VirKeyValue("^\n6","Alpha6", width1, height1),
                new VirKeyValue("&\n7","Alpha7", width1, height1),
                new VirKeyValue("*\n8","Alpha8", width1, height1),
                new VirKeyValue("(\n9","Alpha9", width1, height1),
                new VirKeyValue(")\n0","Alpha0", width1, height1),
                new VirKeyValue("_\n-","Minus", width1, height1),
                new VirKeyValue("+\n=","Equals", width1, height1),
                new VirKeyValue("←Backspace","Backspace", width120, height1),
                new VirKeyValue("Insert","Insert", width1, height1),
                new VirKeyValue("Home", "Home",width1, height1),
                new VirKeyValue("Page\nUp", "PageUp",width1, height1),
                new VirKeyValue("NumLk","Numlock", width1, height1),
                new VirKeyValue("/","KeypadDivide", width1, height1),
                new VirKeyValue("*","KeypadMultiply", width1, height1),
                new VirKeyValue("-","KeypadMinus", width1, height1),
            };
            dicVirKeys.Add(1, tempVirKeys);

            tempVirKeys = new List<VirKeyValue>
            {
    
    
                new VirKeyValue("Tab","Tab", width100, height1),
                new VirKeyValue("Q","Q",width1, height1),
                new VirKeyValue("W","W",width1, height1),
                new VirKeyValue("E","E",width1, height1),
                new VirKeyValue("R","R",width1, height1),
                new VirKeyValue("T","T",width1, height1),
                new VirKeyValue("Y","Y",width1, height1),
                new VirKeyValue("U","U",width1, height1),
                new VirKeyValue("I","I",width1, height1),
                new VirKeyValue("O","O",width1, height1),
                new VirKeyValue("P","P",width1, height1),
                new VirKeyValue("{\n[","LeftBracket", width1, height1),
                new VirKeyValue("}\n]","RightBracket", width1, height1),
                new VirKeyValue("|\n\\", "Backslash",70, height1),
                new VirKeyValue("Delete", "Delete",width1, height1),
                new VirKeyValue("End", "End",width1, height1),
                new VirKeyValue("Page\nDown","PageDown", width1, height1),
                new VirKeyValue("7","Keypad7",width1, height1),
                new VirKeyValue("8","Keypad8",width1, height1),
                new VirKeyValue("9","Keypad9",width1, height1),
                new VirKeyValue("+","KeypadPlus",width1, height1),
            };
            dicVirKeys.Add(2, tempVirKeys);

            tempVirKeys = new List<VirKeyValue>
            {
    
    
                new VirKeyValue("Caps\nLock","Backspace", width120, height1),
                new VirKeyValue("A","A", width1, height1),
                new VirKeyValue("S","S", width1, height1),
                new VirKeyValue("D","D", width1, height1),
                new VirKeyValue("F","F", width1, height1),
                new VirKeyValue("G","G", width1, height1),
                new VirKeyValue("H","H", width1, height1),
                new VirKeyValue("J","J", width1, height1),
                new VirKeyValue("K","K", width1, height1),
                new VirKeyValue("L","L", width1, height1),
                new VirKeyValue(":\n;","Semicolon", width1, height1),
                new VirKeyValue("\"\n'","Quote", width1, height1),
                new VirKeyValue("Enter","Enter", 104, height1),
                new VirKeyValue("4","Keypad4",width1, height1),
                new VirKeyValue("5","Keypad5",width1, height1),
                new VirKeyValue("6","Keypad6",width1, height1),
                new VirKeyValue("Enter","KeypadEnter", width1, height1),
            };
            dicVirKeys.Add(3, tempVirKeys);

            tempVirKeys = new List<VirKeyValue>
            {
    
    
                new VirKeyValue("Shift","LeftShift", width140, height1),
                new VirKeyValue("Z","Z",width1, height1),
                new VirKeyValue("X","X",width1, height1),
                new VirKeyValue("C","C",width1, height1),
                new VirKeyValue("V","V",width1, height1),
                new VirKeyValue("B","B",width1, height1),
                new VirKeyValue("N","N",width1, height1),
                new VirKeyValue("M","M",width1, height1),
                new VirKeyValue("<\n,","Comma", width1, height1),
                new VirKeyValue(">\n.","Period", width1, height1),
                new VirKeyValue("?\n/","Slash", width1, height1),
                new VirKeyValue("Shift","RightControl", width140, height1),
                new VirKeyValue("↑","UpArrow", width1, height1),
                new VirKeyValue("1","Keypad1", width1, height1),
                new VirKeyValue("2","Keypad2", width1, height1),
                new VirKeyValue("3","Keypad3", width1, height1),
            };
            dicVirKeys.Add(4, tempVirKeys);

            tempVirKeys = new List<VirKeyValue>
            {
    
    
                new VirKeyValue("Ctrl","LeftControl", width2, height1),
                new VirKeyValue("Win", "LeftWindows",width1, height1),
                new VirKeyValue("Alt", "LeftAlt",width1, height1),
                new VirKeyValue("—————Space———","Space", width3, height1),
                new VirKeyValue("Alt", "RightAlt",width1, height1),
                new VirKeyValue("Ctrl", "RightControl",width2, height1),
                new VirKeyValue("←","LeftArrow",width1, height1),
                new VirKeyValue("↓","DownArrow",width1, height1),
                new VirKeyValue("→","RightArrow",width1, height1),
                new VirKeyValue("0","Keypad0",width100, height1),
                new VirKeyValue(".","KeypadPeriod",width1, height1),
            };
            dicVirKeys.Add(5, tempVirKeys);
        }
    }

The overall code is as follows:

using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;

// 键盘映射value值
public class VirKeyValue
{
    
    
    public float height;
    public float width;
    public string name;
    public string key;
    public VirKeyValue(string name, string key, float width, float height)
    {
    
    
        this.name = name;
        this.width = width;
        this.height = height;
        this.key = key;
    }
}
public class VirtualKeyboardEditor : EditorWindow
{
    
    
    [MenuItem("工具/键盘映射值")]
    static void OpenWindow()
    {
    
    
        GetWindow<VirtualKeyboardEditor>().Show();
    }

    static Dictionary<int, List<VirKeyValue>> dicVirKeys;
    const int width1 = 50;
    const int width2 = 80;
    const int width100 = 100;
    const int width120 = 120;
    const int width140 = 138;
    const int width3 = 300;
    const int height1 = 30;

    private void DataInit()
    {
    
    
        if (dicVirKeys == null)
        {
    
    
            dicVirKeys = new Dictionary<int, List<VirKeyValue>>();
            List<VirKeyValue> tempVirKeys = new List<VirKeyValue>
            {
    
    
                new VirKeyValue("Esc","Esc", width1, height1),
                new VirKeyValue("F1","F1", width1, height1),
                new VirKeyValue("F2","F2", width1, height1),
                new VirKeyValue("F3","F3", width1, height1),
                new VirKeyValue("F4","F4", width1, height1),
                new VirKeyValue("F5","F5", width1, height1),
                new VirKeyValue("F6","F6", width1, height1),
                new VirKeyValue("F7","F7", width1, height1),
                new VirKeyValue("F8","F8", width1, height1),
                new VirKeyValue("F9","F9", width1, height1),
                new VirKeyValue("F10","F10", width1, height1),
                new VirKeyValue("F11","F11", width1, height1),
                new VirKeyValue("F12","F12", width1, height1),
                new VirKeyValue("Print\nScreen","Print", width1, height1),
                new VirKeyValue("Scroll\nLock", "ScrollLockScroll",width1, height1),
                new VirKeyValue("Pause","Pause", width1, height1)
            };
            dicVirKeys.Add(0, tempVirKeys);

            tempVirKeys = new List<VirKeyValue>
            {
    
    
                new VirKeyValue("~\n`","BackQuote", width1, height1),
                new VirKeyValue("!\n1","Alpha1", width1, height1),
                new VirKeyValue("@\n2","Alpha2", width1, height1),
                new VirKeyValue("#\n3","Alpha3", width1, height1),
                new VirKeyValue("$\n4","Alpha4", width1, height1),
                new VirKeyValue("%\n5","Alpha5", width1, height1),
                new VirKeyValue("^\n6","Alpha6", width1, height1),
                new VirKeyValue("&\n7","Alpha7", width1, height1),
                new VirKeyValue("*\n8","Alpha8", width1, height1),
                new VirKeyValue("(\n9","Alpha9", width1, height1),
                new VirKeyValue(")\n0","Alpha0", width1, height1),
                new VirKeyValue("_\n-","Minus", width1, height1),
                new VirKeyValue("+\n=","Equals", width1, height1),
                new VirKeyValue("←Backspace","Backspace", width120, height1),
                new VirKeyValue("Insert","Insert", width1, height1),
                new VirKeyValue("Home", "Home",width1, height1),
                new VirKeyValue("Page\nUp", "PageUp",width1, height1),
                new VirKeyValue("NumLk","Numlock", width1, height1),
                new VirKeyValue("/","KeypadDivide", width1, height1),
                new VirKeyValue("*","KeypadMultiply", width1, height1),
                new VirKeyValue("-","KeypadMinus", width1, height1),
            };
            dicVirKeys.Add(1, tempVirKeys);

            tempVirKeys = new List<VirKeyValue>
            {
    
    
                new VirKeyValue("Tab","Tab", width100, height1),
                new VirKeyValue("Q","Q",width1, height1),
                new VirKeyValue("W","W",width1, height1),
                new VirKeyValue("E","E",width1, height1),
                new VirKeyValue("R","R",width1, height1),
                new VirKeyValue("T","T",width1, height1),
                new VirKeyValue("Y","Y",width1, height1),
                new VirKeyValue("U","U",width1, height1),
                new VirKeyValue("I","I",width1, height1),
                new VirKeyValue("O","O",width1, height1),
                new VirKeyValue("P","P",width1, height1),
                new VirKeyValue("{\n[","LeftBracket", width1, height1),
                new VirKeyValue("}\n]","RightBracket", width1, height1),
                new VirKeyValue("|\n\\", "Backslash",70, height1),
                new VirKeyValue("Delete", "Delete",width1, height1),
                new VirKeyValue("End", "End",width1, height1),
                new VirKeyValue("Page\nDown","PageDown", width1, height1),
                new VirKeyValue("7","Keypad7",width1, height1),
                new VirKeyValue("8","Keypad8",width1, height1),
                new VirKeyValue("9","Keypad9",width1, height1),
                new VirKeyValue("+","KeypadPlus",width1, height1),
            };
            dicVirKeys.Add(2, tempVirKeys);

            tempVirKeys = new List<VirKeyValue>
            {
    
    
                new VirKeyValue("Caps\nLock","Backspace", width120, height1),
                new VirKeyValue("A","A", width1, height1),
                new VirKeyValue("S","S", width1, height1),
                new VirKeyValue("D","D", width1, height1),
                new VirKeyValue("F","F", width1, height1),
                new VirKeyValue("G","G", width1, height1),
                new VirKeyValue("H","H", width1, height1),
                new VirKeyValue("J","J", width1, height1),
                new VirKeyValue("K","K", width1, height1),
                new VirKeyValue("L","L", width1, height1),
                new VirKeyValue(":\n;","Semicolon", width1, height1),
                new VirKeyValue("\"\n'","Quote", width1, height1),
                new VirKeyValue("Enter","Enter", 104, height1),
                new VirKeyValue("4","Keypad4",width1, height1),
                new VirKeyValue("5","Keypad5",width1, height1),
                new VirKeyValue("6","Keypad6",width1, height1),
                new VirKeyValue("Enter","KeypadEnter", width1, height1),
            };
            dicVirKeys.Add(3, tempVirKeys);

            tempVirKeys = new List<VirKeyValue>
            {
    
    
                new VirKeyValue("Shift","LeftShift", width140, height1),
                new VirKeyValue("Z","Z",width1, height1),
                new VirKeyValue("X","X",width1, height1),
                new VirKeyValue("C","C",width1, height1),
                new VirKeyValue("V","V",width1, height1),
                new VirKeyValue("B","B",width1, height1),
                new VirKeyValue("N","N",width1, height1),
                new VirKeyValue("M","M",width1, height1),
                new VirKeyValue("<\n,","Comma", width1, height1),
                new VirKeyValue(">\n.","Period", width1, height1),
                new VirKeyValue("?\n/","Slash", width1, height1),
                new VirKeyValue("Shift","RightControl", width140, height1),
                new VirKeyValue("↑","UpArrow", width1, height1),
                new VirKeyValue("1","Keypad1", width1, height1),
                new VirKeyValue("2","Keypad2", width1, height1),
                new VirKeyValue("3","Keypad3", width1, height1),
            };
            dicVirKeys.Add(4, tempVirKeys);

            tempVirKeys = new List<VirKeyValue>
            {
    
    
                new VirKeyValue("Ctrl","LeftControl", width2, height1),
                new VirKeyValue("Win", "LeftWindows",width1, height1),
                new VirKeyValue("Alt", "LeftAlt",width1, height1),
                new VirKeyValue("—————Space———","Space", width3, height1),
                new VirKeyValue("Alt", "RightAlt",width1, height1),
                new VirKeyValue("Ctrl", "RightControl",width2, height1),
                new VirKeyValue("←","LeftArrow",width1, height1),
                new VirKeyValue("↓","DownArrow",width1, height1),
                new VirKeyValue("→","RightArrow",width1, height1),
                new VirKeyValue("0","Keypad0",width100, height1),
                new VirKeyValue(".","KeypadPeriod",width1, height1),
            };
            dicVirKeys.Add(5, tempVirKeys);
        }
    }
    //用于绘制窗口内容
    private void OnGUI()
    {
    
    
    }
}

2-2. Interface drawing

using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;

// 键盘映射value值
public class VirKeyValue
{
    
    
    public float height;
    public float width;
    public string name;
    public string key;
    public VirKeyValue(string name, string key, float width, float height)
    {
    
    
        this.name = name;
        this.width = width;
        this.height = height;
        this.key = key;
    }
}
public class VirtualKeyboardEditor : EditorWindow
{
    
    
    [MenuItem("工具/键盘映射值")]
    static void OpenWindow()
    {
    
    
        GetWindow<VirtualKeyboardEditor>().Show();
    }

    static Dictionary<int, List<VirKeyValue>> dicVirKeys;
    const int width1 = 50;
    const int width2 = 80;
    const int width100 = 100;
    const int width120 = 120;
    const int width140 = 138;
    const int width3 = 300;
    const int height1 = 30;

    private void DataInit()
    {
    
    
        if (dicVirKeys == null)
        {
    
    
            dicVirKeys = new Dictionary<int, List<VirKeyValue>>();
            List<VirKeyValue> tempVirKeys = new List<VirKeyValue>
            {
    
    
                new VirKeyValue("Esc","Esc", width1, height1),
                new VirKeyValue("F1","F1", width1, height1),
                new VirKeyValue("F2","F2", width1, height1),
                new VirKeyValue("F3","F3", width1, height1),
                new VirKeyValue("F4","F4", width1, height1),
                new VirKeyValue("F5","F5", width1, height1),
                new VirKeyValue("F6","F6", width1, height1),
                new VirKeyValue("F7","F7", width1, height1),
                new VirKeyValue("F8","F8", width1, height1),
                new VirKeyValue("F9","F9", width1, height1),
                new VirKeyValue("F10","F10", width1, height1),
                new VirKeyValue("F11","F11", width1, height1),
                new VirKeyValue("F12","F12", width1, height1),
                new VirKeyValue("Print\nScreen","Print", width1, height1),
                new VirKeyValue("Scroll\nLock", "ScrollLockScroll",width1, height1),
                new VirKeyValue("Pause","Pause", width1, height1)
            };
            dicVirKeys.Add(0, tempVirKeys);

            tempVirKeys = new List<VirKeyValue>
            {
    
    
                new VirKeyValue("~\n`","BackQuote", width1, height1),
                new VirKeyValue("!\n1","Alpha1", width1, height1),
                new VirKeyValue("@\n2","Alpha2", width1, height1),
                new VirKeyValue("#\n3","Alpha3", width1, height1),
                new VirKeyValue("$\n4","Alpha4", width1, height1),
                new VirKeyValue("%\n5","Alpha5", width1, height1),
                new VirKeyValue("^\n6","Alpha6", width1, height1),
                new VirKeyValue("&\n7","Alpha7", width1, height1),
                new VirKeyValue("*\n8","Alpha8", width1, height1),
                new VirKeyValue("(\n9","Alpha9", width1, height1),
                new VirKeyValue(")\n0","Alpha0", width1, height1),
                new VirKeyValue("_\n-","Minus", width1, height1),
                new VirKeyValue("+\n=","Equals", width1, height1),
                new VirKeyValue("←Backspace","Backspace", width120, height1),
                new VirKeyValue("Insert","Insert", width1, height1),
                new VirKeyValue("Home", "Home",width1, height1),
                new VirKeyValue("Page\nUp", "PageUp",width1, height1),
                new VirKeyValue("NumLk","Numlock", width1, height1),
                new VirKeyValue("/","KeypadDivide", width1, height1),
                new VirKeyValue("*","KeypadMultiply", width1, height1),
                new VirKeyValue("-","KeypadMinus", width1, height1),
            };
            dicVirKeys.Add(1, tempVirKeys);

            tempVirKeys = new List<VirKeyValue>
            {
    
    
                new VirKeyValue("Tab","Tab", width100, height1),
                new VirKeyValue("Q","Q",width1, height1),
                new VirKeyValue("W","W",width1, height1),
                new VirKeyValue("E","E",width1, height1),
                new VirKeyValue("R","R",width1, height1),
                new VirKeyValue("T","T",width1, height1),
                new VirKeyValue("Y","Y",width1, height1),
                new VirKeyValue("U","U",width1, height1),
                new VirKeyValue("I","I",width1, height1),
                new VirKeyValue("O","O",width1, height1),
                new VirKeyValue("P","P",width1, height1),
                new VirKeyValue("{\n[","LeftBracket", width1, height1),
                new VirKeyValue("}\n]","RightBracket", width1, height1),
                new VirKeyValue("|\n\\", "Backslash",70, height1),
                new VirKeyValue("Delete", "Delete",width1, height1),
                new VirKeyValue("End", "End",width1, height1),
                new VirKeyValue("Page\nDown","PageDown", width1, height1),
                new VirKeyValue("7","Keypad7",width1, height1),
                new VirKeyValue("8","Keypad8",width1, height1),
                new VirKeyValue("9","Keypad9",width1, height1),
                new VirKeyValue("+","KeypadPlus",width1, height1),
            };
            dicVirKeys.Add(2, tempVirKeys);

            tempVirKeys = new List<VirKeyValue>
            {
    
    
                new VirKeyValue("Caps\nLock","Backspace", width120, height1),
                new VirKeyValue("A","A", width1, height1),
                new VirKeyValue("S","S", width1, height1),
                new VirKeyValue("D","D", width1, height1),
                new VirKeyValue("F","F", width1, height1),
                new VirKeyValue("G","G", width1, height1),
                new VirKeyValue("H","H", width1, height1),
                new VirKeyValue("J","J", width1, height1),
                new VirKeyValue("K","K", width1, height1),
                new VirKeyValue("L","L", width1, height1),
                new VirKeyValue(":\n;","Semicolon", width1, height1),
                new VirKeyValue("\"\n'","Quote", width1, height1),
                new VirKeyValue("Enter","Enter", 104, height1),
                new VirKeyValue("4","Keypad4",width1, height1),
                new VirKeyValue("5","Keypad5",width1, height1),
                new VirKeyValue("6","Keypad6",width1, height1),
                new VirKeyValue("Enter","KeypadEnter", width1, height1),
            };
            dicVirKeys.Add(3, tempVirKeys);

            tempVirKeys = new List<VirKeyValue>
            {
    
    
                new VirKeyValue("Shift","LeftShift", width140, height1),
                new VirKeyValue("Z","Z",width1, height1),
                new VirKeyValue("X","X",width1, height1),
                new VirKeyValue("C","C",width1, height1),
                new VirKeyValue("V","V",width1, height1),
                new VirKeyValue("B","B",width1, height1),
                new VirKeyValue("N","N",width1, height1),
                new VirKeyValue("M","M",width1, height1),
                new VirKeyValue("<\n,","Comma", width1, height1),
                new VirKeyValue(">\n.","Period", width1, height1),
                new VirKeyValue("?\n/","Slash", width1, height1),
                new VirKeyValue("Shift","RightControl", width140, height1),
                new VirKeyValue("↑","UpArrow", width1, height1),
                new VirKeyValue("1","Keypad1", width1, height1),
                new VirKeyValue("2","Keypad2", width1, height1),
                new VirKeyValue("3","Keypad3", width1, height1),
            };
            dicVirKeys.Add(4, tempVirKeys);

            tempVirKeys = new List<VirKeyValue>
            {
    
    
                new VirKeyValue("Ctrl","LeftControl", width2, height1),
                new VirKeyValue("Win", "LeftWindows",width1, height1),
                new VirKeyValue("Alt", "LeftAlt",width1, height1),
                new VirKeyValue("—————Space———","Space", width3, height1),
                new VirKeyValue("Alt", "RightAlt",width1, height1),
                new VirKeyValue("Ctrl", "RightControl",width2, height1),
                new VirKeyValue("←","LeftArrow",width1, height1),
                new VirKeyValue("↓","DownArrow",width1, height1),
                new VirKeyValue("→","RightArrow",width1, height1),
                new VirKeyValue("0","Keypad0",width100, height1),
                new VirKeyValue(".","KeypadPeriod",width1, height1),
            };
            dicVirKeys.Add(5, tempVirKeys);
        }
    }
    //用于绘制窗口内容
    private void OnGUI()
    {
    
    
        DataInit();
        GUILayout.Label("Note:在开发中会遇到使用监控键盘输入的情况,但是KeyCode的值往往分不清楚,此工具就是跟键盘一一对应的Key值,不清楚的时候" +
            "看一眼就行了,当然,也可以点击按钮,点击后可以打印当前KeyCode值。");
        for (int i = 0; i < dicVirKeys.Count; i++)
        {
    
    
            GUILayout.BeginVertical();
            OnRow(i);
            GUILayout.EndVertical();
        }
    }

    void OnRow(int index)
    {
    
    
        GUILayout.BeginHorizontal();
        for (int i = 0; i < dicVirKeys[index].Count; i++)
        {
    
    
            if (dicVirKeys[index][i].name == "↑")
            {
    
    
                GUILayout.Space(50);
            }
            else if (dicVirKeys[index][i].name == "←")
            {
    
    
                GUILayout.Space(180);
            }
            else if (dicVirKeys[index][i].name == "4")
            {
    
    
                GUILayout.Space(160);
            }
            else if (dicVirKeys[index][i].name == "1")
            {
    
    
                GUILayout.Space(60);
            }
            else if (dicVirKeys[index][i].name == "0")
            {
    
    
                GUILayout.Space(6);
            }
            GUILayout.BeginVertical();

            if (GUILayout.Button(dicVirKeys[index][i].name, GUILayout.Width(dicVirKeys[index][i].width), GUILayout.Height(dicVirKeys[index][i].height)))
            {
    
    
                Debug.Log("当前按下的键位是 : KeyCode." + dicVirKeys[index][i].key);
            }
            GUILayout.Label(dicVirKeys[index][i].key);
            GUILayout.EndVertical();
            if (dicVirKeys[index][i].name == "Esc")
            {
    
    
                GUILayout.Space(52);
            }
            else if (dicVirKeys[index][i].name == "F4")
            {
    
    
                GUILayout.Space(36);
            }
            else if (dicVirKeys[index][i].name == "F8")
            {
    
    
                GUILayout.Space(36);
            }
        }
        GUILayout.EndHorizontal();
    }
}

Then in the Edit bar of the Untiy editor, choose 工具→键盘映射值to open the panel:
Insert image description here

3. Postscript

If you find this article useful, don't forget to click "Follow" to avoid getting lost and continue to share more useful articles about Unity.


Your likes are support for the blogger. If you have any questions, please leave a message:

The blogger’s homepage has contact information.

The blogger also has many treasure articles waiting for you to discover:

Column direction Introduction
Unity3D develops small games Mini game development tutorial Share some small games developed using the Unity3D engine and share some tutorials on making small games.
Unity3D from entry to advanced getting Started Get inspiration from self-study Unity, summarize the route of learning Unity from scratch, and have knowledge of C# and Unity.
Unity3D UGUI UGUI A full analysis of Unity's UI system UGUI, starting from the basic controls of UGUI, and then comprehensively teaching the principles and use of UGUI.
Reading data in Unity3D file reading Use Unity3D to read txt documents, json documents, xml documents, csv documents, and Excel documents.
Unity3D data collection data collection Array collection: Knowledge sharing on data collections such as arrays, Lists, dictionaries, stacks, and linked lists.
Unity3D VR/AR (virtual simulation) development virtual reality Summarize the common virtual simulation needs of bloggers and explain them with cases.
Unity3D plug-in plug-in Mainly share some plug-in usage methods used in Unity development, plug-in introduction, etc.
Daily development of Unity3D daily records Mainly used by bloggers in daily development, including methods and techniques, development ideas, code sharing, etc.
Unity3D daily BUG daily records Record the bugs and pitfalls encountered during project development using the Unity3D editor so that future generations can have some reference.

Guess you like

Origin blog.csdn.net/q764424567/article/details/133708561