0

I'm trying to map a button in my fragment layout as a keypress on a keyboard. This is my button press:

Button down = (Button)(myView.findViewById(R.id.btnDOWN));

    down.setOnTouchListener(new View.OnTouchListener(){
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            Log.i(TAG, "Down pressed");
            return false;
        }
    });

I wasn't able to find any library which would allow me to do so, all I've found was reverse case, when a keyboard or controller press would be sent to device.

What I'm trying to generally do here, is an app that acts like a controller for RetroPie by using the phone as a bluetooth keyboard, with only the keys necessary for a specific controller.

1 Answers1

0

The method you're using is not correct. you should use the setOnClickListener:

Here an example from Android official guide:

 public class MyActivity extends Activity {
     protected void onCreate(Bundle icicle) {
         super.onCreate(icicle);

         setContentView(R.layout.content_layout_id);

         final Button button = findViewById(R.id.button_id);
         button.setOnClickListener(new View.OnClickListener() {
             public void onClick(View v) {
                 // Perform action on click
             }
         });
     }
 }

If you're looking for continuos aka long click, you should change it to setOnLongClickListener:

down.setOnLongClickListener(new OnLongClickListener() { 
        @Override
        public boolean onLongClick(View v) {
            // TODO Auto-generated method stub
            return true;
        }
    });

It wasn't hard to find in this case the solution on this site

Thecave3
  • 762
  • 12
  • 27
  • However, onClick will only register the initial click, making it singular. Shouldn't I use onTouch to get continuous input, then? I don't want to click down everytime I want to move down, I want to hold it instead. – NoobProgrammerWannabe May 31 '17 at 12:04
  • So do you want a listener to a "long press of the button" action? Please help me to understand better what you're looking for – Thecave3 May 31 '17 at 12:07
  • The button function is not an issue here, having a controller I'll have both singular press as well as long press.(You can correct and lead me to the proper implementation of long/continuous press, though). The main problem is having the button press register as keyboard press, so that on RetroPie I can receive it as normal keyboard 'A' input, for example. – NoobProgrammerWannabe May 31 '17 at 12:16
  • If you set both the listeners that I 've shown to you, you'll be able to discriminate the case of a short singular press and the long press. This is what Android permits to you. Maybe you should ask another question more specific about RetroPie (never used, so I can not help you) – Thecave3 May 31 '17 at 12:20
  • The question is not about RetroPie, per se, but about Java libraries that allow me to do the key mapping needed. Thank you for your help with the long press. – NoobProgrammerWannabe May 31 '17 at 12:24
  • If you think that the answer is correct please mark it as correct, else I'm glad to help you (at least with Java), but you need to be more specific about what exactly you want – Thecave3 May 31 '17 at 12:26