Search Unity

Catching Control key being pressed while using EventSystem OnDrag()

Discussion in 'Scripting' started by TroyDavis, Mar 20, 2016.

  1. TroyDavis

    TroyDavis

    Joined:
    Mar 27, 2013
    Posts:
    78
    Hey all I've searched the web and I just can't figure out how I can catch when the user holds down the control key and starts dragging an object that's in my inventory.

    I can catch the OnDrag just fine but the event system doesn't seem to recognize Input.OnKeyDown(Keycode.LeftControl)) at the same time.

    here is what I have
    Code (csharp):
    1. public void OnDrag(PointerEventData eventData)
    2.     {
    3.         if (eventData.button == PointerEventData.InputButton.Left && UniqueID != "") //Starts a Drag & Drop action if allowed to
    4.         {
    5.             //if (EventSystem.current. == KeyCode.LeftControl || e.keyCode == KeyCode.RightControl)
    6.             //{
    7.             //    Debug.Log("Dragging one off the stack!");
    8.             //    return;
    9.             //}
    10.  
    11.             //Debug.Log("Dragging");
    12.             InventorySlotsManager.Instance.IsDragging = true;
    13.  
    14.             // Copy Items in Socket to dragging container
    15.             InventorySlotsManager.Instance.DraggedItemImageContainer.GetComponent<DraggedItemScript>().DraggedItemUniqueID = UniqueID;
    16.             InventorySlotsManager.Instance.DraggedItemImageContainer.GetComponent<DraggedItemScript>().DraggedItemQuantity = itemQuantityContainer.text;
    17.             InventorySlotsManager.Instance.DraggedItemImageContainer.GetComponent<Image>().sprite = iconImageContainer.sprite;
    18.  
    19.             // clear the slot fields
    20.             UniqueID = "";
    21.             iconImageContainer.sprite = EmptyItem;
    22.             itemQuantityContainer.text = "";
    23.  
    24.             // Make the Dragged Container visible
    25.             InventorySlotsManager.Instance.DraggedItemImageContainer.SetActive(true);
    26.  
    27.             // make the Tooltip Visible
    28.             InventorySlotsManager.Instance.Tooltip.SetActive(false);
    29.         }
    30.     }
    This works great to drag everything int he selected inventory slot but I need to tell when the control Key is held down while they drag so that I can separate stacked items and drag one item from the stack out of the slot.

    Any help is appreciated.
     
  2. TroyDavis

    TroyDavis

    Joined:
    Mar 27, 2013
    Posts:
    78
    After reviewing the system it will will not act the way I want it too. Don't know why I thought it was working but apparently it still won't recognize when the key is hit.
     
    Last edited: Mar 20, 2016
  3. passerbycmc

    passerbycmc

    Joined:
    Feb 12, 2015
    Posts:
    1,741
    check for the key up and down events in Update and use it to set a boolean that you use i the ondrag event
     
  4. TroyDavis

    TroyDavis

    Joined:
    Mar 27, 2013
    Posts:
    78
    I tried that it didn't work, I setup a public boolean that chacked for the control keys being prressed in my Inventory Manager script's Update function. When pressed it sets a boolean IsControl = true, if not it sets it to false.

    Since this script is a singleton class I can call any public functions and variables from it using InventoryManagerScript.Instance.IsControl

    but when I am in the OnDrag event it seems to show this as false even when I know it should be true.
     
  5. TroyDavis

    TroyDavis

    Joined:
    Mar 27, 2013
    Posts:
    78
    Found my issue, I used GetKeyDown should have just Used GetKey