Skip to content

File tree

3 files changed

+307
-0
lines changed

3 files changed

+307
-0
lines changed
Lines changed: 205 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,205 @@
1+
/*
2+
* #%L
3+
* SciJava Common shared library for SciJava software.
4+
* %%
5+
* Copyright (C) 2009 - 2014 Board of Regents of the University of
6+
* Wisconsin-Madison, Broad Institute of MIT and Harvard, and Max Planck
7+
* Institute of Molecular Cell Biology and Genetics.
8+
* %%
9+
* Redistribution and use in source and binary forms, with or without
10+
* modification, are permitted provided that the following conditions are met:
11+
*
12+
* 1. Redistributions of source code must retain the above copyright notice,
13+
* this list of conditions and the following disclaimer.
14+
* 2. Redistributions in binary form must reproduce the above copyright notice,
15+
* this list of conditions and the following disclaimer in the documentation
16+
* and/or other materials provided with the distribution.
17+
*
18+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
22+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28+
* POSSIBILITY OF SUCH DAMAGE.
29+
* #L%
30+
*/
31+
32+
package org.scijava.input;
33+
34+
import java.util.HashSet;
35+
36+
import org.scijava.display.Display;
37+
import org.scijava.display.event.DisplayDeletedEvent;
38+
import org.scijava.display.event.input.KyPressedEvent;
39+
import org.scijava.display.event.input.KyReleasedEvent;
40+
import org.scijava.display.event.input.MsExitedEvent;
41+
import org.scijava.display.event.input.MsMovedEvent;
42+
import org.scijava.display.event.input.MsPressedEvent;
43+
import org.scijava.display.event.input.MsReleasedEvent;
44+
import org.scijava.event.EventHandler;
45+
import org.scijava.event.EventService;
46+
import org.scijava.plugin.Parameter;
47+
import org.scijava.plugin.Plugin;
48+
import org.scijava.service.AbstractService;
49+
import org.scijava.service.Service;
50+
51+
/**
52+
* Default implementation of {@link InputService}.
53+
*
54+
* @author Curtis Rueden
55+
*/
56+
@Plugin(type = Service.class)
57+
public class DefaultInputService extends AbstractService implements
58+
InputService
59+
{
60+
61+
@Parameter
62+
private EventService eventService;
63+
64+
private InputModifiers modifiers;
65+
66+
private boolean altDown = false;
67+
private boolean altGrDown = false;
68+
private boolean ctrlDown = false;
69+
private boolean metaDown = false;
70+
private boolean shiftDown = false;
71+
72+
private HashSet<KeyCode> pressedKeys = new HashSet<KeyCode>();
73+
74+
private HashSet<Integer> buttonsDown = new HashSet<Integer>();
75+
76+
private Display<?> display;
77+
private int lastX = -1, lastY = -1;
78+
79+
// -- InputService methods --
80+
81+
@Override
82+
public EventService getEventService() {
83+
return eventService;
84+
}
85+
86+
@Override
87+
public InputModifiers getModifiers() {
88+
return modifiers;
89+
}
90+
91+
@Override
92+
public boolean isAltDown() {
93+
return altDown;
94+
}
95+
96+
@Override
97+
public boolean isAltGrDown() {
98+
return altGrDown;
99+
}
100+
101+
@Override
102+
public boolean isCtrlDown() {
103+
return ctrlDown;
104+
}
105+
106+
@Override
107+
public boolean isMetaDown() {
108+
return metaDown;
109+
}
110+
111+
@Override
112+
public boolean isShiftDown() {
113+
return shiftDown;
114+
}
115+
116+
@Override
117+
public boolean isKeyDown(final KeyCode code) {
118+
return pressedKeys.contains(code);
119+
}
120+
121+
@Override
122+
public Display<?> getDisplay() {
123+
return display;
124+
}
125+
126+
@Override
127+
public int getX() {
128+
return lastX;
129+
}
130+
131+
@Override
132+
public int getY() {
133+
return lastY;
134+
}
135+
136+
@Override
137+
public boolean isButtonDown(final int button) {
138+
return buttonsDown.contains(button);
139+
}
140+
141+
// -- Event handlers --
142+
143+
@EventHandler
144+
public void onEvent(final KyPressedEvent evt) {
145+
modifiers = evt.getModifiers();
146+
altDown = modifiers.isAltDown();
147+
altGrDown = modifiers.isAltGrDown();
148+
ctrlDown = modifiers.isCtrlDown();
149+
metaDown = modifiers.isMetaDown();
150+
shiftDown = modifiers.isShiftDown();
151+
pressedKeys.add(evt.getCode());
152+
}
153+
154+
@EventHandler
155+
public void onEvent(final KyReleasedEvent evt) {
156+
modifiers = evt.getModifiers();
157+
altDown = modifiers.isAltDown();
158+
altGrDown = modifiers.isAltGrDown();
159+
ctrlDown = modifiers.isCtrlDown();
160+
metaDown = modifiers.isMetaDown();
161+
shiftDown = modifiers.isShiftDown();
162+
pressedKeys.remove(evt.getCode());
163+
}
164+
165+
@EventHandler
166+
protected void onEvent(final MsMovedEvent evt) {
167+
updateCoords(evt.getDisplay(), evt.getX(), evt.getY());
168+
}
169+
170+
@EventHandler
171+
protected void onEvent(@SuppressWarnings("unused") final MsExitedEvent evt) {
172+
clearCoords();
173+
}
174+
175+
@EventHandler
176+
protected void onEvent(final MsPressedEvent evt) {
177+
modifiers = evt.getModifiers();
178+
buttonsDown.add(evt.getButton());
179+
}
180+
181+
@EventHandler
182+
protected void onEvent(final MsReleasedEvent evt) {
183+
modifiers = evt.getModifiers();
184+
buttonsDown.remove(evt.getButton());
185+
}
186+
187+
@EventHandler
188+
protected void onEvent(final DisplayDeletedEvent evt) {
189+
if (display != evt.getObject()) return;
190+
clearCoords();
191+
}
192+
193+
// -- Helper methods --
194+
195+
private void updateCoords(final Display<?> d, final int x, final int y) {
196+
display = d;
197+
lastX = x;
198+
lastY = y;
199+
}
200+
201+
private void clearCoords() {
202+
updateCoords(null, -1, -1);
203+
}
204+
205+
}
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
/*
2+
* #%L
3+
* SciJava Common shared library for SciJava software.
4+
* %%
5+
* Copyright (C) 2009 - 2014 Board of Regents of the University of
6+
* Wisconsin-Madison, Broad Institute of MIT and Harvard, and Max Planck
7+
* Institute of Molecular Cell Biology and Genetics.
8+
* %%
9+
* Redistribution and use in source and binary forms, with or without
10+
* modification, are permitted provided that the following conditions are met:
11+
*
12+
* 1. Redistributions of source code must retain the above copyright notice,
13+
* this list of conditions and the following disclaimer.
14+
* 2. Redistributions in binary form must reproduce the above copyright notice,
15+
* this list of conditions and the following disclaimer in the documentation
16+
* and/or other materials provided with the distribution.
17+
*
18+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
22+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28+
* POSSIBILITY OF SUCH DAMAGE.
29+
* #L%
30+
*/
31+
32+
package org.scijava.input;
33+
34+
import org.scijava.display.Display;
35+
import org.scijava.display.event.input.MsButtonEvent;
36+
import org.scijava.event.EventService;
37+
import org.scijava.service.SciJavaService;
38+
39+
/**
40+
* Interface for service that tracks the current status of input devices
41+
* (keyboard and mouse in particular).
42+
*
43+
* @author Barry DeZonia
44+
* @author Curtis Rueden
45+
*/
46+
public interface InputService extends SciJavaService {
47+
48+
EventService getEventService();
49+
50+
InputModifiers getModifiers();
51+
52+
boolean isAltDown();
53+
54+
boolean isAltGrDown();
55+
56+
boolean isCtrlDown();
57+
58+
boolean isMetaDown();
59+
60+
boolean isShiftDown();
61+
62+
boolean isKeyDown(KeyCode code);
63+
64+
/**
65+
* Gets the display associated with the last observed mouse cursor.
66+
*
67+
* @return The display in question, or null if the display has been deleted,
68+
* or the mouse cursor is outside all known displays, or no mouse
69+
* events have ever been observed.
70+
*/
71+
Display<?> getDisplay();
72+
73+
/**
74+
* Gets the last observed X coordinate of the mouse cursor, relative to a
75+
* specific display.
76+
*
77+
* @see #getDisplay()
78+
*/
79+
int getX();
80+
81+
/**
82+
* Gets the last observed Y coordinate of the mouse cursor, relative to a
83+
* specific display.
84+
*
85+
* @see #getDisplay()
86+
*/
87+
int getY();
88+
89+
/**
90+
* Gets whether the given mouse button is currently pressed.
91+
*
92+
* @param button One of:
93+
* <ul>
94+
* <li>{@link MsButtonEvent#LEFT_BUTTON}</li>
95+
* <li>{@link MsButtonEvent#MIDDLE_BUTTON}</li>
96+
* <li>{@link MsButtonEvent#RIGHT_BUTTON}</li>
97+
* </ul>
98+
*/
99+
boolean isButtonDown(int button);
100+
101+
}

src/test/java/org/scijava/ContextCreationTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ public void testFull() {
7878
org.scijava.console.DefaultConsoleService.class,
7979
org.scijava.display.DefaultDisplayService.class,
8080
org.scijava.event.DefaultEventHistory.class,
81+
org.scijava.input.DefaultInputService.class,
8182
org.scijava.io.DefaultIOService.class,
8283
org.scijava.io.DefaultRecentFileService.class,
8384
org.scijava.menu.DefaultMenuService.class,

0 commit comments

Comments
 (0)