2
$\begingroup$

Using NotebookEventActions one can make MMA evaluate a function when a key is pressed. For example:

SetOptions[
  EvaluationNotebook[],
  NotebookEventActions -> {{"KeyDown","x"} :> Print["You pressed \"x\"!"]}
]

will print a sentence every time the user presses "x" on the keyboard.

How do I use NotebookEventActions to do something when the user presses one of the function keys "F2", "F3, ..., "F12", or a special key, such as "pause"?

If this is not possible, then how can I make my program react to special keys with a mechanism other than NotebookEventActions? It is important that the change is local to one notebook, and that it can be made programmatically.

$\endgroup$
6
  • $\begingroup$ AFAIK there is no documented way to do this. There might be undocumented ways, so maybe we are lucky and someone is able to answer... $\endgroup$ Mar 3, 2018 at 23:50
  • 2
    $\begingroup$ You can edit KeyEventTranslations.tr but it will be active across all notebooks. $\endgroup$
    – Kuba
    Mar 4, 2018 at 21:41
  • $\begingroup$ @Kuba: I know, but I need it to be local to the current notebook, and I need to change it programmatically. $\endgroup$
    – JEM_Mosig
    Mar 4, 2018 at 21:46
  • $\begingroup$ Find the key-code for them and pass that to KeyDown. $\endgroup$
    – b3m2a1
    Mar 4, 2018 at 23:32
  • 2
    $\begingroup$ See this mathematica.stackexchange.com/a/139748 $\endgroup$
    – b3m2a1
    Mar 4, 2018 at 23:49

1 Answer 1

4
$\begingroup$

This should work:

CellPrint[
 Cell[BoxData["\"Try the function keys\""],
  "Output",
  CellEventActions -> {
    {"KeyDown", "\.10"} :> Print["this should work"]
    },
  CellEditDuplicate -> False
  ]
 ]

I found that by using this trick

CellPrint[
 Cell[BoxData["\"Type here to determine key type\""],
  "Output",
  CellEventActions -> {
    "KeyDown" :>
     (
      SelectionMove[EvaluationCell[], All, CellContents];
      NotebookWrite[EvaluationNotebook[],
       ToBoxes[
          <|
           "Name" -> CharacterName[#, "UnicodeName"],
           "Display" -> #,
           "KeyRaw" -> FullForm[#]
           |>
          ] &@CurrentValue["EventKey"]
       ]
      )
    },
  CellEditDuplicate -> False
  ]
 ]
$\endgroup$
6
  • $\begingroup$ Hi @b3m2a1. Thank you for your answer! Unfortunately it does not react to any function keys or special keys on my keyboard. May this depend on the OS? I got Windows 10, and MMA version 11.0.1. $\endgroup$
    – JEM_Mosig
    Mar 5, 2018 at 2:56
  • $\begingroup$ This is for Mac. Use that second block of code to determine what your function key binds to. $\endgroup$
    – b3m2a1
    Mar 5, 2018 at 5:18
  • 1
    $\begingroup$ @b3m2a1 does not work for F* on Win according to my test. Anyway, isn't it a duplicate of the question you've linked? $\endgroup$
    – Kuba
    Mar 5, 2018 at 7:03
  • $\begingroup$ @Kuba yeah I’d agree with that. I didn’t realize the other one was about more than just backspace. $\endgroup$
    – b3m2a1
    Mar 5, 2018 at 7:05
  • 1
    $\begingroup$ @JEM_Mosig Yes, that was the point, to keep relevant answers in one place. Sorry didn't noticed you wanted to make clear it is specifically about Windows. And yes, I've already seen it and upvoted. Will have to play with it soon. $\endgroup$
    – Kuba
    Mar 6, 2018 at 19:37

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