# Programmatically create a keyboard shortcut to “Align Center” a Text Cell without losing cursor position

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?

• 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"}]]? – Carl Woll Sep 2 '18 at 1:54
• I didn’t think about that. I’ll try and get back to you! Thanks for the idea! – Michael McCain Sep 2 '18 at 1:56
• 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. – Carl Woll Mar 14 '19 at 15:24
• 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. – Michael McCain Mar 15 '19 at 3:19
• 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 DeveloperCellInformation[]. – Michael McCain Mar 15 '19 at 3:24

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,
]


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.

• 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. – Michael McCain Mar 15 '19 at 3:36
• 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. – Michael McCain Mar 15 '19 at 3:42
• @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. – Carl Woll Mar 15 '19 at 4:08

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.

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

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",
TextAlignment -> Left, Scope -> SelectionCell],
MenuItem["Align at 25%", TextAlignment -> -0.5,
Scope -> SelectionCell],
Scope -> SelectionCell],
MenuItem["Align at 75%", TextAlignment -> 0.5,
Scope -> SelectionCell],
Scope -> SelectionCell],
Scope -> SelectionCell]}]}]
]


Right/Option click on that text:

then picking center:

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.

• 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? – Kuba Aug 27 '18 at 8:45
• @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. – b3m2a1 Aug 27 '18 at 8:47
• 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. – Kuba Aug 27 '18 at 8:49
• @Kuba another option of course is to use FrontEndSetOptions but the difficulty there is in finding the cursor cell. That's always been a pain. – b3m2a1 Aug 27 '18 at 8:50
• @MichaelMcCain it’s possible to do using DeveloperCellInformation` 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. – b3m2a1 Sep 2 '18 at 2:08