[solved] Unexpected buttonLeft Mouse Events in TInputPressRelease.Press

I added the following event handler:

function TViewPlayWorld.Press(const Event: TInputPressRelease): Boolean;
begin
  Result := inherited;
  if Event.MouseButton = TCastleMouseButton.buttonLeft then
  begin
    ...
  end;
end;

However, sometimes Event.MouseButton has the value TCastleMouseButton.buttonLeft even though I did not press the mouse button at all.

Update :
I noticed that when rotating the mouse scroll wheel, the mouse button event reports the value buttonLeft.

You need to check EventType first to make sure it is itMouseButton.

2 Likes

Exactly, check EventType first. Please consult our examples, they all show a simple way of doing it, by IsMouseButton, like

function TViewPlay.Press(const Event: TInputPressRelease): Boolean;
begin
  Result := inherited;
  if Result then Exit; // allow the ancestor to handle keys

  if Event.IsMouseButton(buttonLeft) then
  begin
    // .. do something
    Exit(true); // handled
  end;
end;

IsMouseButton internally checks EventType before checking MouseButton.

Also, just buttonLeft is good, no need for TCastleMouseButton.buttonLeft. We don’t use “scoped enums” for CGE enums, at least now (for historical reasons).

1 Like

Thank you.
I didn’t realize that I should use EventType.

Note: You can search our API docs for identifiers to see how they should be used. I checked that the TInputPressRelease.MouseButton docs mention it:

When EventType is itMouseButton, this is the mouse button pressed or released. Always buttonLeft for touch device press/release events.

1 Like