java - Implementing Virtual Keyboard Android to PC -
i'm attempting use android phone keyboard pc. done on local wifi connection using sockets create client/server connection.
i'm using textwatcher pick change in characters in edittext , sending on server uses robot class write onto pc. using softkeyboard on android phone. issue when writing in edittext 1 character gets sent. , picked server.
e.g. type on android phone "a" , respective keypress simulated on server. if type character not read character if delete "a" , type character send character.
below client side of code, android application.
public class mainactivity extends appcompatactivity implements view.onkeylistener { private final static int portnumber = ****; private final static string hostname = "192.168.0.8"; private final static string ctest = "connection test"; socket socket = null; private static string tag = "test"; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); new thread() { @override public void run() { try { log.i(ctest, "attempting connect"); socket = new socket(hostname, portnumber); log.i(ctest, "connected"); runonuithread(new runnable() { public void run() { toast.maketext(getbasecontext(), "connected successfully", toast.length_short).show(); } }); edittext edittext = (edittext)findviewbyid(r.id.edittext); edittext.addtextchangedlistener(edittextwatcher); } catch (unknownhostexception e) { // todo auto-generated catch block e.printstacktrace(); } catch (ioexception e) { // todo auto-generated catch block log.e(ctest, e.getmessage()); } } }.start(); } private textwatcher edittextwatcher = new textwatcher() { private void sendtexttoserver(string send) { bufferedwriter bw = null; string textchange = send; try { bw = new bufferedwriter(new outputstreamwriter(socket.getoutputstream())); bw.write(textchange); bw.newline(); bw.flush(); } catch (ioexception e) { } } public void aftertextchanged(editable s) { } public void beforetextchanged(charsequence s, int start, int count, int after) { } public void ontextchanged(charsequence s, int start, int before, int count) { log.d(tag, s + " character read"); string test = s.tostring(); this.sendtexttoserver(test); } }; @override public boolean onkey(view v, int keycode, keyevent event) { return false; } }
the following code server side code being done in eclipse.
public class listenservermain { private static boolean ifconnected = true; private final static int portnumber = ****; public static string readin; private static socket client = null; private static serversocket server = null; private static bufferedreader in = null; private static robot robot = null; public static void main(string[] args) { // todo auto-generated method stub try { robot = new robot(); server = new serversocket(portnumber); system.out.println("listening on port :****"); client = server.accept(); //loops until client server connection established system.out.println("client connected"); //receive message client bufferedreader in = new bufferedreader( new inputstreamreader(client.getinputstream())); while(ifconnected) { readin = in.readline(); //reads input received if(readin.equalsignorecase("a")) { robot.keypress(keyevent.vk_a); robot.keyrelease(keyevent.vk_a); } if(readin.equalsignorecase("b")) { robot.keypress(keyevent.vk_b); robot.keyrelease(keyevent.vk_b); } if(readin.equalsignorecase("c")) { robot.keypress(keyevent.vk_c); robot.keyrelease(keyevent.vk_c); } if(readin.equalsignorecase("bs")) { robot.keypress(keyevent.vk_delete); robot.keyrelease(keyevent.vk_delete); } } system.out.println("unable read user input"); //if can't read input client system.exit(-1); //system exits system.out.println("server ended"); system.exit(-1); } catch (ioexception e) { system.out.println("can't open socket"); system.exit(-1); } catch (awtexception e) { system.out.println("unable create robot"); system.exit(-1); } } }
is there way in can read , send each character entered in edittext field?
if further clarification necessary let me know.
thanks
Comments
Post a Comment