unity get key down int code example
Example: converting alpha1 into int unity
Dictionary<KeyCode, System.Action> keyCodeDic = new Dictionary<KeyCode, System.Action>();
void Start()
{
const int alphaStart = 48;
const int alphaEnd = 57;
int paramValue = 0;
for (int i = alphaStart; i <= alphaEnd; i++)
{
KeyCode tempKeyCode = (KeyCode)i;
int temParam = paramValue;
keyCodeDic.Add(tempKeyCode, () => MethodCall(temParam));
paramValue++;
}
}
void MethodCall(int keyNum)
{
Debug.Log("Pressed: " + keyNum);
}
void Update()
{
foreach (KeyValuePair<KeyCode, System.Action> entry in keyCodeDic)
{
if (Input.GetKeyDown(entry.Key))
{
if (keyCodeDic.ContainsKey(entry.Key))
{
keyCodeDic[entry.Key].Invoke();
}
}
}
}