views:

35

answers:

1

Why doesn't this work?? I am trying to create an onClickListener for a button that produces the same effect as pressing the "down" key on the D-pad. Eclipse gives me an error, saying: "Cannot make a static reference to the non-static method sendDownUpKeyEvents(int) from the type InputMethodService" Help!

downButton.setOnClickListener(new View.OnClickListener() {

            public void onClick(View view) {

                InputMethodService.sendDownUpKeyEvents(0x00000014);
            }
+1  A: 

You're trying to invoke non-static method in a static way. You need to obtain an instance of the service first and then invoke the method on the instance. Also the way you're doing the simulation of keypress looks incorrect. UPD: After some digging I've managed to simulate key event, try:

new Thread(new Runnable() {         
    @Override
    public void run() {                 
        new Instrumentation().sendKeyDownUpSync(KeyEvent.KEYCODE_DPAD_DOWN);
    }   
}).start();
Konstantin Burov
please forgive my stupidity but i do not fully understand what that means... how should it be written??
Frank Bozzo
See the update, I'm not sure if that will work for you, but at least eclipse will not complain.
Konstantin Burov
Thank you for your quick reply.... using your code causes a fc when the button is pressed.logcat:E/AndroidRuntime( 487): Uncaught handler: thread main exiting due to uncaught exceptionE/AndroidRuntime( 487): java.lang.ClassCastException: android.view.inputmethod.InputMethodManagerE/AndroidRuntime( 487): at com.soapbox.servicerec.NoteEdit$2.onClick(NoteEdit.java:520)Any Ideas?
Frank Bozzo
Post more of your stacktrace in your question
Falmarri
Actually scratch that.. the system service is not a InputMethodService instance. Here is good tutorial on how to simulate key presses http://www.anddev.org/throwing-simulating_keystrokes_programatically-t717.html
Konstantin Burov
eclipse doesn't like some of the imports in that tutorial... i believe it is outdated.
Frank Bozzo
See the update, that code worked for me.
Konstantin Burov
Thank you so much!
Frank Bozzo