mirror of
https://github.com/KDE/kdeconnect-android
synced 2025-08-30 21:55:10 +00:00
Changes to the detection of keyboard input.
Now it detects more special keys. It does not detect modifiers anymore. Moved to a view that keeps the focus and prevents focusing the back button. Changed bitmasks for a hashmap to make it more readable with lots of keys. Separated sendSpecialKey from sendKey for non-representable keys. CCMAIL: saiarcot895@gmail.com
This commit is contained in:
@@ -0,0 +1,95 @@
|
|||||||
|
package org.kde.kdeconnect.Plugins.MousePadPlugin;
|
||||||
|
|
||||||
|
import android.content.Context;
|
||||||
|
import android.os.Build;
|
||||||
|
import android.util.AttributeSet;
|
||||||
|
import android.util.Log;
|
||||||
|
import android.view.KeyEvent;
|
||||||
|
import android.view.View;
|
||||||
|
|
||||||
|
import org.kde.kdeconnect.BackgroundService;
|
||||||
|
import org.kde.kdeconnect.Device;
|
||||||
|
|
||||||
|
import java.nio.ByteBuffer;
|
||||||
|
import java.util.HashMap;
|
||||||
|
|
||||||
|
public class KeyListenerView extends View {
|
||||||
|
|
||||||
|
private String deviceId;
|
||||||
|
|
||||||
|
private static HashMap<Integer, Integer> SpecialKeysMap = new HashMap<Integer, Integer>();
|
||||||
|
static {
|
||||||
|
int i = 0;
|
||||||
|
SpecialKeysMap.put(KeyEvent.KEYCODE_DEL, ++i); // 1
|
||||||
|
SpecialKeysMap.put(KeyEvent.KEYCODE_TAB, ++i); // 2
|
||||||
|
SpecialKeysMap.put(KeyEvent.KEYCODE_ENTER, ++i); // 3
|
||||||
|
SpecialKeysMap.put(KeyEvent.KEYCODE_DPAD_LEFT, ++i); // 4
|
||||||
|
SpecialKeysMap.put(KeyEvent.KEYCODE_DPAD_UP, ++i); // 5
|
||||||
|
SpecialKeysMap.put(KeyEvent.KEYCODE_DPAD_RIGHT, ++i); // 6
|
||||||
|
SpecialKeysMap.put(KeyEvent.KEYCODE_DPAD_DOWN, ++i); // 7
|
||||||
|
SpecialKeysMap.put(KeyEvent.KEYCODE_PAGE_UP, ++i); // 8
|
||||||
|
SpecialKeysMap.put(KeyEvent.KEYCODE_PAGE_DOWN, ++i); // 9
|
||||||
|
if (Build.VERSION.SDK_INT >= 11) {
|
||||||
|
SpecialKeysMap.put(KeyEvent.KEYCODE_MOVE_HOME, ++i); // 10
|
||||||
|
SpecialKeysMap.put(KeyEvent.KEYCODE_MOVE_END, ++i); // 11
|
||||||
|
SpecialKeysMap.put(KeyEvent.KEYCODE_NUMPAD_ENTER, ++i); // 12
|
||||||
|
SpecialKeysMap.put(KeyEvent.KEYCODE_FORWARD_DEL, ++i); // 13
|
||||||
|
SpecialKeysMap.put(KeyEvent.KEYCODE_ESCAPE, ++i); // 14
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setDeviceId(String id) {
|
||||||
|
deviceId = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public KeyListenerView(Context context, AttributeSet set) {
|
||||||
|
super(context, set);
|
||||||
|
|
||||||
|
setFocusable(true);
|
||||||
|
setFocusableInTouchMode(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean onCheckIsTextEditor() {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean onKeyUp(int keyCode, KeyEvent event) {
|
||||||
|
|
||||||
|
char utfChar = (char)event.getUnicodeChar();
|
||||||
|
if (utfChar == 9 || utfChar == 10) utfChar = 0; //Workaround to send enter and tab as special keys instead of characters
|
||||||
|
|
||||||
|
if (utfChar != 0) {
|
||||||
|
final String utfString = new String(new char[]{utfChar});
|
||||||
|
BackgroundService.RunCommand(getContext(), new BackgroundService.InstanceCallback() {
|
||||||
|
@Override
|
||||||
|
public void onServiceStart(BackgroundService service) {
|
||||||
|
Device device = service.getDevice(deviceId);
|
||||||
|
MousePadPlugin mousePadPlugin = (MousePadPlugin)device.getPlugin("plugin_mousepad");
|
||||||
|
if (mousePadPlugin == null) return;
|
||||||
|
|
||||||
|
mousePadPlugin.sendKey(utfString);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
if (!SpecialKeysMap.containsKey(keyCode)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
final int specialKey = SpecialKeysMap.get(keyCode);
|
||||||
|
BackgroundService.RunCommand(getContext(), new BackgroundService.InstanceCallback() {
|
||||||
|
@Override
|
||||||
|
public void onServiceStart(BackgroundService service) {
|
||||||
|
Device device = service.getDevice(deviceId);
|
||||||
|
MousePadPlugin mousePadPlugin = (MousePadPlugin)device.getPlugin("plugin_mousepad");
|
||||||
|
if (mousePadPlugin == null) return;
|
||||||
|
mousePadPlugin.sendSpecialKey(specialKey);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@@ -5,7 +5,6 @@ import android.content.Context;
|
|||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.view.GestureDetector;
|
import android.view.GestureDetector;
|
||||||
import android.view.inputmethod.InputMethodManager;
|
import android.view.inputmethod.InputMethodManager;
|
||||||
import android.view.KeyEvent;
|
|
||||||
import android.view.Menu;
|
import android.view.Menu;
|
||||||
import android.view.MenuInflater;
|
import android.view.MenuInflater;
|
||||||
import android.view.MenuItem;
|
import android.view.MenuItem;
|
||||||
@@ -16,6 +15,9 @@ import org.kde.kdeconnect.Device;
|
|||||||
import org.kde.kdeconnect_tp.R;
|
import org.kde.kdeconnect_tp.R;
|
||||||
|
|
||||||
public class MousePadActivity extends Activity implements GestureDetector.OnGestureListener, GestureDetector.OnDoubleTapListener, MousePadGestureDetector.OnGestureListener {
|
public class MousePadActivity extends Activity implements GestureDetector.OnGestureListener, GestureDetector.OnDoubleTapListener, MousePadGestureDetector.OnGestureListener {
|
||||||
|
|
||||||
|
String deviceId;
|
||||||
|
|
||||||
private final static float MinDistanceToSendScroll = 2.5f;
|
private final static float MinDistanceToSendScroll = 2.5f;
|
||||||
|
|
||||||
private float mPrevX;
|
private float mPrevX;
|
||||||
@@ -24,23 +26,29 @@ public class MousePadActivity extends Activity implements GestureDetector.OnGest
|
|||||||
private float mCurrentY;
|
private float mCurrentY;
|
||||||
|
|
||||||
boolean isScrolling = false;
|
boolean isScrolling = false;
|
||||||
|
|
||||||
float accumulatedDistanceY = 0;
|
float accumulatedDistanceY = 0;
|
||||||
|
|
||||||
private String deviceId;
|
|
||||||
|
|
||||||
private GestureDetector mDetector;
|
private GestureDetector mDetector;
|
||||||
|
|
||||||
private MousePadGestureDetector mMousePadGestureDetector;
|
private MousePadGestureDetector mMousePadGestureDetector;
|
||||||
|
|
||||||
|
KeyListenerView keyListenerView;
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onCreate(Bundle savedInstanceState) {
|
protected void onCreate(Bundle savedInstanceState) {
|
||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
|
|
||||||
setContentView(R.layout.activity_mousepad);
|
setContentView(R.layout.activity_mousepad);
|
||||||
|
|
||||||
deviceId = getIntent().getStringExtra("deviceId");
|
deviceId = getIntent().getStringExtra("deviceId");
|
||||||
|
|
||||||
mDetector = new GestureDetector(this, this);
|
mDetector = new GestureDetector(this, this);
|
||||||
mMousePadGestureDetector = new MousePadGestureDetector(this, this);
|
mMousePadGestureDetector = new MousePadGestureDetector(this, this);
|
||||||
mDetector.setOnDoubleTapListener(this);
|
mDetector.setOnDoubleTapListener(this);
|
||||||
|
|
||||||
|
keyListenerView = (KeyListenerView)findViewById(R.id.keyListener);
|
||||||
|
keyListenerView.setDeviceId(deviceId);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -67,18 +75,19 @@ public class MousePadActivity extends Activity implements GestureDetector.OnGest
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean onTouchEvent(MotionEvent event) {
|
public boolean onTouchEvent(MotionEvent event) {
|
||||||
if (mMousePadGestureDetector.onTouchEvent(event)) {
|
if (mMousePadGestureDetector.onTouchEvent(event)) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( mDetector.onTouchEvent(event) ) {
|
if ( mDetector.onTouchEvent(event) ) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
int actionType = event.getAction();
|
int actionType = event.getAction();
|
||||||
final float x = event.getX();
|
|
||||||
final float y = event.getY();
|
|
||||||
if (isScrolling) {
|
if (isScrolling) {
|
||||||
if (actionType == MotionEvent.ACTION_UP) {
|
if (actionType == MotionEvent.ACTION_UP) {
|
||||||
isScrolling = false;
|
isScrolling = false;
|
||||||
@@ -87,21 +96,22 @@ public class MousePadActivity extends Activity implements GestureDetector.OnGest
|
|||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
switch (actionType) {
|
switch (actionType) {
|
||||||
case MotionEvent.ACTION_DOWN:
|
case MotionEvent.ACTION_DOWN:
|
||||||
mPrevX = x;
|
mPrevX = event.getX();
|
||||||
mPrevY = y;
|
mPrevY = event.getY();
|
||||||
break;
|
break;
|
||||||
case MotionEvent.ACTION_MOVE:
|
case MotionEvent.ACTION_MOVE:
|
||||||
mCurrentX = x;
|
mCurrentX = event.getX();
|
||||||
mCurrentY = y;
|
mCurrentY = event.getY();
|
||||||
BackgroundService.RunCommand(this, new BackgroundService.InstanceCallback() {
|
BackgroundService.RunCommand(this, new BackgroundService.InstanceCallback() {
|
||||||
@Override
|
@Override
|
||||||
public void onServiceStart(BackgroundService service) {
|
public void onServiceStart(BackgroundService service) {
|
||||||
Device device = service.getDevice(deviceId);
|
Device device = service.getDevice(deviceId);
|
||||||
MousePadPlugin mousePadPlugin = (MousePadPlugin)device.getPlugin("plugin_mousepad");
|
MousePadPlugin mousePadPlugin = (MousePadPlugin)device.getPlugin("plugin_mousepad");
|
||||||
if (mousePadPlugin == null) return;
|
if (mousePadPlugin == null) return;
|
||||||
mousePadPlugin.sendPoints(mCurrentX - mPrevX, mCurrentY - mPrevY);
|
mousePadPlugin.sendMouseDelta(mCurrentX - mPrevX, mCurrentY - mPrevY);
|
||||||
mPrevX = mCurrentX;
|
mPrevX = mCurrentX;
|
||||||
mPrevY = mCurrentY;
|
mPrevY = mCurrentY;
|
||||||
}
|
}
|
||||||
@@ -118,7 +128,7 @@ public class MousePadActivity extends Activity implements GestureDetector.OnGest
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onShowPress(MotionEvent e) {
|
public void onShowPress(MotionEvent e) {
|
||||||
|
//From GestureDetector, left empty
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -158,7 +168,7 @@ public class MousePadActivity extends Activity implements GestureDetector.OnGest
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onLongPress(MotionEvent e) {
|
public void onLongPress(MotionEvent e) {
|
||||||
|
//From GestureDetector, left empty
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -211,55 +221,6 @@ public class MousePadActivity extends Activity implements GestureDetector.OnGest
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean onKeyDown(int keyCode, final KeyEvent event) {
|
|
||||||
String character = new String(new char[] {(char) event.getUnicodeChar(0)});
|
|
||||||
int modifier = 0;
|
|
||||||
if (keyCode == KeyEvent.KEYCODE_DEL) {
|
|
||||||
modifier = 1;
|
|
||||||
} else if (keyCode == KeyEvent.KEYCODE_ENTER) {
|
|
||||||
modifier = 1 << 1;
|
|
||||||
} else if (keyCode == KeyEvent.KEYCODE_TAB) {
|
|
||||||
modifier = 1 << 1 | 1;
|
|
||||||
}
|
|
||||||
// Add space for Home, End, Page Up, and Page Down
|
|
||||||
|
|
||||||
if (event.isShiftPressed()) {
|
|
||||||
modifier |= 1 << 3;
|
|
||||||
}
|
|
||||||
if (android.os.Build.VERSION.SDK_INT >= 11) {
|
|
||||||
if (event.isCtrlPressed()) {
|
|
||||||
modifier |= 1 << 4;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (character.charAt(0) == 0 && modifier == 0) {
|
|
||||||
return super.onKeyDown(keyCode, event);
|
|
||||||
}
|
|
||||||
final String characterToSend;
|
|
||||||
if (character.charAt(0) == 0) {
|
|
||||||
characterToSend = "";
|
|
||||||
} else {
|
|
||||||
characterToSend = character;
|
|
||||||
}
|
|
||||||
BackgroundService.RunCommand(this, new BackgroundService.InstanceCallback() {
|
|
||||||
@Override
|
|
||||||
public void onServiceStart(BackgroundService service) {
|
|
||||||
Device device = service.getDevice(deviceId);
|
|
||||||
MousePadPlugin mousePadPlugin = (MousePadPlugin)device.getPlugin("plugin_mousepad");
|
|
||||||
if (mousePadPlugin == null) return;
|
|
||||||
mousePadPlugin.sendKey(characterToSend, 0);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean onKeyUp(int keyCode, KeyEvent event) {
|
|
||||||
if (event.getUnicodeChar() == 0) {
|
|
||||||
return super.onKeyDown(keyCode, event);
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
private void sendMiddleClick() {
|
private void sendMiddleClick() {
|
||||||
BackgroundService.RunCommand(this, new BackgroundService.InstanceCallback() {
|
BackgroundService.RunCommand(this, new BackgroundService.InstanceCallback() {
|
||||||
@@ -287,6 +248,8 @@ public class MousePadActivity extends Activity implements GestureDetector.OnGest
|
|||||||
|
|
||||||
private void showKeyboard() {
|
private void showKeyboard() {
|
||||||
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
|
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
|
||||||
imm.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT, 0);
|
imm.showSoftInput(keyListenerView, InputMethodManager.SHOW_IMPLICIT);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -56,7 +56,22 @@ public class MousePadPlugin extends Plugin {
|
|||||||
@Override
|
@Override
|
||||||
public AlertDialog getErrorDialog(Context baseContext) { return null; }
|
public AlertDialog getErrorDialog(Context baseContext) { return null; }
|
||||||
|
|
||||||
public void sendPoints(float dx, float dy) {
|
@Override
|
||||||
|
public Button getInterfaceButton(final Activity activity) {
|
||||||
|
Button button = new Button(activity);
|
||||||
|
button.setText(R.string.open_mousepad);
|
||||||
|
button.setOnClickListener(new View.OnClickListener() {
|
||||||
|
@Override
|
||||||
|
public void onClick(View v) {
|
||||||
|
Intent intent = new Intent(activity, MousePadActivity.class);
|
||||||
|
intent.putExtra("deviceId", device.getDeviceId());
|
||||||
|
activity.startActivity(intent);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
return button;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void sendMouseDelta(float dx, float dy) {
|
||||||
NetworkPackage np = new NetworkPackage(NetworkPackage.PACKAGE_TYPE_MOUSEPAD);
|
NetworkPackage np = new NetworkPackage(NetworkPackage.PACKAGE_TYPE_MOUSEPAD);
|
||||||
np.set("dx", dx);
|
np.set("dx", dx);
|
||||||
np.set("dy", dy);
|
np.set("dy", dy);
|
||||||
@@ -95,25 +110,16 @@ public class MousePadPlugin extends Plugin {
|
|||||||
device.sendPackage(np);
|
device.sendPackage(np);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void sendKey(String key, int modifier) {
|
public void sendKey(String utfChar) {
|
||||||
NetworkPackage np = new NetworkPackage(NetworkPackage.PACKAGE_TYPE_MOUSEPAD);
|
NetworkPackage np = new NetworkPackage(NetworkPackage.PACKAGE_TYPE_MOUSEPAD);
|
||||||
np.set("key", key);
|
np.set("key", utfChar);
|
||||||
np.set("modifier", modifier);
|
|
||||||
device.sendPackage(np);
|
device.sendPackage(np);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
public void sendSpecialKey(int specialKey) {
|
||||||
public Button getInterfaceButton(final Activity activity) {
|
NetworkPackage np = new NetworkPackage(NetworkPackage.PACKAGE_TYPE_MOUSEPAD);
|
||||||
Button button = new Button(activity);
|
np.set("specialKey", specialKey);
|
||||||
button.setText(R.string.open_mousepad);
|
device.sendPackage(np);
|
||||||
button.setOnClickListener(new View.OnClickListener() {
|
|
||||||
@Override
|
|
||||||
public void onClick(View v) {
|
|
||||||
Intent intent = new Intent(activity, MousePadActivity.class);
|
|
||||||
intent.putExtra("deviceId", device.getDeviceId());
|
|
||||||
activity.startActivity(intent);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
return button;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@@ -15,4 +15,13 @@
|
|||||||
android:layout_centerInParent="true"
|
android:layout_centerInParent="true"
|
||||||
android:padding="12dip" />
|
android:padding="12dip" />
|
||||||
|
|
||||||
|
<view
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
class="org.kde.kdeconnect.Plugins.MousePadPlugin.KeyListenerView"
|
||||||
|
android:id="@+id/keyListener"
|
||||||
|
android:layout_alignParentTop="true"
|
||||||
|
android:layout_centerHorizontal="true"
|
||||||
|
android:layout_marginTop="58dp" />
|
||||||
|
|
||||||
</RelativeLayout>
|
</RelativeLayout>
|
Reference in New Issue
Block a user