2021-12-28 17:47:18 +01:00
|
|
|
package juloo.keyboard2;
|
|
|
|
|
|
|
|
import android.view.KeyEvent;
|
|
|
|
|
|
|
|
class KeyEventHandler implements Config.IKeyEventHandler
|
|
|
|
{
|
|
|
|
private IReceiver _recv;
|
|
|
|
|
|
|
|
public KeyEventHandler(IReceiver recv)
|
|
|
|
{
|
|
|
|
_recv = recv;
|
|
|
|
}
|
|
|
|
|
2022-06-05 01:38:42 +02:00
|
|
|
public void handleKeyUp(KeyValue key, Pointers.Modifiers mods)
|
2021-12-28 17:47:18 +01:00
|
|
|
{
|
2022-06-05 17:26:34 +02:00
|
|
|
if (key == null)
|
2022-03-13 00:35:15 +01:00
|
|
|
return;
|
2022-06-05 17:26:34 +02:00
|
|
|
switch (key.getKind())
|
2021-12-28 17:47:18 +01:00
|
|
|
{
|
2022-06-05 17:26:34 +02:00
|
|
|
case Char:
|
|
|
|
_recv.commitChar(key.getChar());
|
|
|
|
break;
|
|
|
|
case String:
|
|
|
|
_recv.commitText(key.getString());
|
|
|
|
break;
|
|
|
|
case Event:
|
2022-06-05 19:30:53 +02:00
|
|
|
switch (key.getEvent())
|
2022-06-05 17:26:34 +02:00
|
|
|
{
|
2022-06-05 19:30:53 +02:00
|
|
|
case CONFIG: _recv.showKeyboardConfig(); break;
|
|
|
|
case SWITCH_TEXT: _recv.switchMain(); break;
|
|
|
|
case SWITCH_NUMERIC: _recv.switchNumeric(); break;
|
|
|
|
case SWITCH_EMOJI: _recv.setPane_emoji(); break;
|
|
|
|
case SWITCH_BACK_EMOJI: _recv.setPane_normal(); break;
|
|
|
|
case CHANGE_METHOD: _recv.switchToNextInputMethod(); break;
|
|
|
|
case ACTION: _recv.performAction(); break;
|
|
|
|
case SWITCH_PROGRAMMING: _recv.switchProgramming(); break;
|
2022-06-05 17:26:34 +02:00
|
|
|
}
|
|
|
|
break;
|
2022-06-05 19:30:53 +02:00
|
|
|
case Keyevent:
|
|
|
|
handleKeyUpWithModifier(key.getKeyevent(), mods);
|
|
|
|
break;
|
2022-06-05 17:26:34 +02:00
|
|
|
case Modifier:
|
|
|
|
break;
|
2021-12-28 17:47:18 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// private void handleDelKey(int before, int after)
|
|
|
|
// {
|
|
|
|
// CharSequence selection = getCurrentInputConnection().getSelectedText(0);
|
|
|
|
|
|
|
|
// if (selection != null && selection.length() > 0)
|
|
|
|
// getCurrentInputConnection().commitText("", 1);
|
|
|
|
// else
|
|
|
|
// getCurrentInputConnection().deleteSurroundingText(before, after);
|
|
|
|
// }
|
|
|
|
|
2022-02-22 19:32:16 +01:00
|
|
|
private int sendMetaKey(int eventCode, int metaFlags, int metaState, boolean down)
|
2021-12-28 17:47:18 +01:00
|
|
|
{
|
2022-02-22 19:32:16 +01:00
|
|
|
int action;
|
|
|
|
int updatedMetaState;
|
|
|
|
if (down) { action = KeyEvent.ACTION_DOWN; updatedMetaState = metaState | metaFlags; }
|
|
|
|
else { action = KeyEvent.ACTION_UP; updatedMetaState = metaState & ~metaFlags; }
|
|
|
|
_recv.sendKeyEvent(action, eventCode, metaState);
|
|
|
|
return updatedMetaState;
|
|
|
|
}
|
|
|
|
|
2022-06-05 19:30:53 +02:00
|
|
|
private int sendMetaKeyForModifier(KeyValue.Modifier mod, int metaState, boolean down)
|
2022-02-22 19:32:16 +01:00
|
|
|
{
|
2022-06-05 01:38:42 +02:00
|
|
|
switch (mod)
|
|
|
|
{
|
2022-06-05 19:30:53 +02:00
|
|
|
case CTRL:
|
2022-06-05 01:38:42 +02:00
|
|
|
return sendMetaKey(KeyEvent.KEYCODE_CTRL_LEFT, KeyEvent.META_CTRL_LEFT_ON | KeyEvent.META_CTRL_ON, metaState, down);
|
2022-06-05 19:30:53 +02:00
|
|
|
case ALT:
|
2022-06-05 01:38:42 +02:00
|
|
|
return sendMetaKey(KeyEvent.KEYCODE_ALT_LEFT, KeyEvent.META_ALT_LEFT_ON | KeyEvent.META_ALT_ON, metaState, down);
|
2022-06-05 19:30:53 +02:00
|
|
|
case SHIFT:
|
2022-06-05 01:38:42 +02:00
|
|
|
return sendMetaKey(KeyEvent.KEYCODE_SHIFT_LEFT, KeyEvent.META_SHIFT_LEFT_ON | KeyEvent.META_SHIFT_ON, metaState, down);
|
2022-06-05 19:30:53 +02:00
|
|
|
case META:
|
2022-06-05 01:38:42 +02:00
|
|
|
return sendMetaKey(KeyEvent.KEYCODE_META_LEFT, KeyEvent.META_META_LEFT_ON | KeyEvent.META_META_ON, metaState, down);
|
|
|
|
default: return metaState;
|
|
|
|
}
|
2021-12-28 17:47:18 +01:00
|
|
|
}
|
|
|
|
|
2022-02-22 19:32:16 +01:00
|
|
|
/*
|
|
|
|
* Don't set KeyEvent.FLAG_SOFT_KEYBOARD.
|
|
|
|
*/
|
2022-06-05 17:26:34 +02:00
|
|
|
private void handleKeyUpWithModifier(int keyCode, Pointers.Modifiers mods)
|
2022-02-22 19:32:16 +01:00
|
|
|
{
|
2022-06-05 01:38:42 +02:00
|
|
|
int metaState = 0;
|
|
|
|
for (int i = 0; i < mods.size(); i++)
|
|
|
|
metaState = sendMetaKeyForModifier(mods.get(i), metaState, true);
|
2022-06-05 17:26:34 +02:00
|
|
|
_recv.sendKeyEvent(KeyEvent.ACTION_DOWN, keyCode, metaState);
|
|
|
|
_recv.sendKeyEvent(KeyEvent.ACTION_UP, keyCode, metaState);
|
2022-06-05 01:38:42 +02:00
|
|
|
for (int i = mods.size() - 1; i >= 0; i--)
|
|
|
|
metaState = sendMetaKeyForModifier(mods.get(i), metaState, false);
|
2022-02-22 19:32:16 +01:00
|
|
|
}
|
|
|
|
|
2021-12-28 17:47:18 +01:00
|
|
|
public static interface IReceiver
|
|
|
|
{
|
|
|
|
public void switchToNextInputMethod();
|
|
|
|
public void setPane_emoji();
|
|
|
|
public void setPane_normal();
|
|
|
|
public void showKeyboardConfig();
|
2022-01-09 20:26:06 +01:00
|
|
|
public void performAction();
|
2021-12-28 17:47:18 +01:00
|
|
|
|
2022-04-03 17:15:12 +02:00
|
|
|
public void switchMain();
|
|
|
|
public void switchNumeric();
|
|
|
|
public void switchProgramming();
|
2021-12-28 17:47:18 +01:00
|
|
|
|
2022-02-22 19:32:16 +01:00
|
|
|
public void sendKeyEvent(int eventAction, int eventCode, int meta);
|
2021-12-28 17:47:18 +01:00
|
|
|
|
|
|
|
public void commitText(String text);
|
|
|
|
public void commitChar(char c);
|
|
|
|
}
|
|
|
|
}
|