So, I’m kind of stuck here. I want to ensure that when I click a mouse button, my keybinds reflect the correct button as well as keystroke. The keyboard functionality works fine, but when I press a button on the mouse, my button text changes to “None” instead of the assigned button. Im pretty sure the problem is around line 108 Is my syntax or logic incorrect?
using System.Collections;
using System.Linq;
using System.Collections.Generic;
using UnityEngine.UI;
using UnityEngine;
public class KeyBindScript : MonoBehaviour {
public bool AVMenuActive = true;
public bool GPMenuActive = false;
public bool ControlsMenuActive = false;
public Dictionary<string, KeyCode> Keys = new Dictionary<string, KeyCode> ();
private Dictionary<string, Text> ButTextLookup = new Dictionary<string, Text> ();
private GameObject currentKey;
public Text forward, backwards, movleft, movright, leanleft, leanright, run, jump, crouch, interact, attack, clairvoyance, journal, inventory, tasks, watch, quickwheel, pausemenu;
void Start ()
{
Keys.Add("Forward", KeyCode.W);
Keys.Add("Backwards", KeyCode.S);
Keys.Add("MovLeft", KeyCode.A);
Keys.Add("MovRight", KeyCode.D);
Keys.Add("LeanLeft", KeyCode.Q);
Keys.Add("LeanRight", KeyCode.E);
Keys.Add("Run", KeyCode.LeftShift);
Keys.Add("Jump", KeyCode.Space);
Keys.Add("Crouch", KeyCode.LeftControl);
Keys.Add("Interact", KeyCode.F);
Keys.Add("Attack", KeyCode.Mouse0);
Keys.Add("Clairvoyance", KeyCode.Mouse1);
Keys.Add("Journal", KeyCode.J);
Keys.Add("Inventory", KeyCode.I);
Keys.Add("Tasks", KeyCode.X);
Keys.Add("Watch", KeyCode.C);
Keys.Add("QuickWheel", KeyCode.Mouse3);
Keys.Add("PauseMenu", KeyCode.Escape);
ButTextLookup.Add ("Forward", forward);
ButTextLookup.Add("Backwards", backwards);
ButTextLookup.Add ("MovLeft", movleft);
ButTextLookup.Add ("MovRight", movright);
ButTextLookup.Add ("LeanLeft", leanleft);
ButTextLookup.Add ("LeanRight", leanright);
ButTextLookup.Add ("Run", run);
ButTextLookup.Add ("Jump", jump);
ButTextLookup.Add ("Crouch", crouch);
ButTextLookup.Add ("Interact", interact);
ButTextLookup.Add ("Attack", attack);
ButTextLookup.Add ("Clairvoyance", clairvoyance);
ButTextLookup.Add ("Journal", journal);
ButTextLookup.Add ("Inventory", inventory);
ButTextLookup.Add ("Tasks", tasks);
ButTextLookup.Add ("Watch", watch);
ButTextLookup.Add ("QuickWheel", quickwheel);
ButTextLookup.Add ("PauseMenu", pausemenu);
forward.text = Keys ["Forward"].ToString ();
backwards.text = Keys ["Backwards"].ToString ();
movleft.text = Keys ["MovLeft"].ToString ();
movright.text = Keys ["MovRight"].ToString ();
leanleft.text = Keys ["LeanLeft"].ToString ();
leanright.text = Keys ["LeanRight"].ToString ();
run.text = Keys ["Run"].ToString ();
jump.text = Keys ["Jump"].ToString ();
crouch.text = Keys ["Crouch"].ToString ();
interact.text = Keys ["Interact"].ToString ();
attack.text = Keys ["Attack"].ToString ();
clairvoyance.text = Keys ["Clairvoyance"].ToString ();
journal.text = Keys ["Journal"].ToString ();
inventory.text = Keys ["Inventory"].ToString ();
tasks.text = Keys ["Tasks"].ToString ();
watch.text = Keys ["Watch"].ToString ();
quickwheel.text = Keys ["QuickWheel"].ToString ();
pausemenu.text = Keys ["PauseMenu"].ToString ();
}
public void EnableAVMenu()
{
AVMenuActive = true;
GPMenuActive = false;
ControlsMenuActive = false;
}
public void EnableGPMenu()
{
AVMenuActive = false;
GPMenuActive = true;
ControlsMenuActive = false;
}
public void EnableControlsMenu()
{
AVMenuActive = false;
GPMenuActive = false;
ControlsMenuActive = true;
}
void OnGUI()
{
if (currentKey != null) {
Event e = Event.current;
if (e.isKey | e.isMouse){
Keys [currentKey.name] = (e.keyCode);
foreach (var kvp in Keys.ToList()) {
if (kvp.Value == e.keyCode) {
if (currentKey.name != kvp.Key) {
Keys [kvp.Key] = KeyCode.None;
ButTextLookup [kvp.Key].text = "*Unbound*";
}
}
}
currentKey.transform.GetChild(0).GetComponent<Text> ().text = e.keyCode.ToString ();
currentKey = null;
}
}
}
public void ChangeKey(GameObject clicked)
{
currentKey = clicked;
}
public void ResetKeys()
{
if (ControlsMenuActive == true)
{
Keys["Forward"] = KeyCode.W;
Keys["Backwards"] = KeyCode.S;
Keys["MovLeft"] = KeyCode.A;
Keys["MovRight"] = KeyCode.D;
Keys["LeanLeft"] = KeyCode.Q;
Keys["LeanRight"] = KeyCode.E;
Keys["Run"] = KeyCode.LeftShift;
Keys["Jump"] = KeyCode.Space;
Keys["Crouch"] = KeyCode.LeftControl;
Keys["Interact"] = KeyCode.F;
Keys["Attack"] = KeyCode.Mouse0;
Keys["Clairvoyance"] = KeyCode.Mouse1;
Keys["Journal"] = KeyCode.J;
Keys["Inventory"] = KeyCode.I;
Keys["Tasks"] = KeyCode.X;
Keys["Watch"] = KeyCode.C;
Keys["QuickWheel"] = KeyCode.Mouse3;
Keys["PauseMenu"] = KeyCode.Escape;
forward.text = Keys ["Forward"].ToString ();
backwards.text = Keys ["Backwards"].ToString ();
movleft.text = Keys ["MovLeft"].ToString ();
movright.text = Keys ["MovRight"].ToString ();
leanleft.text = Keys ["LeanLeft"].ToString ();
leanright.text = Keys ["LeanRight"].ToString ();
run.text = Keys ["Run"].ToString ();
jump.text = Keys ["Jump"].ToString ();
crouch.text = Keys ["Crouch"].ToString ();
interact.text = Keys ["Interact"].ToString ();
attack.text = Keys ["Attack"].ToString ();
clairvoyance.text = Keys ["Clairvoyance"].ToString ();
journal.text = Keys ["Journal"].ToString ();
inventory.text = Keys ["Inventory"].ToString ();
tasks.text = Keys ["Tasks"].ToString ();
watch.text = Keys ["Watch"].ToString ();
quickwheel.text = Keys ["QuickWheel"].ToString ();
pausemenu.text = Keys ["PauseMenu"].ToString ();
}
//public void ConsoleOutput()
//{
//foreach (KeyValuePair<string, KeyCode> kvp in Keys)
//Debug.Log ("Key = {0} + Value = {1}" + kvp.Key + kvp.Value);
}
}