JOGL v2.6.0-rc-20250712
JOGL, High-Performance Graphics Binding for Java™ (public API).
NEWTKeyAdapter.java
Go to the documentation of this file.
1/**
2 * Copyright 2010 JogAmp Community. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without modification, are
5 * permitted provided that the following conditions are met:
6 *
7 * 1. Redistributions of source code must retain the above copyright notice, this list of
8 * conditions and the following disclaimer.
9 *
10 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
11 * of conditions and the following disclaimer in the documentation and/or other materials
12 * provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY JogAmp Community ``AS IS'' AND ANY EXPRESS OR IMPLIED
15 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
16 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JogAmp Community OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
19 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
20 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
21 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
22 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 *
24 * The views and conclusions contained in the software and documentation are those of the
25 * authors and should not be interpreted as representing official policies, either expressed
26 * or implied, of JogAmp Community.
27 */
28
29package com.jogamp.opengl.test.junit.util;
30
31import java.util.ArrayList;
32import java.util.EventObject;
33import java.util.List;
34
35import com.jogamp.newt.event.InputEvent;
36import com.jogamp.newt.event.KeyAdapter;
37import com.jogamp.newt.event.KeyEvent;
38
39public class NEWTKeyAdapter extends KeyAdapter implements KeyEventCountAdapter {
40
41 String prefix;
42 int keyPressed, keyReleased;
43 int keyPressedAR, keyReleasedAR;
44 int consumed;
45 boolean pressed;
46 List<EventObject> queue = new ArrayList<EventObject>();
47 boolean verbose = true;
48
49 public NEWTKeyAdapter(final String prefix) {
50 this.prefix = prefix;
51 reset();
52 }
53
54 public synchronized void setVerbose(final boolean v) { verbose = v; }
55
56 public synchronized boolean isPressed() {
57 return pressed;
58 }
59
60 public synchronized int getCount() {
61 return keyReleased;
62 }
63
64 public synchronized int getConsumedCount() {
65 return consumed;
66 }
67
68 public synchronized int getKeyPressedCount(final boolean autoRepeatOnly) {
69 return autoRepeatOnly ? keyPressedAR: keyPressed;
70 }
71
72 public synchronized int getKeyReleasedCount(final boolean autoRepeatOnly) {
73 return autoRepeatOnly ? keyReleasedAR: keyReleased;
74 }
75
76 public synchronized List<EventObject> copyQueue() {
77 return new ArrayList<EventObject>(queue);
78 }
79
80 public synchronized int getQueueSize() {
81 return queue.size();
82 }
83
84 public synchronized void reset() {
85 keyPressed = 0;
86 keyReleased = 0;
87 consumed = 0;
88 keyPressedAR = 0;
89 keyReleasedAR = 0;
90 pressed = false;
91 queue.clear();
92 }
93
94 public synchronized void keyPressed(final KeyEvent e) {
95 pressed = true;
96 keyPressed++;
97 if( 0 != ( e.getModifiers() & InputEvent.AUTOREPEAT_MASK ) ) {
98 keyPressedAR++;
99 }
100 queue.add(e);
101 if( verbose ) {
102 System.err.println("KEY NEWT PRESSED ["+pressed+"]: "+prefix+", "+e);
103 }
104 }
105
106 public synchronized void keyReleased(final KeyEvent e) {
107 pressed = false;
108 keyReleased++;
109 if(e.isConsumed()) {
110 consumed++;
111 }
112 if( 0 != ( e.getModifiers() & InputEvent.AUTOREPEAT_MASK ) ) {
113 keyReleasedAR++;
114 }
115 queue.add(e);
116 if( verbose ) {
117 System.err.println("KEY NEWT RELEASED ["+pressed+"]: "+prefix+", "+e);
118 }
119 }
120
121 public String toString() { return prefix+"[pressed "+pressed+", keysPressed "+keyPressed+" (AR "+keyPressedAR+"), keyReleased "+keyReleased+" (AR "+keyReleasedAR+"), consumed "+consumed+"]"; }
122}
123
final int getModifiers()
Return the modifier bits of this event, e.g.
static final int AUTOREPEAT_MASK
Event is caused by auto-repeat.
Definition: InputEvent.java:71
final boolean isConsumed()
Returns true if this events has been consumed, otherwise false.
Definition: NEWTEvent.java:105
synchronized int getKeyReleasedCount(final boolean autoRepeatOnly)
synchronized List< EventObject > copyQueue()
synchronized void setVerbose(final boolean v)
Instance starts in verbose mode, call w/ false to disable verbosity.
synchronized void keyPressed(final KeyEvent e)
A key has been pressed, excluding auto-repeat modifier keys.
synchronized int getKeyPressedCount(final boolean autoRepeatOnly)
synchronized void keyReleased(final KeyEvent e)
A key has been released, excluding auto-repeat modifier keys.