3
$\begingroup$

I created the following code that overlays an attached cell containing a large canvas on top of a notebook. This allows me to use the drawing tools on top of my notebook so that I can annotate code with my handwriting.

CreateWindow[DocumentNotebook[CellGroup[{
    "Testing Writing Canvas Overlay",
    "Step 1. Click Draw Above", 
    "Step 2.  Invoke the locked freehand line drawing tool by pressing the f key twice.", "Step 3. Draw Something",
    "Step 4.  Click Reset to Delete AttachCell"}],
  CellGrouping -> Manual,
  WholeCellGroupOpener -> True],
 DockedCells -> Cell[BoxData@ToBoxes@Row[{Button["Draw",
        SelectionMove[ButtonNotebook[], Before, Notebook];
         SelectionMove[ButtonNotebook[], Next, Cell];
        obj = 
         AttachCell[SelectedCells[][[1]], 
          Style[Graphics[{}, AspectRatio -> Full, 
            ImageSize -> 
             AbsoluteCurrentValue[EvaluationNotebook[], 
               WindowSize]*{2, 2}], Deployed -> False, 
           Editable -> True], Left, 75, Left];
        SelectionMove[obj, All, CellContents]],
       Button["Reset", 
        NotebookDelete[
         Cells[EvaluationNotebook[], AttachedCell -> True]]]}]]]

I would like to skip step 2 (as shown in the notebook). In other words, I would like to include an extra line of code that allows me to draw immediately upon pressing the "Draw" button. I don't want to have to press the f key at all.

It seems like I need to either simulate two keypresses of the f key somehow or invoke some frontendtoken twice to start the locked freehand line tool. Any ideas?

$\endgroup$

1 Answer 1

2
$\begingroup$

This works....

Needs["JLink`"];
ReinstallJava[];

robotclass = JavaNew["java.awt.Robot"];
LoadJavaClass["java.awt.event.InputEvent"];
LoadJavaClass["java.awt.event.KeyEvent"];
LoadJavaClass["java.awt.Event"];

keypress = (Function[k, 
      robotclass[keyPress[Symbol["KeyEvent`VKU" <> k]]]] /@ {##};
    Function[k, 
      robotclass[
       keyRelease[Symbol["KeyEvent`VKU" <> k]]]] /@ {##}) &;
CreateWindow[
 DocumentNotebook[
  CellGroup[{"Testing Writing Canvas Overlay", 
    "Step 1. Click Draw Above", "Step 2. Draw Something", 
    "Step 3.  Invoke the locked freehand line drawing tool by \
pressing the f key twice.", 
    "Step 4.  Click Reset to Delete AttachCell"}], 
  CellGrouping -> Manual, WholeCellGroupOpener -> True], 
 DockedCells -> 
  Cell[BoxData@
    ToBoxes@Row[{Button["Draw", 
        SelectionMove[ButtonNotebook[], Before, Notebook];
        SelectionMove[ButtonNotebook[], Next, Cell];
        obj = 
         AttachCell[SelectedCells[][[1]], 
          Style[Graphics[{}, AspectRatio -> Full, 
            ImageSize -> 
             AbsoluteCurrentValue[EvaluationNotebook[], 
               WindowSize]*{2, 20}], Deployed -> False, 
           Editable -> True], Left, 75, Left];
        SelectionMove[obj, All, CellContents];
        keypress["F", "F"];], 
       Button["Reset", 
        NotebookDelete[
         Cells[EvaluationNotebook[], AttachedCell -> True]]]}]]]
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.