Skip to content
This repository has been archived by the owner on Jul 22, 2024. It is now read-only.

Commit

Permalink
Add support for Bluetooth keyboards. Fixes #775
Browse files Browse the repository at this point in the history
  • Loading branch information
bluemarvin committed Sep 24, 2019
1 parent 7432674 commit 7e0b06a
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 4 deletions.
37 changes: 35 additions & 2 deletions app/src/common/shared/org/mozilla/vrbrowser/VRBrowserActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,12 @@
import android.os.Process;
import android.util.Log;
import android.util.Pair;
import android.view.KeyCharacterMap;
import android.view.KeyEvent;
import android.view.Surface;
import android.view.View;
import android.view.ViewTreeObserver;
import android.view.inputmethod.InputConnection;
import android.widget.FrameLayout;

import androidx.annotation.Keep;
Expand Down Expand Up @@ -608,12 +610,40 @@ public void onBackPressed() {

@Override
public boolean dispatchKeyEvent(KeyEvent event) {
final int keyCode = event.getKeyCode();
final InputConnection connection = mKeyboard.getInputConnection();
if (connection != null) {
if (mKeyboard.isAttachToWindowWidget()) {
connection.sendKeyEvent(event);
mKeyboard.hide(UIWidget.KEEP_WIDGET);
return true;
}
// Android Components do not support InputConnection.sendKeyEvent()
if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode > 0)) {
switch (keyCode) {
case KeyEvent.KEYCODE_DEL:
mKeyboard.handleBackspace(event.isLongPress());
return true;
case KeyEvent.KEYCODE_ENTER:
case KeyEvent.KEYCODE_NUMPAD_ENTER:
mKeyboard.handleDone();
return true;
default:
break;
}
if (event.getUnicodeChar() != 0) {
KeyCharacterMap map = event.getKeyCharacterMap();
String value = String.valueOf((char) map.get(keyCode, event.getMetaState()));
connection.commitText(value, 1);
return true;
}
}
}
if (DeviceType.isOculusBuild()) {
int action = event.getAction();
if (action != KeyEvent.ACTION_DOWN) {
return super.dispatchKeyEvent(event);
}
int keyCode = event.getKeyCode();
boolean result;
switch (keyCode) {
case KeyEvent.KEYCODE_VOLUME_UP:
Expand All @@ -622,14 +652,17 @@ public boolean dispatchKeyEvent(KeyEvent event) {
case KeyEvent.KEYCODE_VOLUME_DOWN:
result = callOnAudioManager((AudioManager aManager) -> aManager.adjustStreamVolume(AudioManager.STREAM_MUSIC, AudioManager.ADJUST_LOWER, AudioManager.FLAG_SHOW_UI));
break;
case KeyEvent.KEYCODE_VOLUME_MUTE:
result = callOnAudioManager((AudioManager aManager) -> aManager.adjustStreamVolume(AudioManager.STREAM_MUSIC, AudioManager.ADJUST_MUTE, AudioManager.FLAG_SHOW_UI));
break;
default:
return super.dispatchKeyEvent(event);
}
return result || super.dispatchKeyEvent(event);

} else if (DeviceType.isGoogleVR()) {
boolean result;
switch( event.getKeyCode() ) {
switch (keyCode) {
case KeyEvent.KEYCODE_VOLUME_UP:
case KeyEvent.KEYCODE_VOLUME_DOWN:
result = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,14 @@ private void resetKeyboardLayout() {
updateCandidates();
}

public InputConnection getInputConnection() {
return mInputConnection;
}

public Boolean isAttachToWindowWidget() {
return mFocusedView instanceof WindowWidget;
}

public void updateFocusedView(View aFocusedView) {
if (mFocusedView != null && mFocusedView instanceof TextView) {
((TextView)mFocusedView).removeTextChangedListener(this);
Expand Down Expand Up @@ -602,7 +610,7 @@ private void handleShift(boolean isShifted) {
mKeyboardView.setShifted(shifted || mIsCapsLock);
}

private void handleBackspace(final boolean isLongPress) {
public void handleBackspace(final boolean isLongPress) {
final InputConnection connection = mInputConnection;
if (mComposingText.length() > 0) {
CharSequence selectedText = mInputConnection.getSelectedText(0);
Expand Down Expand Up @@ -703,7 +711,7 @@ private void handleSpace() {
}
}

private void handleDone() {
public void handleDone() {
if (mComposingDisplayText.length() > 0) {
// Finish current composing
mComposingText = "";
Expand Down

0 comments on commit 7e0b06a

Please sign in to comment.