Why do right clicks of GUI Buttons also seemingly carry out a left click function ?

I have an editor window with a GUI.Button in it
nothing special. when I left click the button I get the expected result , When I right click the button ,It still carries out a function.

Example

Void OnGUI()
{
if(GUI.Button(new Rect(0,0,200,40),"Click Me"))
{
Debug.Log("Clicked");
}
}

same problem when I use ContextClick

for example

    private Rect rect;
    private Event e;
    void OnEnable()
    {
    rect = new Rect(0,0,200,40);
     e = new Event();
    }
    
    void OnGUI()
    {
//Do  if you right click  it will still output "Clicked"
      if (rect.Contains(e.mousePosition) && e.type == EventType.MouseDown)
    {
    Debug.Log("Clicked");
    }
    }

GUI.Button returns true when it’s clicked, it doesn’t make a difference on the button used.

I’m not sure what your second code is supposed to show, “Right Clicked” is printed with both left and right buttons?

There are a lot questions answered about detecting what button was used, like:

As pointed out by @Positive7

 void OnGUI()
          {
              var e = Event.current;
              GUI.Box (rect, "");
              if(rect.Contains(e.mousePosition) && e.type == EventType.MouseDown && e.button == 0){
     e.Use();
                  Debug.Log ("Left Click");
              }
              else if (rect.Contains(e.mousePosition) && e.type == EventType.MouseDown && e.button == 1) {
     e.Use();
                  Debug.Log ("Right Click");
              }
          }

and as mentioned by @Bunny83

The EventType “ContextClick” is an event that only applies to the UnityEditor and is only used in editor scripts as you can read in the docs. The MouseDown event is for any mouse button, including the second and third (usually the mousewheel-button). If you want to restrict it to some specific button you have to check the “button” field just like @Positive7 showed in his example.

Though I am using an editor window , this method seems to work best.

If you want to make it work with just the right click, try adding && input.mousebuttondown 1 in the if statement.