0

I have code (which can be seen below) which does not function correctly. I have tried literally every solution already presented on SO and i cannot find anything that will work for my problem.

The aim of my application is to get the Key Code (either numerical or represented as the label/"KEYCODE_A") and output this to a txt file. Regardless of the txt file aspect, i cannot even get the keycode to output to the log.

The only four keycodes that come from the stock android keyboard are:

KEYCODE_SHIFT_LEFT = 59

KEYCODE_SHIFT_RIGHT = 60

KEYCODE_ENTER = 66

KEYCODE_DEL = 67

Please can someone help me to be able to get the characters on the keyboard represented as a numerical value or otherwise to output to either the log, a field, or a text file.

I have tried numerous solutions such as

char unicodeChar = (char)event.getUnicodeChar();

try editText.setOnEditorActionListener

and they do not work.

Thank you in advance for your help!!!!!!!

public class MainActivity extends AppCompatActivity{

    EditText et_name, et_content;
    Button b_save;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE)
            != PackageManager.PERMISSION_GRANTED) {
            requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1000);
        }
        et_name = (EditText) findViewById(R.id.et_name);
        et_content = (EditText) findViewById(R.id.et_content);
        final TextView view = (TextView) findViewById(R.id.view);
        b_save = (Button) findViewById(R.id.b_save);


        et_content.setOnKeyListener(new View.OnKeyListener(){
            public boolean onKey(View v, int keyCode, KeyEvent event) {
              //  String keyCodeStr = KeyEvent.keyCodeToString(keyCode);
                //view.setText(String.valueOf(keyCodeStr));
                char unicodeChar = (char) event.getUnicodeChar();
                Log.e("Key", "Code "+keyCode + " " + unicodeChar);
                et_content.getText().append(unicodeChar);


                return true;
            }
        });

        b_save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
              String filename = et_name.getText().toString();
              String content = et_content.getText().toString();

              saveTextAsFile(filename, content);
            }
        });
    }

    private void saveTextAsFile (String filename, String content){
        String fileName = filename + ".txt";

        //create file
        File file = new File (Environment.getExternalStorageDirectory().getAbsolutePath(), fileName);

        //write to file
        try {
            FileOutputStream fos = new FileOutputStream(file);
            fos.write(content.getBytes());
            fos.close();
            Toast.makeText(this, "Saved", Toast.LENGTH_SHORT).show();
        } catch (FileNotFoundException e){
            e.printStackTrace();
            Toast.makeText(this, "File not found", Toast.LENGTH_SHORT).show();
        } catch (IOException e){
            e.printStackTrace();
            Toast.makeText(this, "Error saving", Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        switch (requestCode){
            case 1000:
                if (grantResults[0] == PackageManager.PERMISSION_GRANTED){
                    Toast.makeText(this, "Permission Granted", Toast.LENGTH_SHORT).show();
                } else{
                    Toast.makeText(this, "Permission Not Granted", Toast.LENGTH_SHORT).show();
                    finish();
                }
        }
    }

}

Thank you for your help!

Any more info needed please let me know, but i'm very desperate to get this fixed!

mohammadReza Abiri
  • 1,759
  • 1
  • 9
  • 20
PacMan3000
  • 11
  • 5

1 Answers1

0

Soft keyboards rarely if ever send key codes. They tend to send text directly via commitText() calls on the input connection. They just don't work on the old "send hardware key event" model you want them to, there's no way to do what you're trying.

You can try creating your own custom input connection and working on that level. But my guess is you'll have a lot of issues dealing with the predictive capabilities of the keyboard and composing text (a concept where part of the text is temporary and is overwritten by the next send of any text). And the fact that it may send whole words at a time rather than letters/keystrokes

Gabe Sechan
  • 90,003
  • 9
  • 87
  • 127
  • Thank you for your honest response @Gabe. Any advice on the custom input connection? I’ve created a custom keyboard with no prediction capability so that should help slightly. – PacMan3000 May 08 '19 at 23:15
  • Well if you're willing to use it with just your own custom keyboard then you can make that keyboard send key events- its possible, its just not how most of them work. Using inputConnection.sendKeyEvent() instead of commitText and related calls will do that. If you want to do it with any keyboard- you'd need to have a view focused on screen that returns your custom input connection in getInputConnection, and it would need to translate commitText and other calls into the events you want. – Gabe Sechan May 08 '19 at 23:19
  • see this [example](https://stackoverflow.com/a/54499279/549372) for how to emit key-codes. – Martin Zeitler May 08 '19 at 23:23
  • @GabeSechan thank you for this, i am only using a custom keyboard. Is there any help you can give me with that? I did think around using the text that has been entered from the field and then somehow getting keyup keydown for each letter and appending them to a text file? any thoughts on if this would work? – PacMan3000 May 09 '19 at 07:27