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!