Skip to main content

notebooks - Capturing an F1


I teach an Intro to Mathematica course, and one of the things I try to reinforce early on is just how helpful Mathematica's help is. In the very first lab for the course, I even give the students a randomly selected command (from among simple ones like PrimeQ, IntegerDigits, etc.) and ask them to tell me what that command does. As of right now, they get this question right if they correctly explain what the given command does.


What I'd like to do is make sure they are actually going to the help. Is there some way to capture an F1 key press within an EventHandler or something similar?



Answer



You could add a trap to Documentation`HelpLookup which is called when F1 is pressed:


Unprotect[Documentation`HelpLookup];
Documentation`HelpLookup[link_String, nb_, lang_String, opts : OptionsPattern[]] :=
Block[{$inblock = True},

lookedup[link] = True;
Documentation`HelpLookup[link, nb, opts]] /; ! TrueQ[$inblock]

You can add whatever code you want. As an example I assign a downvalue to lookedup so the searched expressions can be viewed with ? lookedup


Note: The Block[{$in = True}, ...] /; ! TrueQ[$in] construct is the Villegas-Gayley technique for injecting code into built-in functions.


Comments