2
0
mirror of https://github.com/KDE/kdeconnect-android synced 2025-08-29 13:17:43 +00:00

Added support for Ctrl, Alt, Shift combinations and F keys to keyboard.

To use with keyboards like Hacker's Keyboard (tested with it).
This commit is contained in:
Albert Vaca 2015-01-06 00:05:44 -08:00
parent 96e8610363
commit 332c2d4856
2 changed files with 90 additions and 42 deletions

View File

@ -23,7 +23,6 @@ package org.kde.kdeconnect.Plugins.MousePadPlugin;
import android.content.Context; import android.content.Context;
import android.os.Build; import android.os.Build;
import android.util.AttributeSet; import android.util.AttributeSet;
import android.util.Log;
import android.view.KeyEvent; import android.view.KeyEvent;
import android.view.View; import android.view.View;
import android.view.inputmethod.EditorInfo; import android.view.inputmethod.EditorInfo;
@ -31,8 +30,8 @@ import android.view.inputmethod.InputConnection;
import org.kde.kdeconnect.BackgroundService; import org.kde.kdeconnect.BackgroundService;
import org.kde.kdeconnect.Device; import org.kde.kdeconnect.Device;
import org.kde.kdeconnect.NetworkPackage;
import java.nio.ByteBuffer;
import java.util.HashMap; import java.util.HashMap;
public class KeyListenerView extends View { public class KeyListenerView extends View {
@ -57,6 +56,24 @@ public class KeyListenerView extends View {
SpecialKeysMap.put(KeyEvent.KEYCODE_NUMPAD_ENTER, ++i); // 12 SpecialKeysMap.put(KeyEvent.KEYCODE_NUMPAD_ENTER, ++i); // 12
SpecialKeysMap.put(KeyEvent.KEYCODE_FORWARD_DEL, ++i); // 13 SpecialKeysMap.put(KeyEvent.KEYCODE_FORWARD_DEL, ++i); // 13
SpecialKeysMap.put(KeyEvent.KEYCODE_ESCAPE, ++i); // 14 SpecialKeysMap.put(KeyEvent.KEYCODE_ESCAPE, ++i); // 14
SpecialKeysMap.put(KeyEvent.KEYCODE_SYSRQ, ++i); // 15
SpecialKeysMap.put(KeyEvent.KEYCODE_SCROLL_LOCK, ++i); // 16
++i; // 17
++i; // 18
++i; // 19
++i; // 20
SpecialKeysMap.put(KeyEvent.KEYCODE_F1, ++i); // 21
SpecialKeysMap.put(KeyEvent.KEYCODE_F2, ++i); // 22
SpecialKeysMap.put(KeyEvent.KEYCODE_F3, ++i); // 23
SpecialKeysMap.put(KeyEvent.KEYCODE_F4, ++i); // 24
SpecialKeysMap.put(KeyEvent.KEYCODE_F5, ++i); // 25
SpecialKeysMap.put(KeyEvent.KEYCODE_F6, ++i); // 26
SpecialKeysMap.put(KeyEvent.KEYCODE_F7, ++i); // 27
SpecialKeysMap.put(KeyEvent.KEYCODE_F8, ++i); // 28
SpecialKeysMap.put(KeyEvent.KEYCODE_F9, ++i); // 29
SpecialKeysMap.put(KeyEvent.KEYCODE_F10, ++i); // 30
SpecialKeysMap.put(KeyEvent.KEYCODE_F11, ++i); // 31
SpecialKeysMap.put(KeyEvent.KEYCODE_F12, ++i); // 21
} }
} }
@ -85,37 +102,77 @@ public class KeyListenerView extends View {
@Override @Override
public boolean onKeyUp(int keyCode, KeyEvent event) { public boolean onKeyUp(int keyCode, KeyEvent event) {
char utfChar = (char)event.getUnicodeChar(); //Log.e("KeyDown", "------------");
if (utfChar == 9 || utfChar == 10) utfChar = 0; //Workaround to send enter and tab as special keys instead of characters //Log.e("KeyDown", "keyChar:" + (int) event.getDisplayLabel());
//Log.e("KeyDown", "utfChar:" + (char)event.getUnicodeChar());
//Log.e("KeyDown", "intUtfChar:" + event.getUnicodeChar());
final NetworkPackage np = new NetworkPackage(NetworkPackage.PACKAGE_TYPE_MOUSEPAD);
boolean modifier = false;
if (event.isAltPressed()) {
np.set("alt", true);
modifier = true;
}
if (Build.VERSION.SDK_INT >= 11) {
if (event.isCtrlPressed()) {
np.set("ctrl", true);
modifier = true;
}
}
if (modifier) {
//Only send shift in combination with other modifiers or special keys. Otherwise let it modify the letter itself and get the final result in utf.
if (event.isShiftPressed()) {
np.set("shift", true);
}
if (SpecialKeysMap.containsKey(keyCode)) {
int specialKey = SpecialKeysMap.get(keyCode);
np.set("specialKey", specialKey);
} else if (event.getDisplayLabel() != 0) {
//Alt will change the utf symbol to non-ascii characters, we want the plain original letter
//Since getDisplayLabel will always have a value, we have to check for special keys before
char keyCharacter = event.getDisplayLabel();
np.set("key", new String(new char[]{keyCharacter}).toLowerCase());
} else {
return false; //We don't know what to send, better send nothing. Probably this is the modifier key itself.
}
} else {
//If it's not a modifier+key combination, we want the fancy (potentially utf) version of the key pressed
char utfChar = (char) event.getUnicodeChar();
//Workaround to send enter and tab as special keys instead of characters
if (utfChar == 9 || utfChar == 10) utfChar = 0;
if (utfChar != 0) { if (utfChar != 0) {
final String utfString = new String(new char[]{utfChar}); String utfString = new String(new char[]{utfChar});
BackgroundService.RunCommand(getContext(), new BackgroundService.InstanceCallback() { np.set("key", utfString);
@Override } else if (SpecialKeysMap.containsKey(keyCode)) {
public void onServiceStart(BackgroundService service) { //Only send shift in combination with other modifiers or special keys. Otherwise let it modify the letter itself and get the final result in utf.
Device device = service.getDevice(deviceId); if (event.isShiftPressed()) {
MousePadPlugin mousePadPlugin = (MousePadPlugin)device.getPlugin("plugin_mousepad"); np.set("shift", true);
if (mousePadPlugin == null) return;
mousePadPlugin.sendKey(utfString);
} }
}); //If it was not a displayable character, check if it was a special key
int specialKey = SpecialKeysMap.get(keyCode);
np.set("specialKey", specialKey);
} else { } else {
if (!SpecialKeysMap.containsKey(keyCode)) { return false; //We don't know what to send, better send nothing. Probably this is an unhandled special key.
return false; }
} }
final int specialKey = SpecialKeysMap.get(keyCode);
BackgroundService.RunCommand(getContext(), new BackgroundService.InstanceCallback() { BackgroundService.RunCommand(getContext(), 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.sendSpecialKey(specialKey); mousePadPlugin.sendKeyboardPacket(np);
} }
}); });
}
return true; return true;
} }

View File

@ -22,7 +22,6 @@ package org.kde.kdeconnect.Plugins.MousePadPlugin;
import android.app.Activity; import android.app.Activity;
import android.app.AlertDialog; import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.graphics.drawable.Drawable; import android.graphics.drawable.Drawable;
import android.view.View; import android.view.View;
@ -142,15 +141,7 @@ public class MousePadPlugin extends Plugin {
device.sendPackage(np); device.sendPackage(np);
} }
public void sendKey(String utfChar) { public void sendKeyboardPacket(NetworkPackage np) {
NetworkPackage np = new NetworkPackage(NetworkPackage.PACKAGE_TYPE_MOUSEPAD);
np.set("key", utfChar);
device.sendPackage(np);
}
public void sendSpecialKey(int specialKey) {
NetworkPackage np = new NetworkPackage(NetworkPackage.PACKAGE_TYPE_MOUSEPAD);
np.set("specialKey", specialKey);
device.sendPackage(np); device.sendPackage(np);
} }