Working physical keyboard mapping
This commit is contained in:
@@ -1,28 +1,77 @@
|
||||
package cfpi_fpsi.eu.titanboard;
|
||||
|
||||
import android.inputmethodservice.InputMethodService;
|
||||
import android.view.KeyCharacterMap;
|
||||
import android.util.Log;
|
||||
import android.view.KeyEvent;
|
||||
import android.view.View;
|
||||
import android.view.inputmethod.EditorInfo;
|
||||
import android.view.inputmethod.InputConnection;
|
||||
|
||||
import org.xmlpull.v1.XmlPullParser;
|
||||
import org.xmlpull.v1.XmlPullParserException;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.HashMap;
|
||||
|
||||
public class TitanboardIME extends InputMethodService {
|
||||
private HashMap<Integer, int[]> keymap;
|
||||
|
||||
@Override
|
||||
public void onCreate() {
|
||||
super.onCreate();
|
||||
|
||||
keymap = new HashMap<>();
|
||||
// We load the keymap from the XML file into a variable
|
||||
XmlPullParser parser = getResources().getXml(getResources()
|
||||
.getIdentifier("keymap_titan", "xml", getPackageName()));
|
||||
int code = 0;
|
||||
int value = 0;
|
||||
int alt = 0;
|
||||
try {
|
||||
while (parser.getEventType() != XmlPullParser.END_DOCUMENT) {
|
||||
switch (parser.getEventType()) {
|
||||
case XmlPullParser.START_TAG:
|
||||
if (parser.getName().equals("key")) {
|
||||
for (int i = 0; i < parser.getAttributeCount(); ++i) {
|
||||
switch (parser.getAttributeName(i)) {
|
||||
case "code":
|
||||
code = Integer.parseInt(parser.getAttributeValue(i));
|
||||
Log.d("STATE", "KeyCode: " + code);
|
||||
break;
|
||||
case "value":
|
||||
value = Integer.parseInt(String.valueOf(parser.getAttributeValue(i).codePointAt(0)));
|
||||
break;
|
||||
case "alt":
|
||||
alt = Integer.parseInt(String.valueOf(parser.getAttributeValue(i).codePointAt(0)));
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
case XmlPullParser.END_TAG:
|
||||
if (parser.getName().equals("key")) {
|
||||
Log.d("STATE", "Adding KeyCode " + code + " which corresponds to: " + value + " and " + alt);
|
||||
keymap.put(code, new int[] { value, alt });
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
parser.next();
|
||||
}
|
||||
} catch (XmlPullParserException e) {
|
||||
throw new RuntimeException(e);
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public View onCreateInputView() {
|
||||
return getLayoutInflater().inflate(R.layout.input, null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public View onCreateCandidatesView() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onKeyUp(int keyCode, KeyEvent event) {
|
||||
return super.onKeyUp(keyCode, event);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onKeyDown(int keyCode, KeyEvent event) {
|
||||
InputConnection conn = getCurrentInputConnection();
|
||||
@@ -47,12 +96,25 @@ public class TitanboardIME extends InputMethodService {
|
||||
case KeyEvent.KEYCODE_DEL:
|
||||
conn.deleteSurroundingText(1, 0);
|
||||
return true;
|
||||
default:
|
||||
// We get the current key mapping.
|
||||
KeyCharacterMap map = event.getKeyCharacterMap();
|
||||
// And we send the character to the app.
|
||||
conn.commitText(new String(Character.toChars(map.get(keyCode, event.getMetaState()))), 1);
|
||||
// We suppress the alt keys so the system cannot use them.
|
||||
case KeyEvent.KEYCODE_ALT_LEFT:
|
||||
case KeyEvent.KEYCODE_ALT_RIGHT:
|
||||
return true;
|
||||
default:
|
||||
// And we send the character to the app.
|
||||
int[] key = keymap.get(keyCode);
|
||||
if (key != null) {
|
||||
if (event.isAltPressed()) {
|
||||
conn.commitText(new String(Character.toChars(key[1])), 1);
|
||||
} else if (event.isShiftPressed()) {
|
||||
conn.commitText(new String(Character.toChars(key[0])).toUpperCase(), 1);
|
||||
} else {
|
||||
conn.commitText(new String(Character.toChars(key[0])), 1);
|
||||
}
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<keymap>
|
||||
<key code="29" value="a" alt="@"/>
|
||||
<key code="30" value="b" alt="7"/>
|
||||
<key code="31" value="c" alt=","/>
|
||||
<key code="32" value="d" alt="""/>
|
||||
<key code="33" value="e" alt="_"/>
|
||||
<key code="34" value="f" alt="+"/>
|
||||
<key code="35" value="g" alt="*"/>
|
||||
<key code="36" value="h" alt="#"/>
|
||||
<key code="37" value="i" alt="2"/>
|
||||
<key code="38" value="j" alt="4"/>
|
||||
<key code="39" value="k" alt="5"/>
|
||||
<key code="40" value="l" alt="6"/>
|
||||
<key code="41" value="m" alt="9"/>
|
||||
<key code="42" value="n" alt="8"/>
|
||||
<key code="43" value="o" alt="3"/>
|
||||
<key code="44" value="p" alt="0"/>
|
||||
<key code="45" value="q" alt=":"/>
|
||||
<key code="46" value="r" alt="-"/>
|
||||
<key code="47" value="s" alt="'"/>
|
||||
<key code="48" value="t" alt="("/>
|
||||
<key code="49" value="u" alt="1"/>
|
||||
<key code="50" value="v" alt="."/>
|
||||
<key code="51" value="w" alt="/"/>
|
||||
<key code="52" value="x" alt="?"/>
|
||||
<key code="53" value="y" alt=")"/>
|
||||
<key code="54" value="z" alt="!"/>
|
||||
</keymap>
|
||||
Reference in New Issue
Block a user