Skip to content

Commit

Permalink
Fixes for on-screen keys for regressions introduced in hardware keybo…
Browse files Browse the repository at this point in the history
…ard implementation.

Additional logging for keyboard input implemented in RfbProto.

Long-standing issue with on-screen SHIFT fixed for bVNC.
  • Loading branch information
iiordanov committed Mar 16, 2022
1 parent 1c04af8 commit 52acce1
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 18 deletions.
25 changes: 15 additions & 10 deletions bVNC/src/main/java/com/iiordanov/bVNC/RfbProto.java
Original file line number Diff line number Diff line change
Expand Up @@ -1499,7 +1499,7 @@ public synchronized void writePointerEvent(int x, int y, int modifiers, int poin
return;

eventBufLen = 0;
writeModifierKeyEvents(modifiers);
writeModifierKeyEvents(modifiers, true);

eventBuf[eventBufLen++] = (byte) PointerEvent;
eventBuf[eventBufLen++] = (byte) pointerMask;
Expand All @@ -1513,7 +1513,7 @@ public synchronized void writePointerEvent(int x, int y, int modifiers, int poin
//

if (pointerMask == 0) {
writeModifierKeyEvents(0);
writeModifierKeyEvents(modifiers, false);
}

try {
Expand All @@ -1530,17 +1530,17 @@ void writeCtrlAltDel() throws IOException {
try {
// Press
eventBufLen = 0;
writeModifierKeyEvents(CTRLALT);
writeModifierKeyEvents(CTRLALT, true);
writeKeyEvent(DELETE, true);
os.write(eventBuf, 0, eventBufLen);

// Release
eventBufLen = 0;
writeModifierKeyEvents(CTRLALT);
writeKeyEvent(DELETE, false);
writeModifierKeyEvents(CTRLALT, false);

// Reset VNC server modifiers state
writeModifierKeyEvents(0);
//writeModifierKeyEvents(0, false);
os.write(eventBuf, 0, eventBufLen);
} catch (IOException e) {
e.printStackTrace();
Expand All @@ -1557,14 +1557,16 @@ public synchronized void writeKeyEvent(int keySym, int metaState, boolean down)
return;

eventBufLen = 0;
if (down)
writeModifierKeyEvents(metaState);
if (down) {
writeModifierKeyEvents(metaState, down);
}

if (keySym > 0)
writeKeyEvent(keySym, down);

// Always release all modifiers after an "up" event
if (!down) {
writeModifierKeyEvents(0);
writeModifierKeyEvents(metaState, down);
}

try {
Expand All @@ -1581,9 +1583,13 @@ public synchronized void writeKeyEvent(int keySym, int metaState, boolean down)
//

private void writeKeyEvent(int keysym, boolean down) {

if (viewOnly)
return;

GeneralUtils.debugLog(this.debugLogging, TAG, "writeKeyEvent, sending keysym:" +
keysym + ", down: " + down);

eventBuf[eventBufLen++] = (byte) KeyboardEvent;
eventBuf[eventBufLen++] = (byte) (down ? 1 : 0);
eventBuf[eventBufLen++] = (byte) 0;
Expand Down Expand Up @@ -1626,9 +1632,8 @@ public void clientRedirect(int port, String host, String x509subject) {
// Write key events to set the correct modifier state.
//

void writeModifierKeyEvents(int newModifiers) {
void writeModifierKeyEvents(int metaState, boolean down) {
for (int modifierMask: modifierMap.keySet()) {
boolean down = (newModifiers & modifierMask) != 0;
if (remoteKeyboardState.shouldSendModifier(metaState, modifierMask, down)) {
int modifier = modifierMap.get(modifierMask);
GeneralUtils.debugLog(this.debugLogging, TAG, "sendModifierKeys, modifierMask:" +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ private void writeKeyEvent(boolean isUnicode, int code, int metaState, boolean d
if (down) {
lastDownMetaState = metaState;
} else {
metaState = lastDownMetaState;
lastDownMetaState = 0;
}

Expand Down Expand Up @@ -208,8 +209,6 @@ private void writeKeyEvent(boolean isUnicode, int code, int metaState, boolean d
rfb.writeKeyEvent(scode, meta, down);
if (sendUpEvents) {
rfb.writeKeyEvent(scode, meta, false);
GeneralUtils.debugLog(debugLogging, TAG, "Unsetting lastDownMetaState");
lastDownMetaState = 0;
}
}
} catch (NullPointerException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,13 @@ public boolean processLocalKeyEvent(int keyCode, KeyEvent evt, int additionalMet
if ((metaState & ALT_MASK) != 0 || (metaState & RALT_MASK) != 0) {
metaMask |= KeyEvent.META_ALT_MASK;
}
// Add SHIFT to the metastate to get upper-case characters if onscreen shift is pressed
int addShift = 0;
if ((onScreenMetaState & SHIFT_MASK) != 0) {
addShift |= KeyEvent.META_SHIFT_ON|KeyEvent.META_SHIFT_LEFT_ON;
}
KeyEvent copy = new KeyEvent(evt.getDownTime(), evt.getEventTime(), evt.getAction(),
evt.getKeyCode(), evt.getRepeatCount(), evt.getMetaState() & ~metaMask,
evt.getKeyCode(), evt.getRepeatCount(), evt.getMetaState() & ~metaMask|addShift,
evt.getDeviceId(), evt.getScanCode());
key = copy.getUnicodeChar();
keysym = UnicodeToKeysym.translate(key);
Expand Down Expand Up @@ -204,17 +209,18 @@ public boolean processLocalKeyEvent(int keyCode, KeyEvent evt, int additionalMet
if (down) {
lastDownMetaState = metaState;
} else {
metaState = lastDownMetaState;
lastDownMetaState = 0;
}

if (numchars == 1) {
GeneralUtils.debugLog(App.debugLog, TAG, "processLocalKeyEvent: Sending key. Down: " + down +
", key: " + key + ". keysym:" + keysym + ", metaState: " + metaState);
", key: " + key + ", keysym:" + keysym + ", metaState: " + metaState);
rfb.writeKeyEvent(keysym, metaState, down);
// If this is a unicode key, the up event will never come, so we artificially insert it.
if (unicode) {
GeneralUtils.debugLog(App.debugLog, TAG, "processLocalKeyEvent: Unicode key. Down: false" +
", key: " + key + ". keysym:" + keysym + ", metaState: " + metaState);
", key: " + key + ", keysym:" + keysym + ", metaState: " + metaState);
rfb.writeKeyEvent(keysym, metaState, false);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,10 @@ public RdpCommunicator(Context context, Handler handler, Viewable viewable, Stri
this.username = username;
this.domain = domain;
this.password = password;
modifierMap.put(RemoteKeyboard.CTRL_MASK, VK_CONTROL);
modifierMap.put(RemoteKeyboard.RCTRL_MASK, VK_RCONTROL);
modifierMap.put(RemoteKeyboard.CTRL_MASK, VK_LCONTROL);
modifierMap.put(RemoteKeyboard.RCTRL_MASK, (VK_RCONTROL | VK_EXT_KEY));
modifierMap.put(RemoteKeyboard.ALT_MASK, VK_LMENU);
modifierMap.put(RemoteKeyboard.RALT_MASK, VK_RMENU);
modifierMap.put(RemoteKeyboard.RALT_MASK, (VK_RMENU | VK_EXT_KEY));
modifierMap.put(RemoteKeyboard.SUPER_MASK, (VK_LWIN | VK_EXT_KEY));
modifierMap.put(RemoteKeyboard.RSUPER_MASK, (VK_RWIN | VK_EXT_KEY));
modifierMap.put(RemoteKeyboard.SHIFT_MASK, VK_LSHIFT);
Expand Down

0 comments on commit 52acce1

Please sign in to comment.