1
$\begingroup$

I would like to lock the cursor position, i.e. the insertion point, to the current position of the mouse at all times in a given notebook.

An experiment with J/Link approximates this, by repeatedly simulating left mouse button clicks:

Needs["JLink`"];
ReinstallJava[];
robotclass = JavaNew["java.awt.Robot"];
LoadJavaClass["java.awt.event.InputEvent"];

click[] := Block[{},
   robotclass@mousePress[InputEvent`BUTTON1UMASK];
   robotclass@mouseRelease[InputEvent`BUTTON1UMASK];
   ];

Do[click[], 1000]

but this results in repeatedly expanding the current selection. (Ideally I would like to still use the physical left mouse button to expand the current selection.)

A second experiment recursively clicks, but allows a proper system click to pass through only after some time and cursor distance has passed:

LastTime = AbsoluteTime[];
LastMousePosition = MousePosition[];
SetClickOptions[pass_] := SetOptions[InputNotebook[],
  NotebookEventActions -> {"MouseClicked" :> Block[{},
      If[
        EuclideanDistance[LastMousePosition, MousePosition[]] > 4
         &&
         AbsoluteTime[] - LastTime > 0.01
        ,
        LastMousePosition = MousePosition[];
        LastTime = AbsoluteTime[];
        SetClickOptions[True];
        ,
        SetClickOptions[False];
        ];

        click[];
      ],

      PassEventsDown -> pass}]

Run this with

SetClickOptions[False]

and a single click, and keep this lifebuoy nearby:

SetOptions[InputNotebook[], 
 NotebookEventActions -> {"MouseClicked" :> "", 
   PassEventsDown -> True}]

This second piece of code still has the problem of selecting entire pieces of expressions, especially when mousing over a new line.

$\endgroup$
  • $\begingroup$ Is there a good reason behind this wish? :) $\endgroup$ – Kuba Feb 5 '18 at 16: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.