So I have a custom editor window script and I am trying to tell if a button combination is pressed in ongui and have it execute an action once.
private void OnGUI(){
if (Event.current.type == EventType.MouseDrag && Event.current.button == 0)
{
if( Event.current.alt){
//do stuff
Event.current.Use();
}
}
}
Now the problem is the stuff that pressing this button combination does happens a lot more than once when I only want it to happen once until the user releases the alt button and presses it again.
I have also tried
private bool CanDoStuffAgain = true;
private void Update(){
if(!Input.GetKey(KeyCode.Alt) && CanDoStuffAgain == false){
Debug.Log("Can do stuff again");
CanDoStuffAgain = true;
}
}
private void OnGUI(){
if ((Event.current.type == EventType.MouseDrag && Event.current.button == 0) )
{
if(Event.current.alt && CanDoStuffAgain == true){
//do stuff
CanDoStuffAgain = false;
Event.current.Use();
}
}
Now when I do this the debug message just keeps printing over and over even when I have Alt pressed. This means for whatever reason Input.GetKey isn’t working properly in Update. I also have a [ExecuteInEditMode] attribute above my class, but am not sure if it is needed or if Update runs regardless. Anyways I basically just want my stuff that happens when alt is pressed while the user is dragging to happen once and not happen again unless the user stops pressing alt and then presses it again. Is there any way to accomplish this?