Here are four approaches to setting up keyboard shortcuts. The last is the best!
AddMenuCommands
First examples using notebook manipulation, (as you tried):
This example adds a command to the Insert menu, with a key combination Control+U (normally the underline command). This addition just lasts for the session, but could be added to an init file.
FrontEndExecute[
FrontEnd`AddMenuCommands["DuplicatePreviousOutput",
{Delimiter, MenuItem["Evaluate x = 1",
FrontEnd`KernelExecute[
nb = SelectedNotebook[];
SelectionMove[nb, After, Cell];
NotebookWrite[nb, Cell[BoxData[RowBox[{"x", "=", "1"}]], "Input"]];
SelectionMove[nb, Previous, Cell];
SelectionEvaluate[nb]],
MenuKey["u", Modifiers -> {"Control"}],
System`MenuEvaluator -> Automatic]}]]
Note, you can find the box format like so:
ToBoxes[Hold[x = 1]]
Out = RowBox[{"Hold", "[", RowBox[{"x", "=", "1"}], "]"}]
This could also be done invisibly like this:
FrontEndExecute[
FrontEnd`AddMenuCommands["DuplicatePreviousOutput",
{Delimiter, MenuItem["Evaluate x = 1",
FrontEnd`KernelExecute[
nb = CreateDocument[Null, Visible -> False, WindowSelected -> True];
NotebookWrite[nb, Cell[BoxData[RowBox[{"x", "=", "1"}]], "Input"]];
SelectionMove[nb, Previous, Cell];
SelectionEvaluate[nb];
NotebookClose[nb]],
MenuKey["u", Modifiers -> {"Control"}],
System`MenuEvaluator -> Automatic]}]]
A quicker version just uses ToExpression
, (not Evaluate
, as you first tried):
FrontEndExecute[
FrontEnd`AddMenuCommands["DuplicatePreviousOutput",
{Delimiter, MenuItem["Evaluate x = 1",
FrontEnd`KernelExecute[ToExpression["x=1;"]],
MenuKey["u", Modifiers -> {"Control"}],
System`MenuEvaluator -> Automatic]}]]
KeyEventTranslations
The notebook manipulation can be implemented in KeyEventTranslations.tr like so:
Item[KeyEvent["u", Modifiers -> {Control}], FrontEndExecute[
FrontEnd`SelectionMove[FrontEnd`InputNotebook[], After, Cell];
FrontEnd`NotebookWrite[FrontEnd`InputNotebook[],
Cell[BoxData[RowBox[{"x", "=", "1"}]], "Input"]];
FrontEnd`SelectionMove[FrontEnd`InputNotebook[], Previous, Cell];
FrontEnd`SelectionEvaluate[FrontEnd`InputNotebook[]]]]
NotebookEventActions
Another approach for adding a keyboard shortcut is this:
function[] := x = 1
SetOptions[SelectedNotebook[],
NotebookEventActions -> {"DownArrowKeyDown" :> function[]}]
Setting notebook event actions to command or control key combinations does not appear to be possible. A shifted key assignment can be made, e.g. Shift+O (just capital "o"):
NotebookEventActions -> {{"KeyDown", "O"} :> ...
You can replace SelectedNotebook[]
with $FrontEnd (note 'Details') - that adds the assignment to your front end init file. (To undo, you would need to edit the init file to remove the event assignment.)
MenuSetup
Finally -- and this is probably the most obvious -- place x = 1;
in a file called assign.m and call it from a menu item specified in MenuSetup.tr with:
Item["Evaluate x = 1",
KernelExecute[Get["assign.m", Path -> "C:\\Users\\chrisd\\Documents"]]]],
MenuKey["u", Modifiers->{"Control"}], MenuEvaluator->Automatic]
Alternatively, use ToExpression
:
Item["Evaluate x = 1", KernelExecute[ToExpression["x=1;"]],
MenuKey["u", Modifiers -> {"Control"}], MenuEvaluator -> Automatic]