Here's the activity XML layout
- Code: Select all
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="last Key"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/lastKeyPressed"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="-" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Last Long Key"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/longKeyPressed"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="-" />
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Counter"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/counter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0" />
</LinearLayout>
and here's the code for the Activity:
- Code: Select all
package com.nofatclips.keyevent;
import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.widget.*;
public class KeyEventsDumpActivity extends Activity {
int counter = 0;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.key_event);
}
public boolean onKeyDown(int keyCode, KeyEvent event) {
counter++;
((TextView)findViewById(R.id.counter)).setText(""+counter);
((TextView)findViewById(R.id.lastKeyPressed)).setText(""+keyCode);
event.startTracking();
return true; //super.onKeyDown (keyCode, event);
}
public boolean onKeyLongPress(int keyCode, KeyEvent event) {
counter--;
((TextView)findViewById(R.id.counter)).setText(""+counter);
((TextView)findViewById(R.id.longKeyPressed)).setText(""+keyCode);
return super.onKeyLongPress (keyCode, event);
}
}
And finally, here's one of my many fruitless attempt at generating the correct timed sequence of events for the long pression:
- Code: Select all
package com.nofatclips.keyevent.test;
import com.nofatclips.keyevent.KeyEventsDumpActivity;
import android.os.SystemClock;
import android.test.ActivityInstrumentationTestCase2;
import android.util.Log;
import android.view.KeyEvent;
public class KeyEventTest extends ActivityInstrumentationTestCase2<KeyEventsDumpActivity> {
private KeyEventsDumpActivity mActivity; // the activity under test
public KeyEventTest() {
super("com.nofatclips.keyevent", KeyEventsDumpActivity.class);
}
@Override
protected void setUp() throws Exception {
super.setUp();
mActivity = this.getActivity();
}
public void testLongPress() {
sendKeyDownUpLong(KeyEvent.KEYCODE_MENU);
}
public void sendKeyDownUpLong(final int key) {
final KeyEvent downEvent = new KeyEvent (KeyEvent.ACTION_DOWN, key);
sendKeyDownUp(downEvent);
sleep(500);
for (int repetition = 0; repetition<50; repetition++) {
KeyEvent newEvent = KeyEvent.changeTimeRepeat(downEvent, SystemClock.uptimeMillis(), repetition, downEvent.getFlags() | KeyEvent.FLAG_LONG_PRESS);
sendKeyDownUp(newEvent);
sleep(5);
}
final KeyEvent upEvent = new KeyEvent (KeyEvent.ACTION_UP, key);
sendKeyDownUp (upEvent);
}
public void sendKeyDownUp(KeyEvent key) {
getInstrumentation().sendKeySync(key);
getInstrumentation().waitForIdleSync();
}
public void sleep (int msec) {
try {
Thread.sleep(msec);
} catch (InterruptedException e) {
Log.e("nofatclips", "Could not sleep. Mosquito alert!", e);
return;
}
}
}
Thanks in advance for any constructive idea.
DeK


