1

Is it possible to convert a string that was created by Object.ToString() back to the original object i.e:

            public boolean dispatchKeyEvent (KeyEvent event) {     
                appendLog(event.toString());
                return super.dispatchKeyEvent(event);      
            }

appent log will log every keyevent as a string in a new line. After logging i would like to run the logged keyevents

Is it possible to convert the string logged keyevents to a KeyEvent object?

0

6 Answers 6

3

Only if the toString() contains all the necessary event information. If it's just a hash code, of course not. If the relevant info is available, you'd need to parse it, and use one of the KeyEvent constructors, and hope there's nothing internally that isn't reflected in the toString. Seems sketchy to me.

If you need to store it, it's Parcelable, that might help.

You still have the event in the snippet you show, without an actual usecase it's hard to know what you're trying to do.

Sign up to request clarification or add additional context in comments.

3 Comments

i added the function, im logging the keyevents in a text file then i would like to read the keyevents from the text file and "reply" them
@MikeG Why? are you trying to build a robot? Those already exist.
Im trying to build recorder that records user events like clicks, movement, keystrokes etc... Im not sure what a robot is, if what im trying to do already exists can you provide some links. Thanks!
3

In general, no. The String won't contain enough information to reconstruct the original object. Of course, if you do encode enough information in the String returned by toString(), you may reconstruct the object - for instance, you can serialize an object to an XML - a String, and from that XML rebuild the object that originated it. Take a look at JAXB.

Comments

2

Yes, its possible but probably not the way you're thinking. In order to do this all the data related to KeyEvent would have to converted to text form, then parsed and a new KeyEvent created. An additional problem is KeyEvent lacks setters to recreate the internal state so to complete the return conversion you must create a sub class or find a way to use Parcelable to convert the parsed string.

Comments

2

If all you want is to replay keyboard and mouse events then you should look at:

http://docs.oracle.com/javase/6/docs/api/java/awt/Robot.html

You will need to listen for all events and record all of the needed information, but everything you need should be in the events.

Comments

1

event.toString() returns a string representation of the event object (actually will return a string related to the reference).

You can not use a cast to recreate the KeyEvent object.

Comments

0

I doubt toString() contains all info on the object. You need serialization mechanism to recover the object. In Android, I would use JSONObject.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.