2
$\begingroup$

I often use the escape key to quickly write mathematical notation in a text cell(e.g. $\pi$ is written when pressing escape+"pi"+escape). I find the escape key annoying to use because it's too far for my fingers to stretch, so I wrote some code to use "/" instead.

The code below replicates the escape key using "/" (to get the alias delimiter). However, for some reason, it does not parse upon the second press of the "/" key. Unfortunately, my solution requires an extra press of any key. For example, to get $\pi$ I have to press "/" then "pi" then "/" and then also the space bar (or any other key). The escape key does not require the extra keypress.

SetOptions[SelectedNotebook[], 
  NotebookEventActions -> {{"KeyDown", "/"} :>
     
     NotebookWrite[InputNotebook[], "\\[AliasDelimiter]"]}
  ];

I'm looking for a way to force the parse to happen upon the second press of "/" without the extra keypress so that it truly replicates the escape key. I'm looking for a solution that uses NotebookEventActions. I know there is an alternative way of editing the keyeventtranslations.nb, however, I am looking for a solution that uses NotebookEventActions specifically.

Any ideas?

P.S. I tried programmatically writing in a space and then deleting it, but it still doesn't parse until I physically press a key on my keyboard.

$\endgroup$

1 Answer 1

1
$\begingroup$

This works in a text cell. I added functionality to move the current selection to a placeholder automatically if one exists (this feature to move to a placeholder broke in Mathematica years ago).

SetOptions[InputNotebook[], NotebookEventActions -> {
   
   {"KeyDown", ">"} :>
    {
     FrontEndTokenExecute["ShortNameDelimiter"];
     FrontEndTokenExecute["MovePrevious"];
     FrontEndTokenExecute["MoveNextPlaceHolder"];
     FrontEndTokenExecute["MoveNext"];
     If[
      ListQ@
        Part[
         "InlineCellPosition" /. 
          FrontEndExecute@FrontEnd`CellInformation[SelectedCells[]], 
         1] == True,
      FrontEndTokenExecute["Tab"], None];
     }
   }
 ]
$\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.