mirror of
https://github.com/fankes/termux-app.git
synced 2025-09-05 02:05:25 +08:00
add some functional keys
This commit is contained in:
@@ -1,10 +1,17 @@
|
|||||||
package com.termux.app;
|
package com.termux.app;
|
||||||
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
|
import android.graphics.Color;
|
||||||
import android.util.AttributeSet;
|
import android.util.AttributeSet;
|
||||||
|
|
||||||
|
import java.util.concurrent.Executors;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
import java.util.concurrent.ScheduledExecutorService;
|
||||||
|
|
||||||
import android.view.Gravity;
|
import android.view.Gravity;
|
||||||
import android.view.HapticFeedbackConstants;
|
import android.view.HapticFeedbackConstants;
|
||||||
import android.view.KeyEvent;
|
import android.view.KeyEvent;
|
||||||
|
import android.view.MotionEvent;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
import android.widget.Button;
|
import android.widget.Button;
|
||||||
import android.widget.GridLayout;
|
import android.widget.GridLayout;
|
||||||
@@ -38,30 +45,39 @@ public final class ExtraKeysView extends GridLayout {
|
|||||||
case "TAB":
|
case "TAB":
|
||||||
keyCode = KeyEvent.KEYCODE_TAB;
|
keyCode = KeyEvent.KEYCODE_TAB;
|
||||||
break;
|
break;
|
||||||
case "▲":
|
case "HOME":
|
||||||
|
keyCode = KeyEvent.KEYCODE_MOVE_HOME;
|
||||||
|
break;
|
||||||
|
case "END":
|
||||||
|
keyCode = KeyEvent.KEYCODE_MOVE_END;
|
||||||
|
break;
|
||||||
|
case "PGUP":
|
||||||
|
keyCode = KeyEvent.KEYCODE_PAGE_UP;
|
||||||
|
break;
|
||||||
|
case "PGDN":
|
||||||
|
keyCode = KeyEvent.KEYCODE_PAGE_DOWN;
|
||||||
|
break;
|
||||||
|
case "↑":
|
||||||
keyCode = KeyEvent.KEYCODE_DPAD_UP;
|
keyCode = KeyEvent.KEYCODE_DPAD_UP;
|
||||||
break;
|
break;
|
||||||
case "◀":
|
case "←":
|
||||||
keyCode = KeyEvent.KEYCODE_DPAD_LEFT;
|
keyCode = KeyEvent.KEYCODE_DPAD_LEFT;
|
||||||
break;
|
break;
|
||||||
case "▶":
|
case "→":
|
||||||
keyCode = KeyEvent.KEYCODE_DPAD_RIGHT;
|
keyCode = KeyEvent.KEYCODE_DPAD_RIGHT;
|
||||||
break;
|
break;
|
||||||
case "▼":
|
case "↓":
|
||||||
keyCode = KeyEvent.KEYCODE_DPAD_DOWN;
|
keyCode = KeyEvent.KEYCODE_DPAD_DOWN;
|
||||||
break;
|
break;
|
||||||
case "―":
|
|
||||||
chars = "-";
|
|
||||||
break;
|
|
||||||
default:
|
default:
|
||||||
chars = keyName;
|
chars = keyName;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TerminalView terminalView = view.findViewById(R.id.terminal_view);
|
||||||
if (keyCode > 0) {
|
if (keyCode > 0) {
|
||||||
view.dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, keyCode));
|
terminalView.onKeyDown(keyCode, new KeyEvent(KeyEvent.ACTION_UP, keyCode));
|
||||||
view.dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, keyCode));
|
// view.dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, keyCode));
|
||||||
} else {
|
} else {
|
||||||
TerminalView terminalView = view.findViewById(R.id.terminal_view);
|
|
||||||
TerminalSession session = terminalView.getCurrentSession();
|
TerminalSession session = terminalView.getCurrentSession();
|
||||||
if (session != null) session.write(chars);
|
if (session != null) session.write(chars);
|
||||||
}
|
}
|
||||||
@@ -70,6 +86,8 @@ public final class ExtraKeysView extends GridLayout {
|
|||||||
private ToggleButton controlButton;
|
private ToggleButton controlButton;
|
||||||
private ToggleButton altButton;
|
private ToggleButton altButton;
|
||||||
private ToggleButton fnButton;
|
private ToggleButton fnButton;
|
||||||
|
private ScheduledExecutorService scheduledExecutor;
|
||||||
|
private int longPressCount;
|
||||||
|
|
||||||
public boolean readControlButton() {
|
public boolean readControlButton() {
|
||||||
if (controlButton.isPressed()) return true;
|
if (controlButton.isPressed()) return true;
|
||||||
@@ -106,19 +124,19 @@ public final class ExtraKeysView extends GridLayout {
|
|||||||
removeAllViews();
|
removeAllViews();
|
||||||
|
|
||||||
String[][] buttons = {
|
String[][] buttons = {
|
||||||
{"ESC", "CTRL", "ALT", "TAB", "―", "/", "|"}
|
{"ESC", "/", "-", "HOME", "↑", "END", "PGUP"},
|
||||||
|
{"TAB", "CTRL", "ALT", "←", "↓", "→", "PGDN"}
|
||||||
};
|
};
|
||||||
|
|
||||||
final int rows = buttons.length;
|
final int rows = buttons.length;
|
||||||
final int cols = buttons[0].length;
|
final int[] cols = {buttons[0].length, buttons[1].length};
|
||||||
|
|
||||||
setRowCount(rows);
|
setRowCount(rows);
|
||||||
setColumnCount(cols);
|
setColumnCount(cols[0]);
|
||||||
|
|
||||||
for (int row = 0; row < rows; row++) {
|
for (int row = 0; row < rows; row++) {
|
||||||
for (int col = 0; col < cols; col++) {
|
for (int col = 0; col < cols[row]; col++) {
|
||||||
final String buttonText = buttons[row][col];
|
final String buttonText = buttons[row][col];
|
||||||
|
|
||||||
Button button;
|
Button button;
|
||||||
switch (buttonText) {
|
switch (buttonText) {
|
||||||
case "CTRL":
|
case "CTRL":
|
||||||
@@ -140,6 +158,7 @@ public final class ExtraKeysView extends GridLayout {
|
|||||||
|
|
||||||
button.setText(buttonText);
|
button.setText(buttonText);
|
||||||
button.setTextColor(TEXT_COLOR);
|
button.setTextColor(TEXT_COLOR);
|
||||||
|
button.setPadding(0, 0, 0, 0);
|
||||||
|
|
||||||
final Button finalButton = button;
|
final Button finalButton = button;
|
||||||
button.setOnClickListener(new OnClickListener() {
|
button.setOnClickListener(new OnClickListener() {
|
||||||
@@ -162,12 +181,49 @@ public final class ExtraKeysView extends GridLayout {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
GridLayout.LayoutParams param = new GridLayout.LayoutParams();
|
button.setOnTouchListener(new OnTouchListener() {
|
||||||
param.height = param.width = 0;
|
@Override
|
||||||
param.rightMargin = param.topMargin = 0;
|
public boolean onTouch(View v, MotionEvent event) {
|
||||||
|
final View root = getRootView();
|
||||||
|
switch (event.getAction()) {
|
||||||
|
case MotionEvent.ACTION_DOWN:
|
||||||
|
longPressCount = 0;
|
||||||
|
v.setBackgroundColor(Color.GRAY);
|
||||||
|
if (!"CTRLALT".contains(buttonText)) {
|
||||||
|
scheduledExecutor = Executors.newSingleThreadScheduledExecutor();
|
||||||
|
scheduledExecutor.scheduleWithFixedDelay(new Runnable() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
longPressCount++;
|
||||||
|
sendKey(root, buttonText);
|
||||||
|
}
|
||||||
|
}, 400, 100, TimeUnit.MILLISECONDS);
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
case MotionEvent.ACTION_UP:
|
||||||
|
case MotionEvent.ACTION_CANCEL:
|
||||||
|
performClick();
|
||||||
|
v.setBackgroundColor(Color.BLACK);
|
||||||
|
if (scheduledExecutor != null) {
|
||||||
|
scheduledExecutor.shutdownNow();
|
||||||
|
scheduledExecutor = null;
|
||||||
|
}
|
||||||
|
if (longPressCount == 0) {
|
||||||
|
v.performClick();
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
default:
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
LayoutParams param = new GridLayout.LayoutParams();
|
||||||
|
param.width = param.height = 0;
|
||||||
|
param.setMargins(0, 0, 0, 0);
|
||||||
param.setGravity(Gravity.LEFT);
|
param.setGravity(Gravity.LEFT);
|
||||||
float weight = "▲▼◀▶".contains(buttonText) ? 0.7f : 1.f;
|
param.columnSpec = GridLayout.spec(col, GridLayout.FILL, 1.f);
|
||||||
param.columnSpec = GridLayout.spec(col, GridLayout.FILL, weight);
|
|
||||||
param.rowSpec = GridLayout.spec(row, GridLayout.FILL, 1.f);
|
param.rowSpec = GridLayout.spec(row, GridLayout.FILL, 1.f);
|
||||||
button.setLayoutParams(param);
|
button.setLayoutParams(param);
|
||||||
|
|
||||||
|
@@ -70,7 +70,7 @@
|
|||||||
android:id="@+id/viewpager"
|
android:id="@+id/viewpager"
|
||||||
android:visibility="gone"
|
android:visibility="gone"
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="40dp"
|
android:layout_height="75dp"
|
||||||
android:background="@android:drawable/screen_background_dark_transparent"
|
android:background="@android:drawable/screen_background_dark_transparent"
|
||||||
android:layout_alignParentBottom="true" />
|
android:layout_alignParentBottom="true" />
|
||||||
</RelativeLayout>
|
</RelativeLayout>
|
||||||
|
@@ -586,8 +586,8 @@ public final class TerminalView extends View {
|
|||||||
}
|
}
|
||||||
|
|
||||||
final int metaState = event.getMetaState();
|
final int metaState = event.getMetaState();
|
||||||
final boolean controlDownFromEvent = event.isCtrlPressed();
|
final boolean controlDownFromEvent = event.isCtrlPressed() || mClient.readControlKey();
|
||||||
final boolean leftAltDownFromEvent = (metaState & KeyEvent.META_ALT_LEFT_ON) != 0;
|
final boolean leftAltDownFromEvent = (metaState & KeyEvent.META_ALT_LEFT_ON) != 0 || mClient.readAltKey();
|
||||||
final boolean rightAltDownFromEvent = (metaState & KeyEvent.META_ALT_RIGHT_ON) != 0;
|
final boolean rightAltDownFromEvent = (metaState & KeyEvent.META_ALT_RIGHT_ON) != 0;
|
||||||
|
|
||||||
int keyMod = 0;
|
int keyMod = 0;
|
||||||
|
Reference in New Issue
Block a user