Skip to content

Commit 4897374

Browse files
committed
Possible solution for #3808 by accumulating concurrent keystrokes
1 parent c4eb502 commit 4897374

File tree

3 files changed

+121
-25
lines changed

3 files changed

+121
-25
lines changed

ummisco.gama.opengl/src/ummisco/gama/opengl/view/NEWTLayeredDisplayMultiListener.java

+22-6
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
/*******************************************************************************************************
22
*
33
* NEWTLayeredDisplayMultiListener.java, in ummisco.gama.opengl, is part of the source code of the GAMA modeling and
4-
* simulation platform (v.1.9.2).
4+
* simulation platform (v.2.0.0).
55
*
66
* (c) 2007-2023 UMI 209 UMMISCO IRD/SU & Partners (IRIT, MIAT, TLU, CTU)
77
*
8-
* Visit https://github.com/gama-platform/gama for license information and contacts.
8+
* Visit https://github.com/gama-platform/gama2 for license information and contacts.
99
*
1010
********************************************************************************************************/
1111
package ummisco.gama.opengl.view;
@@ -117,10 +117,26 @@ public void keyPressed(final KeyEvent e) {
117117

118118
@Override
119119
public void keyReleased(final KeyEvent e) {
120-
// DEBUG.OUT("Key released: " + e);
121-
// if (!ok.get()) return;
122-
// delegate.keyReleased(e.getKeyChar(),
123-
// PlatformHelper.isMac() ? e.isMetaDown() : e.isControlDown() /* ?? GamaKeyBindings.ctrl(e) */);
120+
DEBUG.OUT("Key released in Newt listener: " + e);
121+
if (!ok.get()) return;
122+
if (e.isPrintableKey()) {
123+
delegate.keyReleased(e.getKeyChar(),
124+
PlatformHelper.isMac() ? e.isMetaDown() : e.isControlDown() /* ?? GamaKeyBindings.ctrl(e) */);
125+
} else if (e.getModifiers() == 0
126+
|| e.isAutoRepeat() && !e.isAltDown() && !e.isControlDown() && !e.isShiftDown() && !e.isMetaDown()) {
127+
delegate.specialKeyReleased(switch (e.getKeyCode()) {
128+
case KeyEvent.VK_UP -> IEventLayerListener.ARROW_UP;
129+
case KeyEvent.VK_DOWN -> IEventLayerListener.ARROW_DOWN;
130+
case KeyEvent.VK_LEFT -> IEventLayerListener.ARROW_LEFT;
131+
case KeyEvent.VK_RIGHT -> IEventLayerListener.ARROW_RIGHT;
132+
case KeyEvent.VK_PAGE_UP -> IEventLayerListener.KEY_PAGE_UP;
133+
case KeyEvent.VK_PAGE_DOWN -> IEventLayerListener.KEY_PAGE_DOWN;
134+
case KeyEvent.VK_ESCAPE -> IEventLayerListener.KEY_ESC;
135+
case KeyEvent.VK_ENTER -> IEventLayerListener.KEY_RETURN;
136+
case KeyEvent.VK_TAB -> IEventLayerListener.KEY_TAB;
137+
default -> 0;
138+
});
139+
}
124140
}
125141

126142
/**

ummisco.gama.ui.experiment/src/ummisco/gama/ui/views/displays/LayeredDisplayMultiListener.java

+43-13
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
/*******************************************************************************************************
22
*
33
* LayeredDisplayMultiListener.java, in ummisco.gama.ui.experiment, is part of the source code of the GAMA modeling and
4-
* simulation platform (v.1.9.2).
4+
* simulation platform (v.2.0.0).
55
*
66
* (c) 2007-2023 UMI 209 UMMISCO IRD/SU & Partners (IRIT, MIAT, TLU, CTU)
77
*
8-
* Visit https://github.com/gama-platform/gama for license information and contacts.
8+
* Visit https://github.com/gama-platform/gama2 for license information and contacts.
99
*
1010
********************************************************************************************************/
1111
package ummisco.gama.ui.views.displays;
1212

13+
import java.util.HashSet;
14+
import java.util.Set;
1315
import java.util.function.Consumer;
1416

1517
import org.eclipse.swt.SWT;
@@ -18,9 +20,9 @@
1820
import msi.gama.common.interfaces.IDisplaySurface;
1921
import msi.gama.runtime.GAMA;
2022
import msi.gama.runtime.PlatformHelper;
23+
import ummisco.gama.dev.utils.DEBUG;
2124
import ummisco.gama.ui.utils.WorkbenchHelper;
2225
import ummisco.gama.ui.views.WorkaroundForIssue1353;
23-
import ummisco.gama.dev.utils.DEBUG;
2426

2527
/**
2628
* The listener interface for receiving layeredDisplayMulti events. The class that is interested in processing a
@@ -33,7 +35,7 @@
3335
public class LayeredDisplayMultiListener {
3436

3537
static {
36-
DEBUG.OFF();
38+
DEBUG.ON();
3739
}
3840

3941
/** The view. */
@@ -60,6 +62,12 @@ public class LayeredDisplayMultiListener {
6062
/** The key listener. */
6163
final Consumer<Character> keyListener;
6264

65+
/** The pressed characters. */
66+
final Set<Character> pressedCharacters = new HashSet<>();
67+
68+
/** The pressed special characters. */
69+
final Set<Integer> pressedSpecialCharacters = new HashSet<>();
70+
6371
/**
6472
* Instantiates a new layered display multi listener.
6573
*
@@ -93,11 +101,13 @@ public LayeredDisplayMultiListener(final IDisplaySurface surface, final LayeredD
93101
* @param isCommand
94102
*/
95103
public void keyPressed(final char e, final boolean isCommand) {
104+
DEBUG.OUT("Key pressed: " + e);
96105
if (isCommand) {
97106
keyListener.accept(e);
98107
return;
99108
}
100-
surface.dispatchKeyEvent(e);
109+
pressedCharacters.add(e);
110+
for (Character c : pressedCharacters) { surface.dispatchKeyEvent(c); }
101111
WorkbenchHelper.asyncRun(view.displayOverlay);
102112
}
103113

@@ -108,7 +118,8 @@ public void keyPressed(final char e, final boolean isCommand) {
108118
* the key code
109119
*/
110120
public void specialKeyPressed(final int keyCode) {
111-
surface.dispatchSpecialKeyEvent(keyCode);
121+
pressedSpecialCharacters.add(keyCode);
122+
for (Integer code : pressedSpecialCharacters) { surface.dispatchSpecialKeyEvent(code); }
112123
WorkbenchHelper.asyncRun(view.displayOverlay); // ??
113124
}
114125

@@ -120,7 +131,22 @@ public void specialKeyPressed(final int keyCode) {
120131
* @param command
121132
* the command
122133
*/
123-
public void keyReleased(final char e, final boolean command) {}
134+
public void keyReleased(final char e, final boolean command) {
135+
DEBUG.OUT("Key released: " + e);
136+
if (!command) { pressedCharacters.remove(e); }
137+
}
138+
139+
/**
140+
* Special key released.
141+
*
142+
* @param keyCode
143+
* the key code
144+
*/
145+
public void specialKeyReleased(final int keyCode) {
146+
pressedSpecialCharacters.remove(keyCode);
147+
// for (Integer code : pressedSpecialCharacters) { surface.dispatchSpecialKeyEvent(code); }
148+
// WorkbenchHelper.asyncRun(view.displayOverlay); // ??
149+
}
124150

125151
/**
126152
* Mouse enter.
@@ -170,6 +196,7 @@ public void mouseExit(final int x, final int y, final boolean modifier, final in
170196
setMousePosition(-1, -1);
171197
if (button > 0) return;
172198
// DEBUG.LOG("Mouse exiting " + e);
199+
173200
surface.dispatchMouseEvent(SWT.MouseExit, x, y);
174201
if (!view.isFullScreen() && WorkaroundForIssue1353.isInstalled()) {
175202
// suppressNextEnter = true;
@@ -204,13 +231,13 @@ public void mouseHover(final int x, final int y, final int button) {
204231
public void mouseMove(final int x, final int y, final boolean modifier) {
205232
WorkbenchHelper.asyncRun(view.displayOverlay);
206233
if (modifier) return;
207-
//DEBUG.LOG("Mouse moving on " + view.view.getPartName() + " at (" + x + "," + y + ")");
234+
// DEBUG.LOG("Mouse moving on " + view.view.getPartName() + " at (" + x + "," + y + ")");
208235
if (mouseIsDown) {
209236
// Depending on the plateform or display, this case is never called,
210237
// because DragDetect events are emitted instead. But on Windows
211238
// Java2D displays, it seems that MouseMove events are emitted even
212239
// when the mouse button is down.
213-
240+
214241
// Eventually moves the surface view
215242
surface.draggedTo(x, y);
216243
// Updates the mouse position. If the surface view is locked, the
@@ -241,7 +268,7 @@ public void mouseMove(final int x, final int y, final boolean modifier) {
241268
* whetehr ALT, CTRL, CMD, META or other modifiers are used
242269
*/
243270
public void mouseDown(final int x, final int y, final int button, final boolean modifier) {
244-
//DEBUG.LOG("Mouse down at " + x + ", " + y);
271+
// DEBUG.LOG("Mouse down at " + x + ", " + y);
245272
setMousePosition(x, y);
246273
if (inMenu) {
247274
inMenu = false;
@@ -269,7 +296,7 @@ public void mouseDown(final int x, final int y, final int button, final boolean
269296
* whetehr ALT, CTRL, CMD, META or other modifiers are used
270297
*/
271298
public void mouseUp(final int x, final int y, final int button, final boolean modifier) {
272-
//DEBUG.LOG("Mouse up at " + x + ", " + y + " on " + view.getPartName());
299+
// DEBUG.LOG("Mouse up at " + x + ", " + y + " on " + view.getPartName());
273300
// In case the mouse has moved (for example on a menu)
274301
if (!mouseIsDown) return;
275302
setMousePosition(x, y);
@@ -300,7 +327,7 @@ public void menuDetected(final int x, final int y) {
300327
* Drag detected.
301328
*/
302329
public void dragDetected(final int x, final int y) {
303-
//DEBUG.LOG("Mouse drag detected on " + view.view.getPartName() + " at (" + x + "," + y + ")");
330+
// DEBUG.LOG("Mouse drag detected on " + view.view.getPartName() + " at (" + x + "," + y + ")");
304331
surface.draggedTo(x, y);
305332
setMousePosition(x, y);
306333
surface.dispatchMouseEvent(SWT.DragDetect, x, y);
@@ -314,7 +341,10 @@ public void focusGained() {}
314341
/**
315342
* Focus lost.
316343
*/
317-
public void focusLost() {}
344+
public void focusLost() {
345+
pressedCharacters.clear();
346+
pressedSpecialCharacters.clear();
347+
}
318348

319349
/**
320350
* Sets the mouse position.

ummisco.gama.ui.experiment/src/ummisco/gama/ui/views/displays/SWTLayeredDisplayMultiListener.java

+56-6
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
/*******************************************************************************************************
22
*
33
* SWTLayeredDisplayMultiListener.java, in ummisco.gama.ui.experiment, is part of the source code of the GAMA modeling
4-
* and simulation platform (v.1.9.2).
4+
* and simulation platform (v.2.0.0).
55
*
66
* (c) 2007-2023 UMI 209 UMMISCO IRD/SU & Partners (IRIT, MIAT, TLU, CTU)
77
*
8-
* Visit https://github.com/gama-platform/gama for license information and contacts.
8+
* Visit https://github.com/gama-platform/gama2 for license information and contacts.
99
*
1010
********************************************************************************************************/
1111
package ummisco.gama.ui.views.displays;
@@ -160,6 +160,36 @@ public void keyPressed(final KeyEvent e) {
160160
@Override
161161
public void keyReleased(final KeyEvent e) {
162162
if (!ok.get()) return;
163+
switch (e.keyCode) {
164+
case SWT.ARROW_DOWN:
165+
delegate.specialKeyReleased(IEventLayerListener.ARROW_DOWN);
166+
return;
167+
case SWT.ARROW_UP:
168+
delegate.specialKeyReleased(IEventLayerListener.ARROW_UP);
169+
return;
170+
case SWT.ARROW_LEFT:
171+
delegate.specialKeyReleased(IEventLayerListener.ARROW_LEFT);
172+
return;
173+
case SWT.ARROW_RIGHT:
174+
delegate.specialKeyReleased(IEventLayerListener.ARROW_RIGHT);
175+
return;
176+
case SWT.ESC:
177+
delegate.specialKeyReleased(IEventLayerListener.KEY_ESC);
178+
return;
179+
case SWT.TAB:
180+
delegate.specialKeyReleased(IEventLayerListener.KEY_TAB);
181+
return;
182+
case SWT.PAGE_DOWN:
183+
delegate.specialKeyReleased(IEventLayerListener.KEY_PAGE_DOWN);
184+
return;
185+
case SWT.PAGE_UP:
186+
delegate.specialKeyReleased(IEventLayerListener.KEY_PAGE_UP);
187+
return;
188+
case SWT.CR, SWT.KEYPAD_CR:
189+
delegate.specialKeyReleased(IEventLayerListener.KEY_RETURN);
190+
return;
191+
192+
}
163193
// DEBUG.OUT("Key released " + e);
164194
delegate.keyReleased(e.character, GamaKeyBindings.ctrl(e));
165195
}
@@ -272,6 +302,11 @@ public java.awt.event.KeyListener getKeyAdapterForAWT() {
272302

273303
@Override
274304
public void keyTyped(final java.awt.event.KeyEvent e) {
305+
306+
}
307+
308+
@Override
309+
public void keyPressed(final java.awt.event.KeyEvent e) {
275310
// Necessary to filter by the time to avoid repetitions
276311
if (e.getWhen() == previous) return;
277312
previous = e.getWhen();
@@ -295,10 +330,25 @@ public void keyTyped(final java.awt.event.KeyEvent e) {
295330
}
296331

297332
@Override
298-
public void keyPressed(final java.awt.event.KeyEvent e) {}
299-
300-
@Override
301-
public void keyReleased(final java.awt.event.KeyEvent e) {}
333+
public void keyReleased(final java.awt.event.KeyEvent e) {
334+
DEBUG.LOG("Key released by the AWT listener. Code " + e.getKeyCode() + " Action ? " + e.isActionKey());
335+
if (!e.isActionKey()) {
336+
delegate.keyReleased(e.getKeyChar(), e.isControlDown());
337+
} else if (e.getModifiersEx() == 0) {
338+
delegate.specialKeyReleased(switch (e.getKeyCode()) {
339+
case java.awt.event.KeyEvent.VK_UP, java.awt.event.KeyEvent.VK_KP_UP -> IEventLayerListener.ARROW_UP;
340+
case java.awt.event.KeyEvent.VK_DOWN, java.awt.event.KeyEvent.VK_KP_DOWN -> IEventLayerListener.ARROW_DOWN;
341+
case java.awt.event.KeyEvent.VK_LEFT, java.awt.event.KeyEvent.VK_KP_LEFT -> IEventLayerListener.ARROW_LEFT;
342+
case java.awt.event.KeyEvent.VK_RIGHT, java.awt.event.KeyEvent.VK_KP_RIGHT -> IEventLayerListener.ARROW_RIGHT;
343+
case java.awt.event.KeyEvent.VK_PAGE_UP -> IEventLayerListener.KEY_PAGE_UP;
344+
case java.awt.event.KeyEvent.VK_PAGE_DOWN -> IEventLayerListener.KEY_PAGE_DOWN;
345+
case java.awt.event.KeyEvent.VK_ESCAPE -> IEventLayerListener.KEY_ESC;
346+
case java.awt.event.KeyEvent.VK_ENTER -> IEventLayerListener.KEY_RETURN;
347+
case java.awt.event.KeyEvent.VK_TAB -> IEventLayerListener.KEY_TAB;
348+
default -> 0;
349+
});
350+
}
351+
}
302352
};
303353
}
304354

0 commit comments

Comments
 (0)