2

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.

JEM_Mosig
  • 3,003
  • 15
  • 28
  • 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... – Albert Retey Mar 03 '18 at 23:50
  • 2
    You can edit KeyEventTranslations.tr but it will be active across all notebooks. – Kuba Mar 04 '18 at 21:41
  • @Kuba: I know, but I need it to be local to the current notebook, and I need to change it programmatically. – JEM_Mosig Mar 04 '18 at 21:46
  • Find the key-code for them and pass that to KeyDown. – b3m2a1 Mar 04 '18 at 23:32
  • @b3m2a1: I have tried that. But EventHandler[InputField[], {{"KeyDown", 113} :> Print["!"]}] does not work, since Google says "F2" corresponds to 113, but FromCharacterCode[113] gives "q". – JEM_Mosig Mar 04 '18 at 23:44
  • 2
    See this https://mathematica.stackexchange.com/a/139748 – b3m2a1 Mar 04 '18 at 23:49

1 Answers1

4

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
  ]
 ]
b3m2a1
  • 46,870
  • 3
  • 92
  • 239
  • 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. – JEM_Mosig Mar 05 '18 at 02:56
  • This is for Mac. Use that second block of code to determine what your function key binds to. – b3m2a1 Mar 05 '18 at 05:18
  • 1
    @b3m2a1 does not work for F* on Win according to my test. Anyway, isn't it a duplicate of the question you've linked? – Kuba Mar 05 '18 at 07:03
  • @Kuba yeah I’d agree with that. I didn’t realize the other one was about more than just backspace. – b3m2a1 Mar 05 '18 at 07:05
  • @Kuba: This is why I added the "on Windows" to the title, but I guess it is best to have everything in one place. I figured out how to do this on Windows, and posted it in the main question here: https://mathematica.stackexchange.com/a/167223/35390, as I cannot answer my own question anymore. – JEM_Mosig Mar 06 '18 at 19:35
  • 1
    @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. – Kuba Mar 06 '18 at 19:37