Move cursor by sliding on the space bar

Send key events for the left or right arrow as the finger slides on the
space bar.
Can be used to select text by holding shift. Works under Termux.

Events are sent linearly as the finger travels. The distance between
each events is defined from the swiping distance divided by 4.

'slider="true"' can be set on a key that have 'edgekeys="true"'.
'key2' and 'key3' represent the right and left keys.
This commit is contained in:
Jules Aguillon 2023-01-22 23:03:30 +01:00
parent f4032e3be9
commit 854eff211d
4 changed files with 79 additions and 10 deletions

View File

@ -2,7 +2,7 @@
<row height="0.95"> <row height="0.95">
<key width="1.8" key0="ctrl" key1="loc switch_greekmath" key2="loc meta" key4="switch_numeric"/> <key width="1.8" key0="ctrl" key1="loc switch_greekmath" key2="loc meta" key4="switch_numeric"/>
<key width="1.2" key0="fn" key1="loc alt" key2="change_method" key3="switch_emoji" key4="config"/> <key width="1.2" key0="fn" key1="loc alt" key2="change_method" key3="switch_emoji" key4="config"/>
<key width="4.0" key0="space" key1="switch_second" edgekeys="true"/> <key width="4.0" key0="space" key1="switch_second" key3="left" key2="right" edgekeys="true" slider="true"/>
<key width="1.2" key1="up" key2="right" key3="left" key4="down" edgekeys="true"/> <key width="1.2" key1="up" key2="right" key3="left" key4="down" edgekeys="true"/>
<key width="1.8" key0="enter" key2="action"/> <key width="1.8" key0="enter" key2="action"/>
</row> </row>

View File

@ -29,6 +29,7 @@ final class Config
// From the 'numpad_layout' option, also apply to the numeric pane. // From the 'numpad_layout' option, also apply to the numeric pane.
public boolean inverse_numpad = false; public boolean inverse_numpad = false;
public float swipe_dist_px; public float swipe_dist_px;
public float slide_step_px;
public boolean vibrateEnabled; public boolean vibrateEnabled;
public long longPressTimeout; public long longPressTimeout;
public long longPressInterval; public long longPressInterval;
@ -118,6 +119,7 @@ final class Config
float swipe_scaling = Math.min(dm.widthPixels, dm.heightPixels) / 10.f * dpi_ratio; float swipe_scaling = Math.min(dm.widthPixels, dm.heightPixels) / 10.f * dpi_ratio;
float swipe_dist_value = Float.valueOf(_prefs.getString("swipe_dist", "15")); float swipe_dist_value = Float.valueOf(_prefs.getString("swipe_dist", "15"));
swipe_dist_px = swipe_dist_value / 25.f * swipe_scaling; swipe_dist_px = swipe_dist_value / 25.f * swipe_scaling;
slide_step_px = swipe_dist_px / 4.f;
vibrateEnabled = _prefs.getBoolean("vibrate_enabled", true); vibrateEnabled = _prefs.getBoolean("vibrate_enabled", true);
longPressTimeout = _prefs.getInt("longpress_timeout", 600); longPressTimeout = _prefs.getInt("longpress_timeout", 600);
longPressInterval = _prefs.getInt("longpress_interval", 65); longPressInterval = _prefs.getInt("longpress_interval", 65);

View File

