Applicable: | This tutorial applies to Confluence 5.9.1 and higher |
Level of experience: | Beginner |
Time estimate | It should take you less than 1 hour to complete this tutorial. |
This tutorial is for 'beginner' level, so you can follow it even if you have never developed a plugin before. Our tutorials are classified as 'beginner', 'intermediate' and 'advanced'.
This tutorial shows you how to use a Keyboard shortcut module in your plugin to add keyboard shortcuts to different parts of the Confluence UI and navigation between pages. To do this, you create a very simple Confluence plugin consisting of the following components:
A single JAR file contains all the components. Each component is further discussed in the examples later on this page.
This plugin is designed to show two ways of adding new keyboard shortcuts to Confluence:
To complete this tutorial, you should work through the Atlassian plugin SDK tutorial first.
We encourage you to work through this tutorial. If you want to skip ahead or check your work when you have finished, you can find the plugin source code on Atlassian Bitbucket. Alternatively, you can download the source as a ZIP archive.
To clone the repository, run the following command:
1 2git clone git@bitbucket.org:atlassian_tutorial/adding-confluence-keyboard-shortcuts.git
About these instructions
You can use any supported combination of operating system and IDE to create this plugin. These instructions were written using Intellij IDEA 2017.2 on macOS Sierra. If you are using another operating system or IDE combination, you should use the equivalent operations for your specific environment. This tutorial was last tested with Confluence 6.7.1 using the Atlassian SDK 6.3.10.
In this step, you'll create a plugin skeleton using atlas-
commands. Because you won't need some
of the files created in the skeleton, you'll also delete them in this step.
Open a Terminal and navigate to your workspace directory.
To create a Confluence plugin skeleton, run the following command:
1 2atlas-create-confluence-plugin
To identify your plugin, enter the following information.
group-id |
|
artifact-id |
|
version |
|
package |
|
Confirm your entries with Y
or y
.
Navigate to the project directory created in the previous step.
1 2cd adding-confluence-keyboard-shortcuts/
Delete the test directories.
Setting up testing for your plugin isn't part of this tutorial. Run the following commands to delete the generated test skeleton:
1 2rm -rf ./src/test/java rm -rf ./src/test/resources/
Delete the unneeded Java class files.
1 2rm -rf ./src/main/java/com/example/plugins/tutorial/confluence/*
atlassian-plugin.xml
fileIn this step, you register the plugin module in your plugin descriptor, that is atlassian-plugin.xml
file.
atlassian-plugin.xml
file, add the following code to your between the <atlassian-plugin>
tags, but below the <plugin-info>
tag group.1 2<keyboard-shortcut key="about-confluence-page" i18n-name-key="keyboard.shortcut.go.to.about.page"> <order>200</order> <description key="keyboard.shortcut.go.to.about.page.desc"/> <shortcut>ga</shortcut> <operation type="goTo">/aboutconfluencepage.action</operation> <context>global</context> </keyboard-shortcut>
Let's break down that XML code. In this example we create a keyboard shortcut that navigates to Confluence About page when triggered.
To do this, we've done a number of things.
In the previous code sample, this line involves 2 attributes:
1 2<keyboard-shortcut key="about-confluence-page" i18n-name-key="keyboard.shortcut.go.to.about.page">
key="about-confluence-page"
sets an internal name for the new item.i18n-name-key="keyboard.shortcut.go.to.about.page"
is the internationalization key
for the item. It is displayed in the admin section as the name of the module in the plugin.Examine these lines of code:
1 2<order>200</order> <description key="keyboard.shortcut.go.to.about.page.desc"/>
In Confluence, if you go to the Help menu, and then select Keyboard Shortcuts, you will see a dialog box with list of keyboard shortcuts.
Each item has a value for order
between 10 and 100. A smaller value means the label for your
keyboard shortcut appears higher in the list. A value of 200 means that the item is almost certainly
placed at the bottom of the list.
The description contains an attribute key
. This is the internationalization key that you can find in a
properties file (explained later in the tutorial). The value of this element defines the label for
the keyboard shortcut in this dialog box.
This line defines how the keyboard shortcut is triggered: <shortcut>ga</shortcut>
This example is triggered by pressing the keys in this order: "g", and then "a".
This line defines what the keyboard shortcut does: <operation type="goTo">/aboutconfluencepage.action</operation>
In this case, the window.location
is changed to a path below the server base URL. So, for a Confluence
installation at http://locahost:1990/confluence, this will correspond to http://localhost:1990/confluence/aboutconfluencepage.action
Find more descriptions of operation types on Keyboard Shortcut Module page.
In this line, we allow the keyboard shortcuts to be triggered globally within Confluence: <context>global</context>
.
The context tag also accepts the page or content values for other situations.
We will want to specify a text label to display in our Confluence keyboard shortcuts. You could just
hard-code this information into your atlassian-plugin.xml
file. However, by adding it in a new resource file,
we can make our plugin compatible with internationalization.
To do so, simply add a following lines into
adding-confluence-keyboard-shortcuts.properties
file:
1 2keyboard.shortcut.go.to.about.page = About page keyboard shortcut keyboard.shortcut.go.to.about.page.desc = Navigate to About page
The first line – keyboard.shortcut.go.to.about.page
– describes the module name in Cog wheel > Add-ons.
Displaying friendly names rather than module keys can be useful when there are many modules in a Confluence plugin.
The second line – keyboard.shortcut.go.to.about.page.desc
– is the label for the keyboard shortcut in
the Keyboard Shortcut dialog displayed earlier.
SDK automatically generates a reference to the resource file (adding-confluence-keyboard-shortcuts.properties
)
in our atlassian-plugin.xml
file:
1 2<resource type="i18n" name="i18n" location="adding-confluence-keyboard-shortcuts.properties" />
To know more about internationalization, see our Confluence documentation on the topic.
To compile the plugin project, and then launch a local instance of Confluence, run the following command:
1 2atlas-run
After Confluence loads, go to the local instance with this URL:
1 2http://localhost:1990/confluence/
Log in with username "admin" and password "admin".
Type "g", and then "a". Your browser displays the following.
Go back to Dashboard, and then select Keyboard Shortcuts from the Help menu (or just enter "?"). You'll see your new keyboard shortcut has been added.
In this step, you add a new keyboard shortcut that triggers the User Menu to be toggled open or closed when you press Alt+U.
In your atlassian-plugin.xml
file, add a new keyboard-shortcut
code block as follows:
1 2<keyboard-shortcut key="scroll-and-toggle-user-menu" i18n-name-key="keyboard.shortcut.toggle.user.menu"> <order>200</order> <description key="keyboard.shortcut.toggle.user.menu.desc"/> <shortcut>[Alt+U]</shortcut> <operation type="moveToAndClick">#user-menu-link</operation> <context>global</context> </keyboard-shortcut>
This is very similar to code block in step 2, except for two key points:
moveToAndClick
that should be fairly self explanatory.
#user-menu-link
is a CSS selector for the id attribute on the User menu anchor element.To rebuild your plugin, run atlas-package
command, and
QuickReload
automatically reloads your plugin for you.
Go to your local Confluence installation and press Alt+U several times. You should see the User drop-down opening and closing.
This example shows how you can quickly add your own keyboard shortcuts to enhance productivity or accessibility.
Congratulations! You have completed this tutorial.
Rate this page: