[MM-23708] Send HW key pressed event only when a HW keyboard is connected (#4100)

* Check if HW keyboard is connected

* Call super

* Fix double new line insert
This commit is contained in:
Miguel Alatzar 2020-03-31 17:43:18 -07:00 committed by GitHub
parent ece9b6f1d9
commit 1079498461
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 3 deletions

View file

@ -2,16 +2,31 @@ package com.mattermost.rnbeta;
import android.os.Bundle;
import androidx.annotation.Nullable;
import android.view.KeyEvent;
import android.content.res.Configuration;
import com.reactnativenavigation.NavigationActivity;
import android.view.KeyEvent;
import com.github.emilioicai.hwkeyboardevent.HWKeyboardEventModule;
public class MainActivity extends NavigationActivity {
private boolean HWKeyboardConnected = false;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.launch_screen);
setHWKeyboardConnected();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if (newConfig.hardKeyboardHidden == Configuration.HARDKEYBOARDHIDDEN_NO) {
HWKeyboardConnected = true;
} else if (newConfig.hardKeyboardHidden == Configuration.HARDKEYBOARDHIDDEN_YES) {
HWKeyboardConnected = false;
}
}
/*
@ -21,11 +36,15 @@ public class MainActivity extends NavigationActivity {
*/
@Override
public boolean dispatchKeyEvent(KeyEvent event) {
if (event.getKeyCode() == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_UP) {
if (HWKeyboardConnected && event.getKeyCode() == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_UP) {
String keyPressed = event.isShiftPressed() ? "shift-enter" : "enter";
HWKeyboardEventModule.getInstance().keyPressed(keyPressed);
return true;
}
return super.dispatchKeyEvent(event);
};
private void setHWKeyboardConnected() {
HWKeyboardConnected = getResources().getConfiguration().keyboard == Configuration.KEYBOARD_QWERTY;
}
}

View file

@ -53,6 +53,7 @@ import {
const {RNTextInputReset} = NativeModules;
const INPUT_LINE_HEIGHT = 20;
const EXTRA_INPUT_PADDING = 3;
const HW_SHIFT_ENTER_TEXT = Platform.OS === 'ios' ? '\n' : '';
export default class PostTextBoxBase extends PureComponent {
static propTypes = {
@ -387,7 +388,7 @@ export default class PostTextBoxBase extends PureComponent {
switch (keyEvent.pressedKey) {
case 'enter': this.handleSendMessage();
break;
case 'shift-enter': this.handleInsertTextToDraft('\n');
case 'shift-enter': this.handleInsertTextToDraft(HW_SHIFT_ENTER_TEXT);
}
}