2
$\begingroup$

My notebooks contain only text-based cells and no code. I use InputAliases quite a bit to save time. Typing the escape key before and after to trigger the InputAliases is not very efficient for me.

I would like to build a NotebookEventAction that triggers when I type "s" and "/" successively. This will work for me as it is rare that I would ever type these keys successively in my text-based notebooks.

I have successfully built the action to trigger when I type the "s" as shown below. Works great.

SetOptions[SelectedNotebook[], 
  NotebookEventActions -> {{"KeyDown", "s"} :> 
     KeyBindings`MySpecial[]}];

KeyBindings`MySpecial[] := Module[{},
   SelectionMove[InputNotebook[], All, Word];
   NotebookApply[InputNotebook[], 
    "\\[AliasDelimiter]\\[SelectionPlaceholder]\\[AliasDelimiter]"]];

However, I would like it to only trigger when I type "s" and then "/" in succession. Any ideas?

$\endgroup$

2 Answers 2

1
$\begingroup$

You'd probably have more luck using a modifier such as Control rather than tracking a two-key sequence. However NotebookEventActions does not seem to support that. A solution would be to add the modified key binding as a menu item, added to MenuSetup.tr

Something like this

Item["Special KeyBinding", KernelExecute[KeyBindings`MySpecial[]],
 MenuKey["s", Modifiers -> {"Control"}], MenuEvaluator -> Automatic]

based on the last item here: How can I set a keyboard shortcut to run a command?

Of course, Control+s is generally used for Save so you'd probably want to use something different. You might also need FrontEndExecute instead of KernelExecute.

$\endgroup$
1
1
$\begingroup$

I found a solution for successive keystrokes. This one triggers when I press "/" and then "s" in succession (the s is my moniker for shortcut). I'm not great at writing code. Maybe someone else can clean up my code below, but it does seem to work. Notice, I temporarily created a NotebookEventAction inside of the other one and then reset it after so that the succession works.

To try it out, run the code below and then open a text cell and type "alpha.s"

Res := SetOptions[SelectedNotebook[], 
   NotebookEventActions -> {{"KeyDown", "/"} :>
      
      MyBindings`MySpecial[]
     }];
Res[];
MyBindings`MySpecial[] := 
  Module[{}, 
   SetOptions[SelectedNotebook[], 
     NotebookEventActions -> {{"KeyDown", "s"} :> {
         SelectionMove[InputNotebook[], All, Word];
         
         NotebookApply[InputNotebook[], 
          "\\[AliasDelimiter]\\[SelectionPlaceholder]\\[\
AliasDelimiter]"];
         Res[];
         }}];
   ];
$\endgroup$
2
  • 1
    $\begingroup$ +1 very clever. Something is weirdly breaking in the copy & paste to Mathematica though. I got it working fine by copying in the complete NotebookApply expression from your original question section. $\endgroup$ Commented Mar 6, 2022 at 22:15
  • $\begingroup$ related... mathematica.stackexchange.com/questions/265379/… $\endgroup$
    – B flat
    Commented Mar 19, 2022 at 4:48

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.