4
$\begingroup$

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.

$\endgroup$
  • $\begingroup$ 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. $\endgroup$ – mitcheljh May 10 '14 at 13:02
7
$\begingroup$

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

$\endgroup$
  • $\begingroup$ 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. $\endgroup$ – Kvothe Oct 2 '17 at 16:17
  • $\begingroup$ @Kvothe put SetOptions[ NotebookSelection[ InputNotebook[]], FontColor -> RGBColor[1, 0, 0]], that should be enough $\endgroup$ – Kuba Oct 3 '17 at 11:23
5
$\begingroup$

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

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.