I am using the LocatorAutoCreate option which allows the user to hold Alt while clicking in order to create/remove locators. However, I need to be able to prevent the "original" functionality of LocatorPane which moves the closest locator to the current mouse position whenever you click.

To clarify, I will use a simple example:

LocatorPane[Dynamic[list], Graphics[{Gray, Disk[]}], LocatorAutoCreate -> True]

I want to prevent locators from being moved when clicking and not holding Alt, but still keep the functionality of LocatorAutoCreate which creates/removes the locators when the user holds Alt and clicks.

My guess is that I will need to use some combination of EventHandler, DynamicWrapper, and CurrentValue, but I am not experienced with these functions and have not yet been able to find a solution.

My best idea currently is to use something like this:

  LocatorPane[Dynamic[list], Graphics[{Gray, Disk[]}], LocatorAutoCreate -> True],
  If[CurrentValue["MouseButtonTest"] && ! CurrentValue["AltKey"], 
    CurrentValue["MouseButtonTest"] = False]]

Yet, clearly this doesn't work as you cannot manually set CurrentValue["MouseButtonTest"] to False.


A few minutes after making this post I thought of a solution (even though I had spent 2 hours prior struggling). If anyone is curious, this problem can be solved using the second argument of dynamic:

    (If[CurrentValue["MouseButtonTest"] && ! CurrentValue["AltKey"], 
       list = list, list = #]) &],
  Graphics[{Gray, Disk[]}],
  LocatorAutoCreate -> True]
  • 1
    $\begingroup$ You should remove your edit post as a self-answer. $\endgroup$ – m_goldberg Oct 18 '17 at 2:37
  • $\begingroup$ I am not sure I got it, so the only action allowed is locators creation right? They should always stay whey they were created? $\endgroup$ – Kuba Oct 18 '17 at 7:48
  • $\begingroup$ LocatorAutoCreate also allows the user to click on a selected locator while holding alt to remove it. So the user can remove a locator and then create a new locator in the necessary position to "move" it. $\endgroup$ – user52941 Oct 18 '17 at 18:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.