Skip to content

Instantly share code, notes, and snippets.

Created August 21, 2013 12:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/6293894 to your computer and use it in GitHub Desktop.
Save anonymous/6293894 to your computer and use it in GitHub Desktop.
HelloInputStick
package com.example.helloinputstick;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import com.example.inputsticklibrary.usb.USBDevice;
import com.example.inputsticklibrary.usb.USBDeviceListener;
import com.example.inputsticklibrary.usb.devices.USBKeyboard;
//USBDeviceListener interface allows to receive callbacks
public class HelloInputStick extends Activity implements USBDeviceListener {
//create new instance of USBKeyboard
//if your app consists of more than one Activity
//it might be a good idea to use some sort of Singleton class to share USBKeyboard
private static final USBKeyboard keyb = new USBKeyboard();
private Button buttonConnect;
private Button buttonType;
//called when data is received form USB host
@Override
public void onUSBData(int endpoint, byte[] data) {
// TODO Auto-generated method stub
//USBKeyboard already takes care of handling NumLock/CapsLock/ScrollLock state
}
//Called when connection state has changed, update UI
public void onUSBEvent(int code) {
manageUI();
}
//Set appropriate caption for buttonConnect, depending on current connection state
//Enable buttonType only if InputStick has been successfully enumerated
private void manageUI() {
int state = keyb.getState();
switch (state) {
case USBDevice.STATE_NONE:
buttonConnect.setText(getString(R.string.string_connect));
buttonConnect.setEnabled(true);
enableUI(false);
break;
case USBDevice.STATE_CONNECTING:
buttonConnect.setText(getString(R.string.string_connecting));
buttonConnect.setEnabled(false);
enableUI(false);
break;
case USBDevice.STATE_CONNECTED:
buttonConnect.setText(getString(R.string.string_disconnect));
buttonConnect.setEnabled(true);
enableUI(false);
break;
case USBDevice.STATE_READY:
buttonConnect.setText(getString(R.string.string_disconnect));
buttonConnect.setEnabled(true);
enableUI(true);
break;
}
}
private void enableUI(boolean enabled) {
buttonType.setEnabled(enabled);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_hello_input_stick);
//Pass application context!
//It will allow to bind to InputStickService (part of DeviceManager app)
keyb.setContext(this.getApplicationContext());
buttonConnect = (Button) findViewById(R.id.buttonConnect);
buttonConnect.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
//Action depends on current connection state
switch (keyb.getState()) {
case USBDevice.STATE_NONE:
keyb.connect();
break;
case USBDevice.STATE_CONNECTED:
case USBDevice.STATE_READY:
keyb.disconnect();
break;
}
}
});
buttonType = (Button) findViewById(R.id.buttonType);
//choose one!
buttonType.setOnClickListener(new MyOnClickListener());
//buttonType.setOnClickListener(new MyOnClickListenerCaps());
//buttonType.setOnClickListener(new MyOnClickListenerLayout());
}
@Override
protected void onResume() {
super.onResume();
//register as a listener for callbacks
keyb.registerListener(this);
//Update UI depending on current connection state
manageUI();
}
@Override
protected void onPause() {
super.onPause();
//Activity is paused, unregister listener
keyb.unregisterListener();
}
//1) Simple case: ignore CapsLock and assume keyboard layout used in OS is set to EN-US or compatible
private class MyOnClickListener implements OnClickListener {
@Override
public void onClick(View arg0) {
//SHIFT key + H key = 'H'
keyb.pressAndRelease(USBKeyboard.SHIFT_LEFT, USBKeyboard.KEY_H);
//E key = 'e'
keyb.pressAndRelease(USBKeyboard.NONE, USBKeyboard.KEY_E);
//'llo world!', USBKeyboard class takes care of adding SHIFT key when necessary
keyb.type("llo world!");
//press ENTER key
keyb.pressAndRelease(USBKeyboard.NONE, USBKeyboard.KEY_ENTER);
//type 'XyZ'
keyb.type("XyZ");
}
}
//2) Take state of CapsLock into account
private class MyOnClickListenerCaps implements OnClickListener {
@Override
public void onClick(View arg0) {
if (keyb.isCapsLockOn()) {
//CapsLock is on! Change case of keys affected by CapsLock
keyb.type("hELLO WORLD!");
keyb.pressAndRelease(USBKeyboard.NONE, USBKeyboard.KEY_ENTER);
keyb.type("xYz");
} else {
keyb.pressAndRelease(USBKeyboard.SHIFT_LEFT, USBKeyboard.KEY_H);
keyb.pressAndRelease(USBKeyboard.NONE, USBKeyboard.KEY_E);
keyb.type("llo world!");
keyb.pressAndRelease(USBKeyboard.NONE, USBKeyboard.KEY_ENTER);
keyb.type("XyZ");
}
}
}
//3) Assume that USB host uses different keyboard layout: qwertz instead of qwerty
private class MyOnClickListenerLayout implements OnClickListener {
@Override
public void onClick(View arg0) {
if (keyb.isCapsLockOn()) {
//CapsLock is on! Change case of keys affected by CapsLock
keyb.type("hELLO WORLD!");
keyb.pressAndRelease(USBKeyboard.NONE, USBKeyboard.KEY_ENTER);
//replace 'z' and 'y' characters to produce expected result
keyb.type("xZy");
} else {
keyb.pressAndRelease(USBKeyboard.SHIFT_LEFT, USBKeyboard.KEY_H);
keyb.pressAndRelease(USBKeyboard.NONE, USBKeyboard.KEY_E);
keyb.type("llo world!");
keyb.pressAndRelease(USBKeyboard.NONE, USBKeyboard.KEY_ENTER);
//replace 'z' and 'y' characters to produce expected result
keyb.type("XzY");
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment