I have been trying to find some resources in order to build a Keylogger Android application for an accessibility research project on the Android platform (APILevel 17).
The Interface of the application would be a simple "EditText" field where the user types using the virtual onscreen keyboard [After selecting the required keyboard from the Input Settings].
I aim to create a Keylog database for my application (with an SQLite DB because I'm familiar with that, but a simple csv file DB would also work well! :) ) which looks like the following: (Illustration)
Hence I need to log each character on a new entry as soon as it is typed, along with the timestamp. I have been trying to expriment with the "TextWatcher" Class
EditText KeyLogEditText = (EditText) findViewById(R.id.editTextforKeyLog);
TextWatcher KeyLogTextWatcher = new TextWatcher() {
@Override
public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3)
{ }
@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,int arg3)
{ }
@Override
public void afterTextChanged(Editable arg0) {
// TODO Log the characters in the SQLite DB with a timeStamp etc.
// Here I call my database each time and insert an entry in the database table.
//I am yet to figure out how to find the latest-typed-character by user in the EditText
}
My Questions are:
*Thanks in Advance to anyone who can help me in any way!
Adit*
For now your TextWatcher is not binded to EditText
You should use addTextChangedListener(TextWatcher yourWatcher)
on your EditText.
Here is my example:
smsET.addTextChangedListener(new TextWatcher() {
public void onTextChanged(CharSequence s, int start, int before, int count) {
Log.d(TAG, "onTextChanged start :"+start +" end :"+count);}
public void beforeTextChanged(CharSequence s, int start, int count,int after) {
Log.d(TAG, "beforeTextChanged start :"+start +" after :"+after);
}
public void afterTextChanged(Editable s) {
int lastPosition = s.length()-1;
char lastChar = s.charAt(lastPosition);
Log.d(TAG, "afterTextChange last char"+lastChar );
}
});
In your code it should be like this :
KeyLogEditText.addTextChangeListener(KeyLogTextWatcher );
Each of method included in this Watcher is triggerd by entering every single sign from keyboard. Since you get posistion after input you can easly get character that was entered
To store data you mentioned, SharedPreferences will be faster than DB. (Many writes to DB) If your target is at least api 11 you can simply use StringSet Editor.putStringSet if your target is lower it is also possible, some example : http://androidcodemonkey.blogspot.com/2011/07/store-and-get-object-in-android-shared.html
.