Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added the ability to customize keyboard shortcuts. Fix for: #92 and #13 #100

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package net.christianbeier.droidvnc_ng;

public class HotkeyBinding implements Comparable<HotkeyBinding> {
private final VNCKey[] keys = new VNCKey[3];
// placeholder for command to execute by this hotkey
private final ICommand command;
// user-friendly name. Uses in GUI
public final String friendlyName;
// uses for settings serialization
public final String PREFS_NAME;

public HotkeyBinding(String friendlyName, String prefsName, VNCKey key1, VNCKey key2, VNCKey key3, ICommand command) {
keys[0] = key1;
keys[1] = key2;
keys[2] = key3;
this.command = command;
this.friendlyName = friendlyName;
this.PREFS_NAME = prefsName;
}

public boolean isTriggered() {
for (VNCKey key : keys) {
if (key != null && key != VNCKey.Empty && key.isNotDown())
return false;
}
return true;
}

public boolean tryExecute() {
if (isTriggered()) {
command.execute();
return true;
}
return false;
}

public VNCKey getKey(int index) {
if (index > 3) return VNCKey.Empty;
return keys[index];
}

public void setKey(int index, VNCKey key) {
if (index > 3) return;
keys[index] = key;
}

@Override
public int compareTo(HotkeyBinding hotkeyBinding) {
return hotkeyBinding.keys.length - keys.length;
}

public interface ICommand {
void execute();
}
}
92 changes: 43 additions & 49 deletions app/src/main/java/net/christianbeier/droidvnc_ng/InputService.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
*
* Swipe fixes and gesture handling by Christian Beier <[email protected]>.
*
* Expanded hotkey system Alexander Balanyuk Anatolevich [email protected]
*
*/

