自用贴,自定义功能对应的快捷键,组合键只添加了项目中需要的。
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// 指定功能配置快捷键
/// </summary>
public class InputKeyCode
{
// key=功能id,list<int>=键位id
public static Dictionary<int, List<int>> inputKeyDic = new Dictionary<int, List<int>>();
/// <summary>
/// 功能默认对应的快捷键
/// </summary>
public static void DefaultInputKeyDic()
{
inputKeyDic.Clear();
List<int> tempList = new List<int>();
tempList.Add((int)KeyCode.Q);
inputKeyDic.Add(0,tempList);
tempList = new List<int>();
tempList.Add((int)KeyCode.T);
inputKeyDic.Add(1, tempList);
tempList = new List<int>();
tempList.Add((int)KeyCode.E);
inputKeyDic.Add(2, tempList);
tempList = new List<int>();
tempList.Add((int)KeyCode.R);
inputKeyDic.Add(3, tempList);
tempList = new List<int>();
tempList.Add((int)KeyCode.W);
inputKeyDic.Add(4, tempList);
tempList = new List<int>();
tempList.Add((int)KeyCode.Z);
inputKeyDic.Add(5, tempList);
tempList = new List<int>();
tempList.Add((int)KeyCode.F1);
inputKeyDic.Add(6, tempList);
tempList = new List<int>();
tempList.Add((int)KeyCode.F);
inputKeyDic.Add(7, tempList);
tempList = new List<int>();
tempList.Add((int)KeyCode.LeftControl);
tempList.Add((int)KeyCode.Z);
inputKeyDic.Add(8, tempList);
tempList = new List<int>();
tempList.Add((int)KeyCode.LeftControl);
tempList.Add((int)KeyCode.Y);
inputKeyDic.Add(9, tempList);
tempList = new List<int>();
tempList.Add((int)KeyCode.LeftControl);
tempList.Add((int)KeyCode.A);
inputKeyDic.Add(10, tempList);
tempList = new List<int>();
tempList.Add((int)KeyCode.C);
inputKeyDic.Add(11, tempList);
tempList = new List<int>();
tempList.Add((int)KeyCode.LeftControl);
tempList.Add((int)KeyCode.S);
inputKeyDic.Add(12, tempList);
tempList = new List<int>();
tempList.Add((int)KeyCode.LeftControl);
tempList.Add((int)KeyCode.V);
inputKeyDic.Add(13, tempList);
tempList = new List<int>();
tempList.Add((int)KeyCode.D);
inputKeyDic.Add(14, tempList);
tempList = new List<int>();
tempList.Add((int)KeyCode.LeftControl);
tempList.Add((int)KeyCode.C);
inputKeyDic.Add(15, tempList);
}
/// <summary>
/// 保存功能对应的快捷键配置,偷懒使用了PlayerPrefs,可以重写成本地文件自己维护
/// </summary>
public static void SaveInputKeyDic()
{
FunctionType functionType = new FunctionType();
int count = Enum.GetValues(functionType.GetType()).Length;
Debug.Log("SaveInputKeyDic count=" + count);
for (int i = 0; i < count; i++)
{
string value = string.Empty;
for (int j = 0; j < inputKeyDic[i].Count; j++)
{
if (j == 1)
value += "&" + inputKeyDic[i][j];
else
value += inputKeyDic[i][j];
}
SetString("key" + i, value);
}
}
public static void LoadInputKeyDic()
{
FunctionType functionType = new FunctionType();
int count = Enum.GetValues(functionType.GetType()).Length;
Debug.Log("LoadInputKeyDic count=" + count);
for (int i = 0; i < count; i++)
{
string[] value = GetString("key" + i).Split('&');
List<int> list = new List<int>();
for (int j = 0; j < value.Length; j++)
{
if (!string.IsNullOrEmpty(value[j]))
list.Add(int.Parse(value[j]));
}
if (i == 6)
{
if (list.Count == 1 && list[0] == (int)KeyCode.LeftAlt)
{
list[0] = (int)KeyCode.F1;
}
}
if (list.Count > 0)
inputKeyDic[i] = list;
}
}
/// <summary>
/// 在update中调用,传入对应的功能id,返回对应的快捷键是否被按下
/// </summary>
/// <param name="functionType"></param>
/// <returns></returns>
public static bool GetKeyDownEvent(FunctionType functionType)
{
List<int> tempList = inputKeyDic[(int)functionType];
if (tempList.Count > 0)
{
if (tempList.Count > 1)
{
if ((KeyCode)tempList[0] == KeyCode.LeftAlt || (KeyCode)tempList[0] == KeyCode.RightAlt)
{
return GetAlt_Letter((KeyCode)tempList[1]);
}
else if ((KeyCode)tempList[0] == KeyCode.LeftControl || (KeyCode)tempList[0] == KeyCode.RightControl)
{
return GetControl_Letter((KeyCode)tempList[1]);
}
else if ((KeyCode)tempList[0] == KeyCode.LeftShift || (KeyCode)tempList[0] == KeyCode.RightShift)
{
return GetShift_Letter((KeyCode)tempList[1]);
}
else if ((KeyCode)tempList[0] == KeyCode.Space)
{
return GetSpace_Letter((KeyCode)tempList[1]);
}
}
else
{
KeyCode key = (KeyCode)tempList[0];
if (key == KeyCode.LeftAlt || key == KeyCode.RightAlt)
{
return GetAltDown();
}
if (key == KeyCode.LeftShift || key == KeyCode.RightShift)
{
return GetShiftDown();
}
if (key == KeyCode.LeftControl || key == KeyCode.RightControl)
{
return GetControlDown();
}
return !GetAltKey() && !GetShiftKey() && !GetControlKey() && Input.GetKeyDown(key);
}
}
return false;
}
//====================================================组合键==============================================
/// <summary>
/// 左右ctrl+key
/// </summary>
/// <param name="keyCode"></param>
/// <returns></returns>
public static bool GetControl_Letter(KeyCode keyCode)
{
if (GetControlKey() && Input.GetKeyDown(keyCode))
{
return true;
}
return false;
}
/// <summary>
/// 左右ctrl
/// </summary>
/// <returns></returns>
public static bool GetControlKey()
{
if (Input.GetKey(KeyCode.LeftControl)||Input.GetKey(KeyCode.RightControl))
{
return true;
}
return false;
}
/// <summary>
/// 左右alt+key
/// </summary>
/// <param name="keyCode"></param>
/// <returns></returns>
public static bool GetAlt_Letter(KeyCode keyCode)
{
if (GetAltKey() && Input.GetKeyDown(keyCode))
{
return true;
}
return false;
}
/// <summary>
/// 左右alt
/// </summary>
/// <returns></returns>
public static bool GetAltKey()
{
if (Input.GetKey(KeyCode.LeftAlt) || Input.GetKey(KeyCode.RightAlt))
{
return true;
}
return false;
}
/// <summary>
/// 左右shift+key
/// </summary>
/// <param name="keyCode"></param>
/// <returns></returns>
public static bool GetShift_Letter(KeyCode keyCode)
{
if (GetShiftKey() && Input.GetKeyDown(keyCode))
{
return true;
}
return false;
}
/// <summary>
/// 空格+key
/// </summary>
/// <param name="keyCode"></param>
/// <returns></returns>
public static bool GetSpace_Letter(KeyCode keyCode)
{
if (GetSpaceKey() && Input.GetKeyDown(keyCode))
{
return true;
}
return false;
}
/// <summary>
/// 左右shift
/// </summary>
/// <returns></returns>
public static bool GetShiftKey()
{
if (Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift))
{
return true;
}
return false;
}
public static bool GetSpaceKey()
{
if (Input.GetKey(KeyCode.Space))
{
return true;
}
return false;
}
/// <summary>
/// 回车键,包括小键盘的
/// </summary>
/// <returns></returns>
public static bool GetEnterKeyDown()
{
if (Input.GetKeyDown(KeyCode.KeypadEnter)|| Input.GetKeyDown(KeyCode.Return))
{
return true;
}
return false;
}
/// <summary>
/// tab按下
/// </summary>
/// <returns></returns>
public static bool GetTabKeyDown()
{
if (Input.GetKeyDown(KeyCode.Tab))
{
return true;
}
return false;
}
//====================================================组合键 END==============================================
public static bool GetAltDown()
{
if (Input.GetKeyDown(KeyCode.LeftAlt)|| Input.GetKeyDown(KeyCode.RightAlt))
{
return true;
}
return false;
}
public static bool GetShiftDown()
{
if (Input.GetKeyDown(KeyCode.LeftShift) || Input.GetKeyDown(KeyCode.RightShift))
{
return true;
}
return false;
}
public static bool GetControlDown()
{
if (Input.GetKeyDown(KeyCode.LeftControl) || Input.GetKeyDown(KeyCode.RightControl))
{
return true;
}
return false;
}
public static bool MouseRightDown()
{
return Input.GetMouseButtonDown(1);
}
public static bool MouseRightPress()
{
return Input.GetMouseButton(1);
}
public static bool MouseLeftDown()
{
return Input.GetMouseButtonDown(0);
}
public static bool MouseLeftPress()
{
return Input.GetMouseButton(0);
}
public static bool MouseMiddleDown()
{
return Input.GetMouseButtonDown(2);
}
public static bool MouseMiddlePress()
{
return Input.GetMouseButton(2);
}
public static bool GetUpArrowDown()
{
return Input.GetKeyDown(KeyCode.UpArrow);
}
public static bool GetUpArrow()
{
return Input.GetKey(KeyCode.UpArrow);
}
public static bool GetDownArrowDown()
{
return Input.GetKeyDown(KeyCode.DownArrow);
}
public static bool GetDownArrow()
{
return Input.GetKey(KeyCode.DownArrow);
}
public static bool GetLeftArrowDown()
{
return Input.GetKeyDown(KeyCode.LeftArrow);
}
public static bool GetLeftArrow()
{
return Input.GetKey(KeyCode.LeftArrow);
}
public static bool GetRightArrowDown()
{
return Input.GetKeyDown(KeyCode.RightArrow);
}
public static bool GetRightArrow()
{
return Input.GetKey(KeyCode.RightArrow);
}
//============================封装PlayerPrefs=============================================
public static bool HasKey(string keyName)
{
return PlayerPrefs.HasKey(keyName);
}
public static int GetInt(string keyName)
{
return PlayerPrefs.GetInt(keyName);
}
public static void SetInt(string keyName, int valueInt)
{
PlayerPrefs.SetInt(keyName, valueInt);
}
public static float GetFloat(string keyName)
{
return PlayerPrefs.GetFloat(keyName);
}
public static void SetFloat(string keyName, float valueFloat)
{
PlayerPrefs.SetFloat(keyName, valueFloat);
}
public static string GetString(string keyName, string defaultValue)
{
return PlayerPrefs.GetString(keyName, defaultValue);
}
public static string GetString(string keyName)
{
return PlayerPrefs.GetString(keyName);
}
public static void SetString(string keyName, string valueString)
{
PlayerPrefs.SetString(keyName, valueString);
}
public static void DeleteAllKey()
{
PlayerPrefs.DeleteAll();
}
public static void DeleteTheKey(string keyName)
{
PlayerPrefs.DeleteKey(keyName);
}
//============================封装PlayerPrefs END=============================================
}
/// <summary>
/// 快捷对应的功能类型
/// </summary>
public enum FunctionType
{
DrawPoint = 0,
DrawCircle = 1,
DrawCurve = 2,
DrawCurve2 = 3,
DrawLine = 4,
DrawLine2 = 5,
ShowDistance = 6,
CheckDistance = 7,
Revocation = 8,
Advance = 9,
SelectedAll = 10,
CancelDraw = 11,
Save = 12,
Paste = 13,
Delete = 14,
Copy = 15,
}