View Javadoc
1   /*
2    * Copyright (c) 2002-2026 Gargoyle Software Inc.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * https://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software
10   * distributed under the License is distributed on an "AS IS" BASIS,
11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   * See the License for the specific language governing permissions and
13   * limitations under the License.
14   */
15  package org.htmlunit.html;
16  
17  import java.util.ArrayList;
18  import java.util.List;
19  
20  import org.htmlunit.javascript.host.event.KeyboardEvent;
21  
22  /**
23   * Keeps track of the typed keys.
24   *
25   * @author Ahmed Ashour
26   * @author Ronald Brill
27   */
28  public class Keyboard {
29  
30      private final List<Object[]> keys_ = new ArrayList<>();
31      private final boolean startAtEnd_;
32  
33      /**
34       * Creates a new instance.
35       */
36      public Keyboard() {
37          this(false);
38      }
39  
40      /**
41       * Creates a new instance, specifying whether typing should start at the text end or not.
42       * @param startAtEnd whether typing should start at the text end or not
43       */
44      public Keyboard(final boolean startAtEnd) {
45          startAtEnd_ = startAtEnd;
46      }
47  
48      /**
49       * Types the specified character.
50       * @param ch the character
51       */
52      public void type(final char ch) {
53          keys_.add(new Object[] {ch});
54      }
55  
56      /**
57       * Press the specified key code (without releasing it).
58       * <p>
59       * An example of predefined values is
60       * {@link org.htmlunit.javascript.host.event.KeyboardEvent#DOM_VK_PAGE_DOWN}.
61       *
62       * @param keyCode the key code
63       */
64      public void press(final int keyCode) {
65          if (keyCode >= KeyboardEvent.DOM_VK_A && keyCode <= KeyboardEvent.DOM_VK_Z) {
66              throw new IllegalArgumentException("For key code " + keyCode + ", use type(char) instead");
67          }
68          keys_.add(new Object[] {keyCode, true});
69      }
70  
71      /**
72       * Releases the specified key code.
73       * <p>
74       * An example of predefined values is
75       * {@link org.htmlunit.javascript.host.event.KeyboardEvent#DOM_VK_PAGE_DOWN}.
76       *
77       * @param keyCode the key code.
78       */
79      public void release(final int keyCode) {
80          keys_.add(new Object[] {keyCode, false});
81      }
82  
83      /**
84       * Clears all keys.
85       */
86      public void clear() {
87          keys_.clear();
88      }
89  
90      /**
91       * Returns the keys.
92       * <p>
93       * If the length of the item is 1, then it is a character.
94       * If the length of the item is 2, the first is the key code, the second is boolean whether pressing or not
95       *
96       * @return the keys
97       */
98      List<Object[]> getKeys() {
99          return keys_;
100     }
101 
102     /**
103      * Returns whether typing should start at the text end or not.
104      * @return whether typing should start at the text end or not
105      */
106     public boolean isStartAtEnd() {
107         return startAtEnd_;
108     }
109 }