@ -285,9 +285,13 @@ class KeyboardData
public final float shift; public final float shift;
/** Put keys 1 to 4 on the edges instead of the corners. */ /** Put keys 1 to 4 on the edges instead of the corners. */
public final boolean edgekeys; public final boolean edgekeys;
/** Keys 2 and 3 are repeated as the finger moves laterally on the key.
Used for the left and right arrow keys on the space bar. */
public final boolean slider;
/** String printed on the keys. It has no other effect. */
public final String indication; public final String indication;
protected Key(Corner k0, Corner k1, Corner k2, Corner k3, Corner k4, float w, float s, boolean e, String i) protected Key(Corner k0, Corner k1, Corner k2, Corner k3, Corner k4, float w, float s, boolean e, boolean sl, String i)
{ {
key0 = k0; key0 = k0;
key1 = k1; key1 = k1;
@ -297,6 +301,7 @@ class KeyboardData
width = w; width = w;
shift = s; shift = s;
edgekeys = e; edgekeys = e;
slider = sl;
indication = i; indication = i;
} }
@ -310,17 +315,18 @@ class KeyboardData
float width = attribute_float(parser, "width", 1f); float width = attribute_float(parser, "width", 1f);
float shift = attribute_float(parser, "shift", 0.f); float shift = attribute_float(parser, "shift", 0.f);
boolean edgekeys = attribute_bool(parser, "edgekeys", false); boolean edgekeys = attribute_bool(parser, "edgekeys", false);
boolean slider = attribute_bool(parser, "slider", false);
String indication = parser.getAttributeValue(null, "indication"); String indication = parser.getAttributeValue(null, "indication");
while (parser.next() != XmlPullParser.END_TAG) while (parser.next() != XmlPullParser.END_TAG)
continue ; continue ;
return new Key(k0, k1, k2, k3, k4, width, shift, edgekeys, indication); return new Key(k0, k1, k2, k3, k4, width, shift, edgekeys, slider, indication);
} }
/** New key with the width multiplied by 's'. */ /** New key with the width multiplied by 's'. */
public Key scaleWidth(float s) public Key scaleWidth(float s)
{ {
return new Key(key0, key1, key2, key3, key4, width * s, shift, edgekeys, return new Key(key0, key1, key2, key3, key4, width * s, shift, edgekeys,
indication); slider, indication);
} }
public KeyValue getKeyValue(int i) public KeyValue getKeyValue(int i)
@ -350,12 +356,14 @@ class KeyboardData
case 3: k3 = k; break; case 3: k3 = k; break;
case 4: k4 = k; break; case 4: k4 = k; break;
} }
return new Key(k0, k1, k2, k3, k4, width, shift, edgekeys, indication); return new Key(k0, k1, k2, k3, k4, width, shift, edgekeys, slider,
indication);
} }
public Key withShift(float s) public Key withShift(float s)
{ {
return new Key(key0, key1, key2, key3, key4, width, s, edgekeys, indication); return new Key(key0, key1, key2, key3, key4, width, s, edgekeys, slider,
indication);
} }
/** /**
@ -464,7 +472,7 @@ class KeyboardData
{ {
return new Key(apply_key0(k.key0), apply(k.key1), apply(k.key2), return new Key(apply_key0(k.key0), apply(k.key1), apply(k.key2),
apply(k.key3), apply(k.key4), k.width, k.shift, k.edgekeys, apply(k.key3), apply(k.key4), k.width, k.shift, k.edgekeys,
k.indication); k.slider, k.indication);
} }
protected Corner apply_key0(Corner c) protected Corner apply_key0(Corner c)

View File

@ -147,6 +147,10 @@ public final class Pointers implements Handler.Callback
public void onTouchDown(float x, float y, int pointerId, KeyboardData.Key key) public void onTouchDown(float x, float y, int pointerId, KeyboardData.Key key)
{ {
// Ignore new presses while a sliding key is active. On some devices, ghost
// touch events can happen while the pointer travels on top of other keys.
if (isSliding())
return;
// Don't take latched modifiers into account if an other key is pressed. // Don't take latched modifiers into account if an other key is pressed.
// The other key already "own" the latched modifiers and will clear them. // The other key already "own" the latched modifiers and will clear them.
Modifiers mods = getModifiers(isOtherPointerDown()); Modifiers mods = getModifiers(isOtherPointerDown());
@ -198,11 +202,16 @@ public final class Pointers implements Handler.Callback
// The position in a IME windows is clampled to view. // The position in a IME windows is clampled to view.
// For a better up swipe behaviour, set the y position to a negative value when clamped. // For a better up swipe behaviour, set the y position to a negative value when clamped.
if (y == 0.0) y = -400; if (y == 0.0) y = -400;
float dx = x - ptr.downX; float dx = x - ptr.downX;
float dy = y - ptr.downY; float dy = y - ptr.downY;
float dist = Math.abs(dx) + Math.abs(dy);
if (ptr.sliding)
{
onTouchMove_sliding(ptr, dx);
return;
}
float dist = Math.abs(dx) + Math.abs(dy);
int direction; int direction;
if (dist < _config.swipe_dist_px) if (dist < _config.swipe_dist_px)
{ {
@ -228,9 +237,21 @@ public final class Pointers implements Handler.Callback
KeyValue newValue = getKeyAtDirection(ptr, direction); KeyValue newValue = getKeyAtDirection(ptr, direction);
if (newValue != null && (ptr.value == null || !newValue.equals(ptr.value))) if (newValue != null && (ptr.value == null || !newValue.equals(ptr.value)))
{ {
int old_flags = ptr.flags;
ptr.value = newValue; ptr.value = newValue;
ptr.flags = newValue.getFlags(); ptr.flags = newValue.getFlags();
// Sliding mode is entered when key2 or key3 is down on a slider key.
if (ptr.key.slider)
{
switch (direction)
{
case 1:
case 8:
case 4:
case 5:
startSliding(ptr, dy);
break;
}
}
_handler.onPointerDown(true); _handler.onPointerDown(true);
} }
} }
@ -287,6 +308,14 @@ public final class Pointers implements Handler.Callback
_handler.onPointerFlagsChanged(shouldVibrate); _handler.onPointerFlagsChanged(shouldVibrate);
} }
boolean isSliding()
{
for (Pointer ptr : _ptrs)
if (ptr.sliding)
return true;
return false;
}
// Key repeat // Key repeat
/** Message from [_keyrepeat_handler]. */ /** Message from [_keyrepeat_handler]. */
@ -344,6 +373,30 @@ public final class Pointers implements Handler.Callback
return true; return true;
} }
// Sliding
void startSliding(Pointer ptr, float initial_dy)
{
stopKeyRepeat(ptr);
ptr.sliding = true;
ptr.sliding_count = (int)(initial_dy / _config.slide_step_px);
}
/** Handle move events for sliding pointers. [dx] is distance travelled from
[downX]. */
void onTouchMove_sliding(Pointer ptr, float dx)
{
int count = (int)(dx / _config.slide_step_px);
if (count == ptr.sliding_count)
return;
KeyValue newValue = handleKV(
(count < ptr.sliding_count) ? ptr.key.key3 : ptr.key.key2,
ptr.modifiers);
ptr.sliding_count = count;
ptr.value = newValue;
if (newValue != null)
_handler.onPointerHold(newValue, ptr.modifiers);
}
private static final class Pointer private static final class Pointer
{ {
@ -363,6 +416,10 @@ public final class Pointers implements Handler.Callback
public int flags; public int flags;
/** Identify timeout messages. */ /** Identify timeout messages. */
public int timeoutWhat; public int timeoutWhat;
/** Whether the pointer is "sliding" laterally on a key. */
public boolean sliding;
/** Number of event already caused by sliding. */
public int sliding_count;
public Pointer(int p, KeyboardData.Key k, KeyValue v, float x, float y, Modifiers m) public Pointer(int p, KeyboardData.Key k, KeyValue v, float x, float y, Modifiers m)
{ {
@ -375,6 +432,8 @@ public final class Pointers implements Handler.Callback
modifiers = m; modifiers = m;
flags = (v == null) ? 0 : v.getFlags(); flags = (v == null) ? 0 : v.getFlags();
timeoutWhat = -1; timeoutWhat = -1;
sliding = false;
sliding_count = 0;
} }
} }