2
0
mirror of https://github.com/KDE/kdeconnect-android synced 2025-08-30 05:37:43 +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:
Albert Vaca 2014-08-14 21:40:04 +02:00
parent d02684c714
commit 1fe2590ec8
4 changed files with 155 additions and 82 deletions

View File

@ -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;
}
}

View File

@ -5,7 +5,6 @@ import android.content.Context;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.inputmethod.InputMethodManager;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
@ -16,6 +15,9 @@ import org.kde.kdeconnect.Device;
import org.kde.kdeconnect_tp.R;
public class MousePadActivity extends Activity implements GestureDetector.OnGestureListener, GestureDetector.OnDoubleTapListener, MousePadGestureDetector.OnGestureListener {
String deviceId;
private final static float MinDistanceToSendScroll = 2.5f;
private float mPrevX;
@ -24,23 +26,29 @@ public class MousePadActivity extends Activity implements GestureDetector.OnGest
private float mCurrentY;
boolean isScrolling = false;
float accumulatedDistanceY = 0;
private String deviceId;
private GestureDetector mDetector;
private MousePadGestureDetector mMousePadGestureDetector;
KeyListenerView keyListenerView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mousepad);
deviceId = getIntent().getStringExtra("deviceId");
mDetector = new GestureDetector(this, this);
mMousePadGestureDetector = new MousePadGestureDetector(this, this);
mDetector.setOnDoubleTapListener(this);
keyListenerView = (KeyListenerView)findViewById(R.id.keyListener);
keyListenerView.setDeviceId(deviceId);
}
@Override
@ -67,18 +75,19 @@ public class MousePadActivity extends Activity implements GestureDetector.OnGest
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if (mMousePadGestureDetector.onTouchEvent(event)) {
return true;
}
if ( mDetector.onTouchEvent(event) ) {
return true;
}
int actionType = event.getAction();
final float x = event.getX();
final float y = event.getY();
if (isScrolling) {
if (actionType == MotionEvent.ACTION_UP) {
isScrolling = false;
@ -87,21 +96,22 @@ public class MousePadActivity extends Activity implements GestureDetector.OnGest
}
}
switch (actionType) {
case MotionEvent.ACTION_DOWN:
mPrevX = x;
mPrevY = y;
mPrevX = event.getX();
mPrevY = event.getY();
break;
case MotionEvent.ACTION_MOVE:
mCurrentX = x;
mCurrentY = y;
mCurrentX = event.getX();
mCurrentY = event.getY();
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.sendPoints(mCurrentX - mPrevX, mCurrentY - mPrevY);
mousePadPlugin.sendMouseDelta(mCurrentX - mPrevX, mCurrentY - mPrevY);
mPrevX = mCurrentX;
mPrevY = mCurrentY;
}
@ -118,7 +128,7 @@ public class MousePadActivity extends Activity implements GestureDetector.OnGest
@Override
public void onShowPress(MotionEvent e) {
//From GestureDetector, left empty
}
@Override
@ -158,7 +168,7 @@ public class MousePadActivity extends Activity implements GestureDetector.OnGest
@Override
public void onLongPress(MotionEvent e) {
//From GestureDetector, left empty
}
@Override
@ -211,55 +221,6 @@ public class MousePadActivity extends Activity implements GestureDetector.OnGest
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() {
BackgroundService.RunCommand(this, new BackgroundService.InstanceCallback() {
@ -287,6 +248,8 @@ public class MousePadActivity extends Activity implements GestureDetector.OnGest
private void showKeyboard() {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT, 0);
imm.showSoftInput(keyListenerView, InputMethodManager.SHOW_IMPLICIT);
}
}

View File

@ -56,7 +56,22 @@ public class MousePadPlugin extends Plugin {
@Override
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);
np.set("dx", dx);
np.set("dy", dy);
@ -95,25 +110,16 @@ public class MousePadPlugin extends Plugin {
device.sendPackage(np);
}
public void sendKey(String key, int modifier) {
public void sendKey(String utfChar) {
NetworkPackage np = new NetworkPackage(NetworkPackage.PACKAGE_TYPE_MOUSEPAD);
np.set("key", key);
np.set("modifier", modifier);
np.set("key", utfChar);
device.sendPackage(np);
}
@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 sendSpecialKey(int specialKey) {
NetworkPackage np = new NetworkPackage(NetworkPackage.PACKAGE_TYPE_MOUSEPAD);
np.set("specialKey", specialKey);
device.sendPackage(np);
}
}

View File

@ -15,4 +15,13 @@
android:layout_centerInParent="true"
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>