7
$\begingroup$

I'm attempting to create a keyboard shortcut to center the contents of a text cell exactly as clicking "Align Center" accomplishes in the Format Menu. As far as I can tell, there is no frontendtoken for "Align Center". This surprised me.

The problem seems to be that I can’t find a way to grab the entire cell while retaining the current cursor position. Of course I can create code that will select the current cell and then I change text centering option to center, but then I lose the current cursor position.

The menu item, “align center” can do it so it seems like there must be way I can do it programmatically but can’t figure it out.

Any help?

$\endgroup$
5
  • $\begingroup$ Why can't you just add a short cut to the menu item? Something like: MenuItem["Align Center", TextAlignment -> Center, Scope -> SelectionCell, MenuKey["c", Modifiers -> {"Command", "Option"}]]? $\endgroup$
    – Carl Woll
    Sep 2, 2018 at 1:54
  • $\begingroup$ I didn’t think about that. I’ll try and get back to you! Thanks for the idea! $\endgroup$
    – B flat
    Sep 2, 2018 at 1:56
  • $\begingroup$ I don't understand your comment, is there a typo? Note that the "Align Center" menu item already exists, so you would just be adding a keyboard shortcut to it. $\endgroup$
    – Carl Woll
    Mar 14, 2019 at 15:24
  • $\begingroup$ Carl Woll -My apologies. Your method above does achieve the desired end result. However, I would still like to create my own new menu item that has the same functionality as the existing one and then create a shortcut for it. I see that you added a comment below. I will check that out to see if that works. $\endgroup$
    – B flat
    Mar 15, 2019 at 3:19
  • $\begingroup$ It seems like the only way to center the cell (without using the preinstalled "align center" menu item) is to select the whole cell. But I lose my cursor position when I do this. That is a problem for me. It would solve some related questions I've had over the years. There is comment below that it may be able to be done with Developer`CellInformation[]. $\endgroup$
    – B flat
    Mar 15, 2019 at 3:24

3 Answers 3

4
+100
$\begingroup$

The key option is Scope -> SelectionCell. So, you can either modify the menu item in MenuSetup.tr to:

MenuItem[
    "Align Center",
    TextAlignment -> Center,
    Scope -> SelectionCell,
    MenuKey["c", Modifiers -> {"Command", "Option"}]
]

or you can add a KeyEvent to KeyEventTranslations.tr:

Item[
    KeyEvent["c", Modifiers->{Command, Option}],
    TextAlignment->Center,
    Scope->SelectionCell
]

In both cases, I would add a modified version of the file to the appropriate place in $UserBaseDirectory.

$\endgroup$
3
  • $\begingroup$ Interesting! I've never heard of the Scope->SelectionCell option. I can't find much documentation on it. I'm not sure what that option is doing exactly and Im not sure what other choices there are beside SelectionCell. Do you know? I will confirm your code works tonight or tomorrow. Thanks for working on this. $\endgroup$
    – B flat
    Mar 15, 2019 at 3:36
  • $\begingroup$ It works! If you have a chance I'm curious about this Scope option and what else I can try with it or a reference to where I can find more about it. Thanks Carl. $\endgroup$
    – B flat
    Mar 15, 2019 at 3:42
  • 1
    $\begingroup$ @MichaelMcCain I believe the Scope RHS indicates where an option will be added. If you use the above shortcut, you will see that the Cell has acquired a TextAlignment->Center option. $\endgroup$
    – Carl Woll
    Mar 15, 2019 at 4:08
4
$\begingroup$

You may use the standard menu short-cuts. By pressing the following keys in sequence you will navigate the menu using the keyboard to perform the text centering.

Alt,r,a,c

In general, once you press Alt the items in the menu will have one character underlined. Press that character to select that menu item.

Update: I think for Mac you press Ctrl+F2 to enter the menu bar from the keyboard.

Hope this helps.

$\endgroup$
2
  • 1
    $\begingroup$ Is this Windows only? It does not seem to work on Mac. $\endgroup$
    – b3m2a1
    Aug 28, 2018 at 2:20
  • $\begingroup$ @b3m2a1 I think for Mac you press Ctrl+F2 to enter the menu. $\endgroup$
    – Edmund
    Aug 28, 2018 at 10:28
2
$\begingroup$

So it turns out all you need to do is use the token TextAlignment->Center. Here's a way to do this taken directly from the menu itself:

Style[
 "asdasdasd",
 ContextMenu ->
  Menu["Text Alignment", {LinkedItems[{MenuItem["Align Left", 
       TextAlignment -> Left, Scope -> SelectionCell], 
      MenuItem["Align at 25%", TextAlignment -> -0.5, 
       Scope -> SelectionCell], 
      MenuItem["Align Center", TextAlignment -> Center, 
       Scope -> SelectionCell], 
      MenuItem["Align at 75%", TextAlignment -> 0.5, 
       Scope -> SelectionCell], 
      MenuItem["Align Right", TextAlignment -> Right, 
       Scope -> SelectionCell], 
      MenuItem["On AlignmentMarker", TextAlignment -> AlignmentMarker,
        Scope -> SelectionCell]}]}]
 ]

Right/Option click on that text:

enter image description here

then picking center:

enter image description here

Just to confirm that the MenuSetup and KeyEventTranslations file are effectively operating in the same way I added this to my events file:

Item[KeyEvent["Up", Modifiers -> {Shift}], TextAlignment -> Center]

and then selected a cell bracket, pressed Shift-Up and it centered the cell.

Presumably this will work for any style option.

$\endgroup$
10
  • $\begingroup$ I still don't know what's the token/command behind that MenuItem and the MenuItem will not help in putting that in a key event procedure, will it? $\endgroup$
    – Kuba
    Aug 27, 2018 at 8:45
  • $\begingroup$ @Kuba I always assumed the key events and menu system operated via the same basic code. Most key events just get a string which is a FE token, just like the menu items do. I haven't tested this, though. $\endgroup$
    – b3m2a1
    Aug 27, 2018 at 8:47
  • $\begingroup$ I thought so, that is there is either a token+params or an explicit SetOptions expression. Does not seem the case here. This boils down to how to translate MenuItems from above to an expression which will work from e.g. a Button. $\endgroup$
    – Kuba
    Aug 27, 2018 at 8:49
  • $\begingroup$ @Kuba another option of course is to use FrontEnd`SetOptions but the difficulty there is in finding the cursor cell. That's always been a pain. $\endgroup$
    – b3m2a1
    Aug 27, 2018 at 8:50
  • 1
    $\begingroup$ @MichaelMcCain it’s possible to do using Developer`CellInformation and other tricks but I don’t have time to go into it more now. You should be able to set a bounty with a little hyperlink looking thing underneath the question or something like that. $\endgroup$
    – b3m2a1
    Sep 2, 2018 at 2:08

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.