Skip to content

Commit

Permalink
Forcing software keyboard
Browse files Browse the repository at this point in the history
  • Loading branch information
hajdam committed Sep 1, 2024
1 parent fb44a1e commit 22aa2aa
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ protected void onSizeChanged(int width, int height, int oldWidth, int oldHeight)

@Override
public boolean onKeyDown(int keyCode, KeyEvent keyEvent) {
commandHandler.keyTyped(keyCode, keyEvent);
commandHandler.keyTyped(keyEvent.getUnicodeChar(), keyEvent);
return super.onKeyDown(keyCode, keyEvent);
}

Expand Down
2 changes: 2 additions & 0 deletions app/src/main/java/org/exbin/bined/android/basic/CodeArea.java
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,8 @@ private void init() {
// TODO: Use swing color instead
//setBackground(Color.WHITE);
setLongClickable(true);
setFocusable(true);
setFocusableInTouchMode(true);
codeAreaCaret.setSection(BasicCodeAreaSection.CODE_MATRIX);
}

Expand Down
50 changes: 50 additions & 0 deletions app/src/main/java/org/exbin/bined/editor/android/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,20 @@

import android.Manifest;
import androidx.fragment.app.DialogFragment;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.text.Editable;
import android.text.method.KeyListener;
import android.text.method.TextKeyListener;
import android.view.ContextMenu;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
Expand All @@ -27,6 +33,7 @@
import org.exbin.bined.EditOperation;
import org.exbin.bined.SelectionRange;
import org.exbin.bined.android.basic.CodeArea;
import org.exbin.bined.basic.BasicCodeAreaSection;
import org.exbin.bined.capability.EditModeCapable;
import org.exbin.bined.highlight.android.HighlightNonAsciiCodeAreaPainter;
import org.exbin.bined.operation.android.CodeAreaOperationCommandHandler;
Expand Down Expand Up @@ -62,6 +69,7 @@ public class MainActivity extends AppCompatActivity implements FileDialog.OnFile

private boolean storageReadPermissionGranted;
private boolean storageWritePermissionGranted;
private boolean keyboardShown = false;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
Expand Down Expand Up @@ -135,6 +143,48 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {

codeArea.addCaretMovedListener((CodeAreaCaretPosition caretPosition) -> {
binaryStatus.setCursorPosition(caretPosition);

boolean showKeyboard = codeArea.getActiveSection() == BasicCodeAreaSection.TEXT_PREVIEW;
if (showKeyboard != keyboardShown) {

keyboardShown = showKeyboard;
InputMethodManager im = (InputMethodManager)codeArea.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
if (showKeyboard) {
im.showSoftInput(codeArea, InputMethodManager.SHOW_IMPLICIT);
} else {
im.hideSoftInputFromWindow(codeArea.getWindowToken(), 0);
}
}
});

codeArea.setOnKeyListener(new View.OnKeyListener() {

private KeyListener keyListener = new TextKeyListener(TextKeyListener.Capitalize.NONE, false);
private Editable editable = Editable.Factory.getInstance().newEditable("");

@Override
public boolean onKey(View view, int keyCode, KeyEvent keyEvent) {
if (keyEvent.getAction() == KeyEvent.ACTION_DOWN) {
keyListener.onKeyDown(view, editable, keyCode, keyEvent);
processKeys(keyEvent);
} else if (keyEvent.getAction() == KeyEvent.ACTION_UP) {
commandHandler.keyPressed(keyEvent);
} else {
keyListener.onKeyOther(view, editable, keyEvent);
processKeys(keyEvent);
}
return true;
}

private void processKeys(KeyEvent keyEvent) {
int outputCharsLength = editable.length();
if (outputCharsLength > 0) {
for (int i = 0; i < outputCharsLength; i++) {
commandHandler.keyTyped(editable.charAt(i), keyEvent);
}
editable.clear();
}
}
});

codeArea.addEditModeChangedListener((EditMode mode, EditOperation operation) -> {
Expand Down
12 changes: 12 additions & 0 deletions app/src/main/res/menu/menu_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,31 @@
<menu>
<item
android:id="@+id/view_mode"
android:enabled="false"
android:title="@string/view_mode" />
<item
android:id="@+id/code_type"
android:enabled="false"
android:title="@string/code_type" />
<item
android:id="@+id/hex_chars_case"
android:enabled="false"
android:title="@string/hex_characters_case" />
<item
android:id="@+id/non_printing_characters"
android:enabled="false"
android:title="@string/non_printing_characters" />
</menu>
</item>
<item
android:id="@+id/edit_menu"
android:title="@string/edit_menu">
<menu>
<item
android:id="@+id/go_to_position"
android:enabled="false"
android:title="@string/go_to_position" />

</menu>
</item>
<item
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@
<string name="view_mode">View Mode</string>
<string name="code_type">Code Type</string>
<string name="hex_characters_case">Hex Characters Case</string>
<string name="non_printing_characters">Non-printing Characters</string>
<string name="go_to_position">Go To Position</string>
<string name="edit_menu">Edit</string>
<string name="action_search">Search</string>
<string name="action_undo">Undo</string>
Expand Down

0 comments on commit 22aa2aa

Please sign in to comment.