Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

Mouse click not returning any keycode?!

Discussion in 'Scripting' started by Abuscaglio, Apr 8, 2018.

  1. Abuscaglio

    Abuscaglio

    Joined:
    Sep 5, 2017
    Posts:
    46
    I want to ensure that when I click a mouse button, my keybinds reflect the correct keycode. The keyboard functionality works fine, but when I press a button on the mouse, my button text changes to "None" instead of the keycode. Anyone know what is going on??

    Code (CSharp):
    1. using System.Collections;
    2. using System.Linq;
    3. using System.Collections.Generic;
    4. using UnityEngine.UI;
    5. using UnityEngine;
    6.  
    7. public class KeyBindScript : MonoBehaviour {
    8.  
    9.     public bool AVMenuActive = true;
    10.  
    11.     public bool GPMenuActive = false;
    12.  
    13.     public bool ControlsMenuActive = false;
    14.  
    15.     public Button[] InactiveButtons = new Button[23];
    16.  
    17.     public Dictionary<string, KeyCode> Keys = new Dictionary<string, KeyCode> ();
    18.  
    19.     private Dictionary<string, Text> ButTextLookup = new Dictionary<string, Text> ();
    20.  
    21.     public GameObject RCPanel;
    22.  
    23.     private GameObject currentKey;
    24.  
    25.     public Text forward, backwards, movleft, movright, leanleft, leanright, run, jump, crouch, interact, attack, clairvoyance, journal, inventory, tasks, watch, quickwheel, pausemenu;  
    26.  
    27.     void Start ()
    28.     {
    29.         Keys.Add("Forward", KeyCode.W);
    30.         Keys.Add("Backwards", KeyCode.S);
    31.         Keys.Add("MovLeft", KeyCode.A);
    32.         Keys.Add("MovRight", KeyCode.D);
    33.         Keys.Add("LeanLeft", KeyCode.Q);
    34.         Keys.Add("LeanRight", KeyCode.E);
    35.         Keys.Add("Run", KeyCode.LeftShift);
    36.         Keys.Add("Jump", KeyCode.Space);
    37.         Keys.Add("Crouch", KeyCode.LeftControl);
    38.         Keys.Add("Interact", KeyCode.F);
    39.         Keys.Add("Attack", KeyCode.Mouse0);
    40.         Keys.Add("Clairvoyance", KeyCode.Mouse1);
    41.         Keys.Add("Journal", KeyCode.J);
    42.         Keys.Add("Inventory", KeyCode.I);
    43.         Keys.Add("Tasks", KeyCode.X);
    44.         Keys.Add("Watch", KeyCode.C);
    45.         Keys.Add("QuickWheel", KeyCode.Mouse3);
    46.         Keys.Add("PauseMenu", KeyCode.Escape);
    47.         ButTextLookup.Add ("Forward", forward);
    48.         ButTextLookup.Add("Backwards", backwards);
    49.         ButTextLookup.Add ("MovLeft", movleft);
    50.         ButTextLookup.Add ("MovRight", movright);
    51.         ButTextLookup.Add ("LeanLeft", leanleft);
    52.         ButTextLookup.Add ("LeanRight", leanright);
    53.         ButTextLookup.Add ("Run", run);
    54.         ButTextLookup.Add ("Jump", jump);
    55.         ButTextLookup.Add ("Crouch", crouch);
    56.         ButTextLookup.Add ("Interact", interact);
    57.         ButTextLookup.Add ("Attack", attack);
    58.         ButTextLookup.Add ("Clairvoyance", clairvoyance);
    59.         ButTextLookup.Add ("Journal", journal);
    60.         ButTextLookup.Add ("Inventory", inventory);
    61.         ButTextLookup.Add ("Tasks", tasks);
    62.         ButTextLookup.Add ("Watch", watch);
    63.         ButTextLookup.Add ("QuickWheel", quickwheel);
    64.         ButTextLookup.Add ("PauseMenu", pausemenu);
    65.  
    66.         forward.text = Keys ["Forward"].ToString ();
    67.         backwards.text = Keys ["Backwards"].ToString ();
    68.         movleft.text = Keys ["MovLeft"].ToString ();
    69.         movright.text = Keys ["MovRight"].ToString ();
    70.         leanleft.text = Keys ["LeanLeft"].ToString ();
    71.         leanright.text = Keys ["LeanRight"].ToString ();
    72.         run.text = Keys ["Run"].ToString ();
    73.         jump.text = Keys ["Jump"].ToString ();
    74.         crouch.text = Keys ["Crouch"].ToString ();
    75.         interact.text = Keys ["Interact"].ToString ();
    76.         attack.text = Keys ["Attack"].ToString ();
    77.         clairvoyance.text = Keys ["Clairvoyance"].ToString ();
    78.         journal.text = Keys ["Journal"].ToString ();
    79.         inventory.text = Keys ["Inventory"].ToString ();
    80.         tasks.text = Keys ["Tasks"].ToString ();
    81.         watch.text = Keys ["Watch"].ToString ();
    82.         quickwheel.text = Keys ["QuickWheel"].ToString ();
    83.         pausemenu.text = Keys ["PauseMenu"].ToString ();
    84.     }
    85.  
    86.     public void EnableAVMenu()
    87.     {
    88.         AVMenuActive = true;
    89.         GPMenuActive = false;
    90.         ControlsMenuActive = false;
    91.     }
    92.  
    93.     public void EnableGPMenu()
    94.     {
    95.         AVMenuActive = false;
    96.         GPMenuActive = true;
    97.         ControlsMenuActive = false;
    98.     }
    99.  
    100.     public void EnableControlsMenu()
    101.     {
    102.         AVMenuActive = false;
    103.         GPMenuActive = false;
    104.         ControlsMenuActive = true;
    105.     }
    106.        
    107.  
    108.     void OnGUI()
    109.     {
    110.         if (currentKey != null) {
    111.             RCPanel.SetActive (true);
    112.             //foreach (Button button in InactiveButtons) {
    113.                 //button.interactable = false;
    114.             Event e = Event.current;
    115.             if (e.isKey | e.isMouse) {
    116.                 Keys [currentKey.name] = (e.keyCode);
    117.                 foreach (var kvp in Keys.ToList()) {
    118.                     if (kvp.Value == e.keyCode) {
    119.                         if (currentKey.name != kvp.Key) {
    120.                             Keys [kvp.Key] = KeyCode.None;
    121.                             ButTextLookup [kvp.Key].text = "*Unbound*";
    122.                         }
    123.                     }
    124.                 }
    125.                
    126.                 currentKey.transform.GetChild (0).GetComponent<Text> ().text = e.keyCode.ToString ();
    127.                 currentKey = null;
    128.  
    129.                 System.Threading.Thread.Sleep(250);
    130.  
    131.                 RCPanel.SetActive (false);
    132.                 foreach (Button button in InactiveButtons) {
    133.                     button.interactable = true;
    134.                 }
    135.             }
    136.            
    137.         }
    138.     }
    139.  
    140.     public void ChangeKey(GameObject clicked)
    141.     {
    142.             currentKey = clicked;
    143.     }
    144.  
    145.     public void ResetKeys()
    146.     {
    147.         if (ControlsMenuActive == true) {
    148.             Keys ["Forward"] = KeyCode.W;
    149.             Keys ["Backwards"] = KeyCode.S;
    150.             Keys ["MovLeft"] = KeyCode.A;
    151.             Keys ["MovRight"] = KeyCode.D;
    152.             Keys ["LeanLeft"] = KeyCode.Q;
    153.             Keys ["LeanRight"] = KeyCode.E;
    154.             Keys ["Run"] = KeyCode.LeftShift;
    155.             Keys ["Jump"] = KeyCode.Space;
    156.             Keys ["Crouch"] = KeyCode.LeftControl;
    157.             Keys ["Interact"] = KeyCode.F;
    158.             Keys ["Attack"] = KeyCode.Mouse0;
    159.             Keys ["Clairvoyance"] = KeyCode.Mouse1;
    160.             Keys ["Journal"] = KeyCode.J;
    161.             Keys ["Inventory"] = KeyCode.I;
    162.             Keys ["Tasks"] = KeyCode.X;
    163.             Keys ["Watch"] = KeyCode.C;
    164.             Keys ["QuickWheel"] = KeyCode.Mouse3;
    165.             Keys ["PauseMenu"] = KeyCode.Escape;
    166.             forward.text = Keys ["Forward"].ToString ();
    167.             backwards.text = Keys ["Backwards"].ToString ();
    168.             movleft.text = Keys ["MovLeft"].ToString ();
    169.             movright.text = Keys ["MovRight"].ToString ();
    170.             leanleft.text = Keys ["LeanLeft"].ToString ();
    171.             leanright.text = Keys ["LeanRight"].ToString ();
    172.             run.text = Keys ["Run"].ToString ();
    173.             jump.text = Keys ["Jump"].ToString ();
    174.             crouch.text = Keys ["Crouch"].ToString ();
    175.             interact.text = Keys ["Interact"].ToString ();
    176.             attack.text = Keys ["Attack"].ToString ();
    177.             clairvoyance.text = Keys ["Clairvoyance"].ToString ();
    178.             journal.text = Keys ["Journal"].ToString ();
    179.             inventory.text = Keys ["Inventory"].ToString ();
    180.             tasks.text = Keys ["Tasks"].ToString ();
    181.             watch.text = Keys ["Watch"].ToString ();
    182.             quickwheel.text = Keys ["QuickWheel"].ToString ();
    183.             pausemenu.text = Keys ["PauseMenu"].ToString ();
    184.         }
    185.    
    186.     }
    187.        
    188.  
    189.     //public void ConsoleOutput()
    190.     //{
    191.         //foreach (KeyValuePair<string, KeyCode> kvp in Keys)
    192.             //Debug.Log ("Key = {0} + Value = {1}" + kvp.Key + kvp.Value);
    193.  
    194.  
    195.  
    196. }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,599
    Not sure if this is causing you the problem, but broadly speaking, you should not do "processing" in OnGUI().

    The thinking behind this is that OnGUI() is called an indeterminate number of times per frame, depending on platform and other factors, so it is not consistent to use it this way.

    Instead do your processing on Update() and set some local variables appropriately, then do only display stuff in OnGUI().

    In your specific case, it doesn't even look like you are displaying anything, so perhaps you just mean to do this all in Update()??

    And also, do not do thread sleeping like that, ESPECIALLY not in OnGUI(). I'm not even sure what Unity is thinking about that... at best it will freeze the entire Unity app for 250ms. It probably just gets ignored but I'm not sure.

    Use coroutines if you want something to happen "a little bit later."
     
  3. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Small example I wrote up, as I've seen you stuck on this...
    Does this help?
    Code (csharp):
    1. public class Test1 : MonoBehaviour
    2. {
    3.     Event myEvent = new Event();
    4.     KeyCode[] mouseKeys;
    5.     void Awake()
    6.     {
    7.         mouseKeys = new KeyCode[] { KeyCode.Mouse0, KeyCode.Mouse1, KeyCode.Mouse2, KeyCode.Mouse3, KeyCode.Mouse4, KeyCode.Mouse5, KeyCode.Mouse6 };
    8.     }
    9.     void Update()
    10.     {
    11.         while (Event.PopEvent(myEvent))
    12.         {
    13.             if (myEvent.isMouse)
    14.             {
    15.                 if (myEvent.type == EventType.MouseDown)
    16.                 {
    17.                     for (int i = 0; i < mouseKeys.Length; ++i)
    18.                     {
    19.                         if (Input.GetKeyDown(mouseKeys[i]))
    20.                         {
    21.                             print("KeyCode : " + mouseKeys[i].ToString());
    22.                             break;
    23.                         }
    24.                     }
    25.                 }
    26.             }
    27.             else if (myEvent.isKey && myEvent.type == EventType.KeyDown && myEvent.keyCode != KeyCode.None)
    28.             {
    29.                 print("Key Keycode: " + myEvent.keyCode.ToString());
    30.             }
    31.         }
    32.     }
    33. }
     
    Kurt-Dekker likes this.
  4. Abuscaglio

    Abuscaglio

    Joined:
    Sep 5, 2017
    Posts:
    46
    Thank you for taking the time to reply as I've been smashing my head against a wall for a little while on this now. From what I can tell, your block of code assigns the mouse keycodes to an array and then uses that to decipher which button was pressed within the Update() function? I'm assuming I would have to change line 108 in my code to "void Update() in place of the current "void onGUI()"? after doing that , how would I link up/enter your snippet to function properly with my block?
     
  5. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    I'm sorry, I wasn't up to the task of re-inserting all of your code into the snippet that I wrote :)
    It does however deal with the events, sort of like yours lol.

    Okay, here's a little update.. I didn't find this answer before, for some reason, but instead of the array, you can use "myEvent.button" after the if statement for the isMouse. :) That will give you the same result. Just tested it.

    Personally I would use update and fit my code into it, if I were you.. but you have the option to use either. :)
     
  6. Abuscaglio

    Abuscaglio

    Joined:
    Sep 5, 2017
    Posts:
    46
    Yea, I've been hearing from more than one person that onGUI() is not the way to go in this instance. Why is that? is update() called more frequently per frame?
     
  7. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,599
    OnGUI() is the old legacy system. You use it when writing editor scripts nowadays, or if you're just doing simple placeholder UI. Either way, there should be no state-changing logic inside of it due to its inherent limitations.

    All other interactions with the user screen should use the "new Unity UI," which is like five years old now, all the classes contained under the UnityEngine.UI namespace, the objects that come up when you rightclick in the hierarchy panel under the "UI" entry (canvas, text, image, scrollview, etc.)