First test for sending mouse controls over a network
mouse controls are captured using the win32api module
and sent over udp socket thru the network using IP 192.168.2.100 a commmon
network ip gateway.

Client code as stated below

import win32api
import socket
import time
import json

UDPSock = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
# Simply set up a target address and port ...
addr = ("192.168.2.100",50002)

def formatdata(mevent,curx,cury):
    #s = "mouse event {0}, x {1}, y{2}".format(mevent,curx,cury)
    s = json.dumps({mevent: (curx,cury)})
    UDPSock.sendto(s,addr)


old = (0, 0)
while True:
      coord = win32api.GetCursorPos()
      x = coord[0]
      y = coord[1]

      if(x,y) != old:
               #print "do something x{0} , y{1}".format(x,y)
               formatdata("move",x,y)
               old = (x,y)
      else:
               old

      #works
      a=win32api.GetAsyncKeyState(0X01)
      b=win32api.GetAsyncKeyState(0x02)
      if (a == -32768):
           formatdata("lclk",x,y)
      if (b == -32768):
         formatdata("rclk",x,y)

SERVER CODE

import win32api,win32con
import time
import sys
import json
import socket

UDPSock = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)

listen_addr = ("192.168.2.100",50002)
UDPSock.bind(listen_addr)

while True:
      data = UDPSock.recvfrom(1024)
      s = data[0]
      y  = json.loads(s)


      for a,c in y.iteritems():
          event = "{0}".format(a)
          x,y = c[0],c[1]
          if event=="move":
             win32api.SetCursorPos(( int(round(x)),int(round(y))))
          if event=="lclk":
             win32api.SetCursorPos(( int(round(x)),int(round(y))))
             win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN,x,y,0,0)
             win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP,x,y,0,0)
          if event=="rclk":
             win32api.SetCursorPos(( int(round(x)),int(round(y))))
             win32api.mouse_event(win32con.MOUSEEVENTF_RIGHTDOWN,x,y,0,0)
             win32api.mouse_event(win32con.MOUSEEVENTF_RIGHTUP,x,y,0,0)
          #print event,x,y

      if data[0]=="exit":

         print "server shutdown"
         sys.exit()

issues

1) lack of authorization, client and server just assuming they are the only ones to be talking to each other, there should be a handshaking routines.
2) client may have different resolution from server, this mean mouse movements on the server side will be restricted to clients monitor resolution
3) mouse buttons do not work correctly

Advertisements