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

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