6

Just wondering if I can create a custom shortcut key to change the font color of the notebook text.

I've created a custom shortcut (in dkyeventtransactions.tr) to bring up the font color dialog, as so...

Item[KeyEvent["q", Modifiers -> {Control}],  
     FrontEndExecute[FrontEndToken["FontColorDialog"]]]

but I'm hoping to create different shortcut keys to change the font directly to different colors, without using the font color selection dialog.

Karsten7
  • 27,448
  • 5
  • 73
  • 134
mitcheljh
  • 1,017
  • 1
  • 9
  • 11
  • Thanks Kuba, this is very close, but it seems to have the effect of coloring all the characters in the line before the spot that I use it, rather than just change the color for the characters I type next. I your example below, if I were to use the press [Ctrl]-t after typeing dsf, it will change the dsf to red also. – mitcheljh May 10 '14 at 13:02

2 Answers2

8

You have to add the following statement to the KeyEventTranslations.tr file:

Item[KeyEvent["t", Modifiers -> {Control}],
     FrontEndExecute[
      FrontEnd`SetOptions[ 
                 FrontEnd`NotebookSelection[ FrontEnd`InputNotebook[]], 
                 FontColor -> RGBColor[1, 0, 0]]]]

enter image description here

Be careful: there are not many "free" letters available and when you use one that is already in use, it will not work :)


p.s. a more flexible solution is available with Shortcuts package and joker functionality, just proceed like in: 72914

Kuba
  • 136,707
  • 13
  • 279
  • 740
  • How does one use this using the joker functionality? I tried including NotebookApply[SelectedNotebook[], FrontEndExecute[ FrontEndSetOptions[ FrontEndNotebookSelection[ FrontEndInputNotebook[]], FontColor -> RGBColor[1, 0, 0]]], Before]` in the joker file, but this is wrong as it produces Null (as well as switching to a red font). What is my mistake?

    It also does not switch the font to red if the selection is not currently on a new line, but this is a minor issue.

    – Kvothe Oct 02 '17 at 16:17
  • @Kvothe put SetOptions[ NotebookSelection[ InputNotebook[]], FontColor -> RGBColor[1, 0, 0]], that should be enough – Kuba Oct 03 '17 at 11:23
7

One can change

MenuItem["Red",     FontColor->RGBColor[1, 0, 0]],

to, e. g.,

MenuItem["Red",     FontColor->RGBColor[1, 0, 0], MenuKey["r", Modifiers->{"Control"}]],

in the MenuSetup.tr file.

example

This has the additional advantage of adding the shortcuts to the menu for easier lookup.

menu

Karsten7
  • 27,448
  • 5
  • 73
  • 134
  • Where is one supposed to make the suggested change? – divenex Jan 17 '21 at 18:15
  • SystemOpen@FileNameJoin@{$InstallationDirectory, "SystemFiles", "FrontEnd", "TextResources", "Windows"} (make sure to save a copy before editing), see also How do I add new menuitems to menus? – Sterling Mar 27 '21 at 02:42
  • To avoid messing with the $InstallationDirectory, copy MenuSetup.tr to SystemOpen@FileNameJoin@{$UserBaseDirectory, "SystemFiles", "FrontEnd", "TextResources", "Windows"} and edit there. You may need to manually create some of those folders. Should take precedence over the other MenuSetup.tr file. – Sterling Mar 27 '21 at 02:50