Skip to content

Commit

Permalink
Add support for keyboard down and up events
Browse files Browse the repository at this point in the history
Closes #150.
  • Loading branch information
fniephaus committed Oct 28, 2021
1 parent 829e68d commit ba75056
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 29 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
* Copyright (c) 2017-2021 Software Architecture Group, Hasso Plattner Institute
* Copyright (c) 2021 Oracle and/or its affiliates
*
* Licensed under the MIT License.
*/
Expand Down Expand Up @@ -112,7 +113,6 @@ public static final class KEYBOARD {
public static final int ALT = 32;
public static final int CMD = 64;
public static final int ALL = SHIFT + CTRL + ALT + CMD;
public static final long INTERRUPT_KEYCODE = ((long) CMD >> 3 << 8) + '.'; // "CMD + ."
}

public static final class KEY {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
* Copyright (c) 2017-2021 Software Architecture Group, Hasso Plattner Institute
* Copyright (c) 2021 Oracle and/or its affiliates
*
* Licensed under the MIT License.
*/
Expand All @@ -9,7 +10,6 @@
import java.awt.event.KeyListener;

import de.hpi.swa.trufflesqueak.io.SqueakIOConstants.EVENT_TYPE;
import de.hpi.swa.trufflesqueak.io.SqueakIOConstants.KEYBOARD;
import de.hpi.swa.trufflesqueak.io.SqueakIOConstants.KEYBOARD_EVENT;

public final class SqueakKeyboard implements KeyListener {
Expand All @@ -19,46 +19,38 @@ public SqueakKeyboard(final SqueakDisplay display) {
this.display = display;
}

@Override
public void keyTyped(final KeyEvent e) {
/* Ignored. Called just after the user types a Unicode character. */
}

@Override
public void keyPressed(final KeyEvent e) {
display.recordModifiers(e);
final int keyCode = mapSpecialKeyCode(e);
final char keyChar = e.getKeyChar();
if (keyCode >= 0) {
// Special key pressed, record mapped code
recordKeyboardEvent(keyCode);
} else if (keyChar != KeyEvent.CHAR_UNDEFINED) {
// Regular key pressed, record char value
recordKeyboardEvent(keyChar);
final int keyChar = toKeyChar(e);
addKeyboardEvent(KEYBOARD_EVENT.DOWN, keyChar != KeyEvent.CHAR_UNDEFINED ? keyChar : e.getKeyCode());
if (keyChar != KeyEvent.CHAR_UNDEFINED) {
addKeyboardEvent(KEYBOARD_EVENT.CHAR, keyChar);
}
if (e.isMetaDown() && keyChar == '.') {
display.image.interrupt.setInterruptPending();
}
}

@Override
public void keyTyped(final KeyEvent e) {
/** Keyboard char events handled in keyPressed(KeyEvent). */
}

@Override
public void keyReleased(final KeyEvent e) {
display.recordModifiers(e);
final int keyChar = toKeyChar(e);
addKeyboardEvent(KEYBOARD_EVENT.UP, keyChar != KeyEvent.CHAR_UNDEFINED ? keyChar : e.getKeyCode());
}

private void recordKeyboardEvent(final int key) {
final int buttonsShifted = display.buttons >> 3;
if ((buttonsShifted << 8 | key) == KEYBOARD.INTERRUPT_KEYCODE) {
display.image.interrupt.setInterruptPending();
} else {
display.addEvent(EVENT_TYPE.KEYBOARD,
key /* MacRoman */,
KEYBOARD_EVENT.CHAR,
buttonsShifted,
key /* Unicode */);
}
private void addKeyboardEvent(final long eventType, final int keyCharOrCode) {
display.addEvent(EVENT_TYPE.KEYBOARD, keyCharOrCode, eventType, display.buttons >> 3, keyCharOrCode);
}

private static int mapSpecialKeyCode(final KeyEvent e) {
private static int toKeyChar(final KeyEvent e) {
//@formatter:off
switch(e.getExtendedKeyCode()) {
switch(e.getKeyCode()) { // Handle special keys.
case KeyEvent.VK_BACK_SPACE: return 8;
case KeyEvent.VK_TAB: return 9;
case KeyEvent.VK_ENTER: return 13;
Expand All @@ -74,7 +66,7 @@ private static int mapSpecialKeyCode(final KeyEvent e) {
case KeyEvent.VK_DOWN: return 31;
case KeyEvent.VK_INSERT: return 5;
case KeyEvent.VK_DELETE: return 127;
default: return -1; // Not a special key.
default: return e.getKeyChar();
}
//@formatter:on
}
Expand Down

0 comments on commit ba75056

Please sign in to comment.