Hey everyone! I’m trying to figure out how to make a keyboard shortcut that changes the text color in Google Docs. I’ve got the color-changing part working with a menu item, but I’m stuck on the shortcut part.
I found some code online that’s supposed to help, but I’m not sure how to use it. Here’s what I’ve tried:
function openDoc() {
let ui = DocumentApp.getUI();
ui.createMenu('ColorMenu')
.addItem('ChangeHue', 'applyHue')
.addToUi();
let doc = DocumentApp.getActiveDocument();
// Not sure if this is right
doc.addKeyboardShortcut(function(e) {
if (e.ctrlKey && e.keyCode === 81) {
ui.alert('Shortcut pressed!');
}
});
}
function applyHue() {
let doc = DocumentApp.getActiveDocument();
let selection = doc.getSelection();
if (selection) {
// Code to change text color
}
}
I’m confused about where to put the shortcut code and how to make it work. Also, do I need to do something special to activate it? Any help would be awesome!
I’ve implemented custom hotkeys in Google Docs before, and I can offer some insights. The approach you’re taking won’t work as Google Docs doesn’t support direct event listeners for keyboard shortcuts. Instead, you should utilize the setShortcut() method on menu items.
Here’s a modified version of your code that should work:
function onOpen() {
DocumentApp.getUi().createMenu('ColorMenu')
.addItem('Change Text Color', 'applyHue')
.addToUi();
}
function applyHue() {
var selection = DocumentApp.getActiveDocument().getSelection();
if (selection) {
var elements = selection.getSelectedElements();
for (var i = 0; i < elements.length; i++) {
elements[i].getElement().asText().setForegroundColor('#FF0000');
}
}
}
After setting this up, go to Tools > Script editor, then Edit > Current project’s triggers. Add a new trigger for the onOpen function to run when the document opens. This should create your custom menu with the shortcut option available.
hey jess, i’ve messed with custom shortcuts before. looks like ur on the right track! one thing tho, google docs doesn’t support direct keyboard event listeners like that. instead, try using the menu item’s setShortcut() method. something like:
.addItem(‘ChangeHue’, ‘applyHue’)
.setShortcut(‘Ctrl+Q’)
hope that helps!
I’ve actually been working on something similar recently! Here’s what I found works:
Instead of trying to add a keyboard shortcut directly, you can use Google Docs’ built-in Tools > Preferences menu to set up custom shortcuts.
First, create your custom menu and function as you have:
function onOpen() {
DocumentApp.getUi().createMenu(‘ColorMenu’)
.addItem(‘Change Text Color’, ‘applyHue’)
.addToUi();
}
function applyHue() {
// Your color change code here
}
Then, go to Tools > Preferences in Google Docs. In the ‘Automatic substitution’ section, you can set up a text shortcut that will trigger your custom function. For example, you could set ‘/red’ to expand to ‘@@CHANGECOLOR@@’.
Finally, modify your applyHue function to check for this trigger:
function applyHue() {
var doc = DocumentApp.getActiveDocument();
var cursor = doc.getCursor();
if (cursor) {
var element = cursor.getElement();
var text = element.asText();
if (text.getText().endsWith(‘@@CHANGECOLOR@@’)) {
text.deleteText(text.getText().length - 14, text.getText().length - 1);
text.setForegroundColor(‘#FF0000’);
}
}
}
This way, whenever you type ‘/red’, it’ll change the text color to red. You can set up multiple shortcuts for different colors too!