You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

advanced-shortcuts.asciidoc 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. ---
  2. title: Shortcut Keys
  3. order: 5
  4. layout: page
  5. ---
  6. [[advanced.shortcuts]]
  7. = Shortcut Keys
  8. Vaadin provides simple ways to define shortcut keys for field components, as
  9. well as to a default button, and a lower-level generic shortcut API based on
  10. actions.
  11. A __shortcut__ is an action that is executed when a key or key combination is
  12. pressed within a specific scope in the UI. The scope can be the entire
  13. [classname]#UI# or a [classname]#Window# inside it.
  14. [[advanced.shortcuts.defaultbutton]]
  15. == Shortcut Keys for Default Buttons
  16. You can add a __click shortcut__ to a button to set it as "default" button;
  17. pressing the defined key, typically kbd:[Enter], in any component in the scope
  18. (sub-window or UI) causes a click event for the button to be fired.
  19. You can define a click shortcut with the [methodname]#setClickShortcut()#
  20. shorthand method:
  21. [source, java]
  22. ----
  23. // Have an OK button and set it as the default button
  24. Button ok = new Button("OK");
  25. ok.setClickShortcut(KeyCode.ENTER);
  26. ok.addStyleName(ValoTheme.BUTTON_PRIMARY);
  27. ----
  28. The [methodname]#setClickShortcut()# is a shorthand method to create, add, and
  29. manage a [classname]#ClickShortcut#, rather than to add it with
  30. [methodname]#addShortcutListener()#.
  31. Themes offer special button styles to show that a button is special. In the Valo
  32. theme, you can use the [literal]#++BUTTON_PRIMARY++# style name. The result can
  33. be seen in <<figure.advanced.shortcuts.defaultbutton>>.
  34. [[figure.advanced.shortcuts.defaultbutton]]
  35. .Default Button with Click Shortcut
  36. image::img/shortcut-defaultbutton.png[]
  37. [[advanced.shortcuts.focus]]
  38. == Field Focus Shortcuts
  39. You can define a shortcut key that sets the focus to any focusable component (implements [interface]#Focusable#), usually field components, by adding a
  40. [interface]#FocusShortcut# as a shortcut listener to the component.
  41. The constructor of the [classname]#FocusShortcut# takes the focusable component as
  42. its first parameter, followed by the key code, and an optional list of modifier
  43. keys, as listed in <<advanced.shortcuts.keycodes>>.
  44. [source, java]
  45. ----
  46. // A field with Alt+N bound to it
  47. TextField name = new TextField("Name (Alt+N)");
  48. name.addShortcutListener(
  49. new FocusShortcut(name, KeyCode.N,
  50. ModifierKey.ALT));
  51. layout.addComponent(name);
  52. ----
  53. You can also specify the shortcut by a shorthand notation, where the shortcut
  54. key is indicated with an ampersand ( [literal]#++&++#).
  55. [source, java]
  56. ----
  57. // A field with Alt+A bound to it, using shorthand notation
  58. TextField address = new TextField("Address (Alt+A)");
  59. address.addShortcutListener(
  60. new FocusShortcut(address, "&Address"));
  61. ----
  62. This is especially useful for internationalization, so that you can determine
  63. the shortcut key from the localized string.
  64. [[advanced.shortcuts.actions]]
  65. == Generic Shortcut Actions
  66. Shortcut keys can be defined as __actions__ using the
  67. [classname]#ShortcutAction# class. It extends the generic [classname]#Action#
  68. class that is used for example in [classname]#Tree# and [classname]#Table# for
  69. context menus. Currently, the only classes that accept
  70. [classname]##ShortcutAction##s are [classname]#Window# and [classname]#Panel#.
  71. To handle key presses, you need to define an action handler by implementing the
  72. [classname]#Handler# interface. The interface has two methods that you need to
  73. implement: [methodname]#getActions()# and [methodname]#handleAction()#.
  74. The [methodname]#getActions()# method must return an array of
  75. [classname]#Action# objects for the component, specified with the second
  76. parameter for the method, the [parameter]#sender# of an action. For a keyboard
  77. shortcut, you use a [classname]#ShortcutAction#. The implementation of the
  78. method could be following:
  79. [source, java]
  80. ----
  81. // Have the unmodified Enter key cause an event
  82. Action action_ok = new ShortcutAction("Default key",
  83. ShortcutAction.KeyCode.ENTER, null);
  84. // Have the C key modified with Alt cause an event
  85. Action action_cancel = new ShortcutAction("Alt+C",
  86. ShortcutAction.KeyCode.C,
  87. new int[] { ShortcutAction.ModifierKey.ALT });
  88. Action[] actions = new Action[] {action_cancel, action_ok};
  89. public Action[] getActions(Object target, Object sender) {
  90. if (sender == myPanel)
  91. return actions;
  92. return null;
  93. }
  94. ----
  95. The returned [classname]#Action# array may be static or you can create it
  96. dynamically for different senders according to your needs.
  97. The constructor of [classname]#ShortcutAction# takes a symbolic caption for the
  98. action; this is largely irrelevant for shortcut actions in their current
  99. implementation, but might be used later if implementors use them both in menus
  100. and as shortcut actions. The second parameter is the key code and the third a
  101. list of modifier keys, which are listed in <<advanced.shortcuts.keycodes>>.
  102. The following example demonstrates the definition of a default button for a user
  103. interface, as well as a normal shortcut key, kbd:[Alt+C] for clicking the
  104. [guibutton]#Cancel# button.
  105. [source, java]
  106. ----
  107. public class DefaultButtonExample extends CustomComponent
  108. implements Handler {
  109. // Define and create user interface components
  110. Panel panel = new Panel("Login");
  111. FormLayout formlayout = new FormLayout();
  112. TextField username = new TextField("Username");
  113. TextField password = new TextField("Password");
  114. HorizontalLayout buttons = new HorizontalLayout();
  115. // Create buttons and define their listener methods.
  116. Button ok = new Button("OK", event -> okHandler());
  117. Button cancel = new Button("Cancel", event -> cancelHandler());
  118. // Have the unmodified Enter key cause an event
  119. Action action_ok = new ShortcutAction("Default key",
  120. ShortcutAction.KeyCode.ENTER, null);
  121. // Have the C key modified with Alt cause an event
  122. Action action_cancel = new ShortcutAction("Alt+C",
  123. ShortcutAction.KeyCode.C,
  124. new int[] { ShortcutAction.ModifierKey.ALT });
  125. public DefaultButtonExample() {
  126. // Set up the user interface
  127. setCompositionRoot(panel);
  128. panel.addComponent(formlayout);
  129. formlayout.addComponent(username);
  130. formlayout.addComponent(password);
  131. formlayout.addComponent(buttons);
  132. buttons.addComponent(ok);
  133. buttons.addComponent(cancel);
  134. // Set focus to username
  135. username.focus();
  136. // Set this object as the action handler
  137. panel.addActionHandler(this);
  138. }
  139. /**
  140. * Retrieve actions for a specific component. This method
  141. * will be called for each object that has a handler; in
  142. * this example just for login panel. The returned action
  143. * list might as well be static list.
  144. */
  145. public Action[] getActions(Object target, Object sender) {
  146. System.out.println("getActions()");
  147. return new Action[] { action_ok, action_cancel };
  148. }
  149. /**
  150. * Handle actions received from keyboard. This simply directs
  151. * the actions to the same listener methods that are called
  152. * with ButtonClick events.
  153. */
  154. public void handleAction(Action action, Object sender,
  155. Object target) {
  156. if (action == action_ok) {
  157. okHandler();
  158. }
  159. if (action == action_cancel) {
  160. cancelHandler();
  161. }
  162. }
  163. public void okHandler() {
  164. // Do something: report the click
  165. formlayout.addComponent(new Label("OK clicked. "
  166. + "User=" + username.getValue() + ", password="
  167. + password.getValue()));
  168. }
  169. public void cancelHandler() {
  170. // Do something: report the click
  171. formlayout.addComponent(new Label("Cancel clicked. User="
  172. + username.getValue() + ", password="
  173. + password.getValue()));
  174. }
  175. }
  176. ----
  177. Notice that the keyboard actions can currently be attached only to
  178. [classname]##Panel##s and [classname]##Window##s. This can cause problems if you
  179. have components that require a certain key. For example, multi-line
  180. [classname]#TextField# requires the kbd:[Enter] key. There is currently no way to
  181. filter the shortcut actions out while the focus is inside some specific
  182. component, so you need to avoid such conflicts.
  183. [[advanced.shortcuts.keycodes]]
  184. == Supported Key Codes and Modifier Keys
  185. The shortcut key definitions require a key code to identify the pressed key and
  186. modifier keys, such as kbd:[Shift], kbd:[Alt], or kbd:[Ctrl], to specify a key combination.
  187. The key codes are defined in the [classname]#ShortcutAction.KeyCode# interface
  188. and are:
  189. Keys [parameter]#A# to [parameter]#Z#:: Normal letter keys
  190. [parameter]#F1# to [parameter]#F12#:: Function keys
  191. [parameter]#BACKSPACE#, [parameter]#DELETE#, [parameter]#ENTER#, [parameter]#ESCAPE#, [parameter]#INSERT#, [parameter]#TAB#:: Control keys
  192. [parameter]#NUM0# to [parameter]#NUM9#:: Number pad keys
  193. [parameter]#ARROW_DOWN#, [parameter]#ARROW_UP#, [parameter]#ARROW_LEFT#, [parameter]#ARROW_RIGHT#:: Arrow keys
  194. [parameter]#HOME#, [parameter]#END#, [parameter]#PAGE_UP#, [parameter]#PAGE_DOWN#:: Other movement keys
  195. Modifier keys are defined in [classname]#ShortcutAction.ModifierKey# and are:
  196. [parameter]#ModifierKey.ALT#:: kbd:[Alt] key
  197. [parameter]#ModifierKey.CTRL#:: kbd:[Ctrl] key
  198. [parameter]#ModifierKey.SHIFT#:: kbd:[Shift] key
  199. All constructors and methods accepting modifier keys take them as a variable
  200. argument list following the key code, separated with commas. For example, the
  201. following defines a kbd:[Ctrl+Shift+N] key combination for a shortcut.
  202. [source, java]
  203. ----
  204. TextField name = new TextField("Name (Ctrl+Shift+N)");
  205. name.addShortcutListener(
  206. new FocusShortcut(name, KeyCode.N,
  207. ModifierKey.CTRL,
  208. ModifierKey.SHIFT));
  209. ----
  210. === Supported Key Combinations
  211. The actual possible key combinations vary greatly between browsers, as most
  212. browsers have a number of built-in shortcut keys, which can not be used in web
  213. applications. For example, Mozilla Firefox allows binding almost any key
  214. combination, while Opera does not even allow binding kbd:[Alt] shortcuts. Other
  215. browsers are generally in between these two. Also, the operating system can
  216. reserve some key combinations and some computer manufacturers define their own
  217. system key combinations.