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       * </p>
62       *
63       * @param keyCode the key code
64       */
65      public void press(final int keyCode) {
66          if (keyCode >= KeyboardEvent.DOM_VK_A && keyCode <= KeyboardEvent.DOM_VK_Z) {
67              throw new IllegalArgumentException("For key code " + keyCode + ", use type(char) instead");
68          }
69          keys_.add(new Object[] {keyCode, true});
70      }
71  
72      /**
73       * Releases the specified key code.
74       * <p>
75       * An example of predefined values is
76       * {@link org.htmlunit.javascript.host.event.KeyboardEvent#DOM_VK_PAGE_DOWN}.
77       * </p>
78       *
79       * @param keyCode the key code.
80       */
81      public void release(final int keyCode) {
82          keys_.add(new Object[] {keyCode, false});
83      }
84  
85      /**
86       * Clears all keys.
87       */
88      public void clear() {
89          keys_.clear();
90      }
91  
92      /**
93       * Returns the keys.
94       * <p>
95       * If the length of the item is 1, then it is a character.
96       * If the length of the item is 2, the first is the key code, the second is boolean whether pressing or not
97       * </p>
98       *
99       * @return the keys
100      */
101     List<Object[]> getKeys() {
102         return keys_;
103     }
104 
105     /**
106      * Returns whether typing should start at the text end or not.
107      * @return whether typing should start at the text end or not
108      */
109     public boolean isStartAtEnd() {
110         return startAtEnd_;
111     }
112 }