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

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