- Home /
Detect Ctrl+LMB on a rect in edit mode?
Hello,
I have a Rect
called lastRect
(which is a rect for an object field), and I want to perform and operation if the user (for example) left click on it while holding the Ctrl
key.
I'm not sure how to go about doing that, I tried this:
if (lastRect != null && lastRect.Contains(Event.current.mousePosition)) {
if (Event.current.control)
if (Event.current.button == 0)
{
isObjectField = !isObjectField;
}
}
It didn't seem to work, it gave strange results. For example, if I hold Ctrl and do a left click, the boolean toggles, then if I just hold Ctrl without a LMB, it toggles too!
I think that's not the way to detect input in this case.
I tried caching the current event, and using the local version instead, didn't seem to change much. I also tried using the event (`Event.current.Use()`) when the boolean toggles, didn't do much too (inspector went crazy).
Any ideas?
Thanks!
Answer by vexe · Feb 19, 2014 at 01:34 PM
Found the solution - with the help of @Jamora
The thing is, if there's no mouse button is pressed, Event.current.button
will also return true, that's why holding just control without clicking will return true too.
The trick is to check for the event type first, making sure we get a MouseDown/MouseUp
if (Event.current.control) {
if (Event.current.type == EventType.MouseDown) {
if (Event.current.button == 0) // 0 for LMB, 1 for RMB, 2 for MMB
// do whatever...
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613133927im_/https://answers.unity.com/themes/thub/images/avi.jpg)