import android.accessibilityservice.AccessibilityService;
Expand Down Expand Up @@ -56,16 +58,42 @@ public synchronized void onCancelled(GestureDescription gestureDescription) {
private Path mPath;
private long mLastGestureStartTime;

private boolean mIsKeyCtrlDown;
private boolean mIsKeyAltDown;
private boolean mIsKeyShiftDown;
private boolean mIsKeyDelDown;
private boolean mIsKeyEscDown;

private float mScaling;

private final GestureCallback mGestureCallback = new GestureCallback();

// FIXME: I don't know how to get out of this situation without using the static keyword.
// It might be worth moving this block into the HotkeyHandler class and leaving it static,
// but I can't imagine a good way to do this.
public static final HotkeyBinding[] HOTKEY_BINDINGS = new HotkeyBinding[]{
new HotkeyBinding("Portrait/Landscape Workaround", "hotkey_portrait_landscape_Workaround_",
VNCKey.Ctrl, VNCKey.Alt, VNCKey.Del,
() -> instance.mMainHandler.post(MainService::togglePortraitInLandscapeWorkaround)),

new HotkeyBinding("Show Recents", "hotkey_show_recents_",
VNCKey.Ctrl, VNCKey.Shift, VNCKey.Esc,
() -> instance.performGlobalAction(AccessibilityService.GLOBAL_ACTION_RECENTS)),

new HotkeyBinding("Back", "hotkey_back_",
VNCKey.Esc, VNCKey.Empty, VNCKey.Empty,
() -> instance.performGlobalAction(AccessibilityService.GLOBAL_ACTION_BACK)),

new HotkeyBinding("Home", "hotkey_home_",
VNCKey.Home, VNCKey.Empty, VNCKey.Empty,
() -> instance.performGlobalAction(AccessibilityService.GLOBAL_ACTION_HOME)),

new HotkeyBinding("Power dialog", "hotkey_power_dialog_",
VNCKey.Backspace, VNCKey.Empty, VNCKey.Empty,
() -> instance.performGlobalAction(AccessibilityService.GLOBAL_ACTION_POWER_DIALOG)),

new HotkeyBinding("Notifications", "hotkey_notifications_",
VNCKey.PageDown, VNCKey.Empty, VNCKey.Empty,
() -> instance.performGlobalAction(AccessibilityService.GLOBAL_ACTION_NOTIFICATIONS)),

new HotkeyBinding("Split screen", "hotkey_split_screen_",
VNCKey.PageUp, VNCKey.Empty, VNCKey.Empty,
() -> instance.performGlobalAction(AccessibilityService.GLOBAL_ACTION_TOGGLE_SPLIT_SCREEN))
};

@Override
public void onAccessibilityEvent( AccessibilityEvent event ) { }
Expand Down Expand Up @@ -178,51 +206,17 @@ public static void onKeyEvent(int down, long keysym, long client) {
/*
Save states of some keys for combo handling.
*/
if(keysym == 0xFFE3)
instance.mIsKeyCtrlDown = down != 0;

if(keysym == 0xFFE9 || keysym == 0xFF7E) // MacOS clients send Alt as 0xFF7E
instance.mIsKeyAltDown = down != 0;

if(keysym == 0xFFE1)
instance.mIsKeyShiftDown = down != 0;

if(keysym == 0xFFFF)
instance.mIsKeyDelDown = down != 0;
boolean isKeyExist = VNCKey.tryChangeKeyState(keysym, down);

if(keysym == 0xFF1B)
instance.mIsKeyEscDown = down != 0;
if (isKeyExist) {
for (HotkeyBinding handler : HOTKEY_BINDINGS) {
boolean isCommandExecuted = handler.tryExecute();

/*
Ctrl-Alt-Del combo.
*/
if(instance.mIsKeyCtrlDown && instance.mIsKeyAltDown && instance.mIsKeyDelDown) {
Log.i(TAG, "onKeyEvent: got Ctrl-Alt-Del");
instance.mMainHandler.post(MainService::togglePortraitInLandscapeWorkaround);
}

/*
Ctrl-Shift-Esc combo.
*/
if(instance.mIsKeyCtrlDown && instance.mIsKeyShiftDown && instance.mIsKeyEscDown) {
Log.i(TAG, "onKeyEvent: got Ctrl-Shift-Esc");
instance.performGlobalAction(AccessibilityService.GLOBAL_ACTION_RECENTS);
}

/*
Home/Pos1
*/
if (keysym == 0xFF50 && down != 0) {
Log.i(TAG, "onKeyEvent: got Home/Pos1");
instance.performGlobalAction(AccessibilityService.GLOBAL_ACTION_HOME);
}

/*
Esc
*/
if(keysym == 0xFF1B && down != 0) {
Log.i(TAG, "onKeyEvent: got Home/Pos1");
instance.performGlobalAction(AccessibilityService.GLOBAL_ACTION_BACK);
if (isCommandExecuted) {
Log.i(TAG, handler.PREFS_NAME + "X executed");
break;
}
}
}

} catch (Exception e) {
Expand Down
75 changes: 75 additions & 0 deletions app/src/main/java/net/christianbeier/droidvnc_ng/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,14 @@
import android.util.Log;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

Expand Down Expand Up @@ -72,6 +76,77 @@ protected void onCreate(Bundle savedInstanceState) {

final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);

/*---------------- start of hotkey editor logic ----------------*/

LinearLayout hotkeysEditPanel = findViewById(R.id.hotkeysEditPanel);

for (HotkeyBinding handler : InputService.HOTKEY_BINDINGS) {

LinearLayout mainVComposer = new LinearLayout(this);
mainVComposer.setOrientation(LinearLayout.VERTICAL);

TextView nameLabel = new TextView(this);
nameLabel.setText(handler.friendlyName);
mainVComposer.addView(nameLabel);

// loading saved hotkeys settings
LinearLayout horizontalComposer = new LinearLayout(this);
horizontalComposer.setOrientation(LinearLayout.HORIZONTAL);
for (int i = 0; i < 3; i++) {
VNCKey key = VNCKey.getKeyByName(prefs.getString(handler.PREFS_NAME + i, handler.getKey(i).toString()));
handler.setKey(i, key);
}

// Used to evenly place selectors on a view
LinearLayout.LayoutParams keySelectorLayout = new LinearLayout.LayoutParams (
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT,
1.0f
);

for (int i = 0; i < 3; i++) {
Spinner keySelector = new Spinner(this);
ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, VNCKey.toStringsArray());
adapter.setDropDownViewResource(android.R.layout.simple_spinner_item);
keySelector.setAdapter(adapter);
keySelector.setLayoutParams(keySelectorLayout);
keySelector.setTag(i);

int selectionPosition = adapter.getPosition(handler.getKey((int) keySelector.getTag()).toString());
keySelector.setSelection(selectionPosition);

keySelector.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
int keyIndex = (int) keySelector.getTag();
String keyName = keySelector.getSelectedItem().toString();
SharedPreferences.Editor ed = prefs.edit();
ed.putString(handler.PREFS_NAME + keyIndex, keyName);
ed.apply();

VNCKey selectedKey = VNCKey.getKeyByName(keyName);
handler.setKey(keyIndex, selectedKey);
}

@Override
public void onNothingSelected(AdapterView<?> adapterView) {

}
});
horizontalComposer.addView(keySelector);
}
mainVComposer.addView(horizontalComposer);
hotkeysEditPanel.addView(mainVComposer);
}

