12
$\begingroup$

How can I switch the functionality of the Enter and Shift+Enter key combinations in Windows?

I find Matlab's interface more intuitive sometimes. By default in Mathematica's front-end, the Shift+Enter combination processes input in the active cell, while Enter inserts a carriage-return.

Is there a standard way to re-map the actions of different key combinations?

$\endgroup$
4
  • $\begingroup$ Have you tried KeyEventTranslations.tr? On OS X there's an explicit entry for the Evaluate command. $\endgroup$
    – Szabolcs
    Commented Oct 11, 2016 at 19:38
  • $\begingroup$ @Szabolcs no I haven't I have it open now not sure what to change exactly but it looks logical. $\endgroup$
    – William
    Commented Oct 11, 2016 at 19:45
  • $\begingroup$ Do make a backup. If you keep the backup in the same location, make sure it doesn't have the .tr extension. Not sure about how much is shared between Windows and Mac, but on Mac "Enter" and "Return" are considered different keys. "Return" is what we normally use and "Enter" is accessed either as Shift-Return or through the numpad. KeyEventTranslations uses the same names (Return vs Enter) on Mac. $\endgroup$
    – Szabolcs
    Commented Oct 11, 2016 at 21:39
  • $\begingroup$ @Kuba that's correct I would like to swap the Enter and Shift+Enter keys $\endgroup$
    – William
    Commented Oct 12, 2016 at 15:06

4 Answers 4

6
+100
$\begingroup$

First move KeyEventTranslations.tr file to your $UserBaseDirectory in order to not damage installation files. (Rebinding keys to define new keyboard shortcuts)

Open the file and find those lines:

...

(* Evaluation *)

...

Item[KeyEvent["Return", Modifiers -> {Shift}], "HandleShiftReturn"]

...

(* Input *)

Item[KeyEvent["Return"], "Linebreak"]

...

and swap "HandleShiftReturn" with "Linebreak".

Works on Windows.

$\endgroup$
4
  • $\begingroup$ This does not work on linux ;( $\endgroup$
    – William
    Commented Oct 13, 2016 at 21:26
  • $\begingroup$ Is there a way to do this on a per cell basis easily? $\endgroup$
    – William
    Commented Oct 13, 2016 at 23:00
  • 2
    $\begingroup$ @Liam No, you can change each event separately but both with result in a loop: SetOptions[ PreviousCell[], CellEventActions :> { {"MenuCommand", "HandleShiftReturn"} :> FrontEndTokenExecute["Linebreak"], "ReturnKeyDown" :> FrontEndTokenExecute["HandleShiftReturn"] } ] $\endgroup$
    – Kuba
    Commented Oct 14, 2016 at 6:24
  • 1
    $\begingroup$ @Kuba Strangely enough, it works for PreviousCell[] but doesn't work for $FrontEndSession... $\endgroup$ Commented Oct 15, 2016 at 19:29
9
$\begingroup$

Using Autohotkey the following works pretty well.

#IfWinActive ahk_class NotebookFrame
  Enter::Send {blind}{shift down}{enter}{shift up} 
  Shift & Enter::Send {enter}
#IfWinActive

In case the internals are ever changed you can use the following to get the internal class.

Return::
  WinGetClass, class, A
  MsgBox, The active window's class is "%class%".
  return
$\endgroup$
7
$\begingroup$

This piece of code should do the trick. It takes effect immediately.

SetOptions[$FrontEnd, 
  FrontEndEventActions:>{{"MenuCommand", "HandleShiftReturn"} :> 
  FrontEndTokenExecute["Linebreak"], "ReturnKeyDown" :> 
FrontEndTokenExecute["EvaluateCells"]}]

(Tested on ArchLinux/MacOS_Sierra/Win10Server2016 with MMA 11. Hopefully it will work on Ubuntu, too)

If you want to run it automatically each time you open MMA, you can modify the code and insert it to the frontend configuration file $UserBaseDirectory/FrontEnd/init.m

And many thanks to Kuba. The comment under his answer inspired me a lot. Without his idea I wouldn't be able to produce this answer.

$\endgroup$
4
  • $\begingroup$ EvaluateCells is what numpad enter does, not shift+enter. $\endgroup$
    – Kuba
    Commented Oct 15, 2016 at 17:00
  • 2
    $\begingroup$ (+1) Works with Mathematica 11.0.0 on Win7 x64. After evaluating this the usual (not numpad) Enter evaluates Cell, and Shift+Enter inserts a linebreak. $\endgroup$ Commented Oct 15, 2016 at 17:04
  • $\begingroup$ Did you take this from Kuba ;) $\endgroup$
    – William
    Commented Oct 15, 2016 at 17:53
  • $\begingroup$ @Liam Yes, Kuba is whom the credit should go to : ) $\endgroup$
    – btwiuse
    Commented Oct 15, 2016 at 17:58
0
$\begingroup$

For completion this technique seems to work fairly well in ubuntu.

Basically if you open /usr/share/X11/xkb/symbols/pc and edit the file you can make the following 2 lines like the following.

key <RTRN> {    [ KP_Enter      ]   };
key <CAPS> {    [ Return        ]   };

Now CapsLock functions as the return key in Mathematica and the return key functions as the cellevaluation function.

This isn't perfect but works fairly well and most applications seem to still work well.

$\endgroup$

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.