Skip to content

Commit

Permalink
Merge pull request #72 from peterekepeter/fix-repeating-key
Browse files Browse the repository at this point in the history
Bugfix repeating keys stuck on repeat forever
  • Loading branch information
gazlaws-dev authored May 17, 2020
2 parents 4aab2de ef7101b commit 4673697
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 16 deletions.
36 changes: 25 additions & 11 deletions app/src/main/java/com/gazlaws/codeboard/CodeBoardIME.java
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 215,6 @@ public void onKey(int primaryCode, int[] KeyCodes) {
}
}

@Override
public void onPress(final int primaryCode) {

if (soundOn) {
Expand All @@ -234,8 233,7 @@ public void onCompletion(MediaPlayer mp) {
if (vibrator != null)
vibrator.vibrate(20);
}
if (timerLongPress != null)
timerLongPress.cancel();
clearLongPressTimer();

timerLongPress = new Timer();

Expand Down Expand Up @@ -276,9 274,19 @@ public void run() {
}

@Override
public void onWindowHidden() {
super.onWindowHidden();
clearLongPressTimer();
}

@Override
public void onViewClicked(boolean focusChanged) {
super.onViewClicked(focusChanged);
clearLongPressTimer();
}

public void onRelease(int primaryCode) {
if (timerLongPress != null)
timerLongPress.cancel();
clearLongPressTimer();
}

public void onKeyLongPress(int keyCode) {
Expand Down Expand Up @@ -310,23 318,24 @@ public void onKeyLongPress(int keyCode) {
vibrator.vibrate(50);
}

@Override
public void onText(CharSequence text) {
InputConnection ic = getCurrentInputConnection();
ic.commitText(text, 1);
}

@Override
public void swipeDown() {
public void swipeLeft() {

}

@Override
public void swipeLeft() {
public void swipeRight() {

}

@Override
public void swipeRight() {
public void swipeDown() {

}

@Override
Expand Down Expand Up @@ -419,10 428,8 @@ public void onStartInputView(EditorInfo attribute, boolean restarting) {
super.onStartInputView(attribute, restarting);
setInputView(onCreateInputView());
sEditorInfo = attribute;

}


public void controlKeyUpdateView() {
mCurrentKeyboardLayoutView.applyCtrlModifier(ctrl);
}
Expand All @@ -431,6 438,13 @@ public void shiftKeyUpdateView() {
mCurrentKeyboardLayoutView.applyShiftModifier(shift);
}

private void clearLongPressTimer(){
if (timerLongPress != null){
timerLongPress.cancel();
}
timerLongPress = null;
}

private ThemeInfo getThemeByIndex(int index){
switch (index) {
case 0:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 21,10 @@ public static void addArrowsRow(KeyboardLayoutBuilder keyboard)
keyboard.newRow()
.addKey("Esc", CODE_ESCAPE)
.addTabKey()
.addKey("◀", CODE_ARROW_LEFT)
.addKey("▼", CODE_ARROW_DOWN)
.addKey("▲", CODE_ARROW_UP)
.addKey("▶", CODE_ARROW_RIGHT)
.addKey("◀", CODE_ARROW_LEFT).asRepeatable()
.addKey("▼", CODE_ARROW_DOWN).asRepeatable()
.addKey("▲", CODE_ARROW_UP).asRepeatable()
.addKey("▶", CODE_ARROW_RIGHT).asRepeatable()
.addKey("SYM", CODE_SYMBOLS)
;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 3,7 @@
import android.content.Context;
import android.graphics.Canvas;
import android.inputmethodservice.KeyboardView;
import android.util.Log;
import android.support.annotation.NonNull;
import android.view.MotionEvent;
import android.view.View;

Expand All @@ -23,6 23,7 @@ public class KeyboardButtonView extends View {
private final UiTheme uiTheme;
private Timer timer;
private String currentLabel = null;
private boolean isPressed = false;

public KeyboardButtonView(Context context, Key key, KeyboardView.OnKeyboardActionListener inputService, UiTheme uiTheme) {
super(context);
Expand Down Expand Up @@ -68,6 69,18 @@ public void draw(Canvas canvas){
super.draw(canvas);
}

@Override
protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
autoReleaseIfPressed();
}

@Override
protected void onVisibilityChanged(@NonNull View changedView, int visibility) {
super.onVisibilityChanged(changedView, visibility);
autoReleaseIfPressed();
}

private void drawButtonContent(Canvas canvas) {
float x = this.getWidth()/2;
float y = this.getHeight()/2 uiTheme.fontHeight/3;
Expand All @@ -85,6 98,7 @@ private void drawButtonBody(Canvas canvas) {
}

private void onPress() {
isPressed = true;
if (key.info.code != 0){
inputService.onPress(key.info.code);
}
Expand All @@ -96,6 110,7 @@ private void onPress() {
}

private void onRelease() {
isPressed = false;
// NOTE: If the arrow keys move out of the input view, the onRelease is never called
if (key.info.code != 0){
inputService.onRelease(key.info.code);
Expand All @@ -115,6 130,12 @@ private void submitKeyEvent(){
}
}

private void autoReleaseIfPressed(){
if (isPressed){
onRelease();
}
}

private void stopRepeating() {
if (timer == null){
return;
Expand Down

0 comments on commit 4673697

Please sign in to comment.