SwitchMaterial showEditPanel = findViewById(R.id.showEditPanel);

showEditPanel.setOnCheckedChangeListener((v, checked) -> {
hotkeysEditPanel.setVisibility(checked ? View.VISIBLE : View.GONE );
});

/*---------------- end of hotkey editor logic ----------------*/

mButtonToggle = (Button) findViewById(R.id.toggle);
mButtonToggle.setOnClickListener(view -> {

Expand Down
84 changes: 84 additions & 0 deletions app/src/main/java/net/christianbeier/droidvnc_ng/VNCKey.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package net.christianbeier.droidvnc_ng;

import java.util.HashMap;

public enum VNCKey {
/*
List of registered key codes and their corresponding keys.
Easy to edit
*/
Empty(0x0L),
Ctrl(0xFFE3L),
Alt(0xFFE9L),
MacOSAlt(0xFF7EL),
Shift(0xFFE1L),
Del(0xFFFFL),
Esc(0xFF1BL),
Home(0xFF50L),
Tab(0xFF09L),
PageUp(0xFF55L),
PageDown(0xFF56L),
End(0xFF57L),
Backspace(0xFF08L),
F1(0xFFBEL),
F2(0xFFBFL),
F3(0xFFC0L),
F4(0xFFC1L),
F5(0xFFC2L),
F6(0xFFC3L),
F7(0xFFC4L),
F8(0xFFC5L),
F9(0xFFC6L),
F10(0xFFC7L),
F11(0xFFC8L),
F12(0xFFC9L);

/*
Used for easy access to key states. Helps to get rid of endless if/else constructs
*/
private static final HashMap<Long, VNCKey> KeyRegistrar = new HashMap<Long, VNCKey>() {{
for (VNCKey k : VNCKey.values())
put(k.keysym, k);
}};

public final long keysym;
private boolean isDown = false;

VNCKey(long keysym) {
this.keysym = keysym;
}

public boolean isNotDown() {
return !isDown;
}

public void setKeyState(int state) {
isDown = state != 0;
}

public static boolean tryChangeKeyState(long keysym, int keystate) {
if (KeyRegistrar.containsKey(keysym)) {
KeyRegistrar.get(keysym).setKeyState(keystate);

return true;
}
return false;
}

public static String[] toStringsArray() {
String[] result = new String[KeyRegistrar.size()];
VNCKey[] values = VNCKey.values();
for (int i = 0; i < result.length; i++) {
result[i] = values[i].name();
}
return result;
}

public static VNCKey getKeyByName(String keyName) {
try {
return VNCKey.valueOf(keyName);
} catch (Exception ignored) {
return null;
}
}
}
Loading