Created
June 20, 2015 21:53
-
-
Save lionaneesh/e5eda7fe7522e490f690 to your computer and use it in GitHub Desktop.
Shhh!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import time | |
import uinput | |
import socket | |
import sys | |
from thread import * | |
HOST = '' # Symbolic name, meaning all available interfaces | |
PORT = 9123 # Arbitrary non-privileged port | |
events = ( | |
uinput.REL_X, | |
uinput.REL_Y, | |
uinput.BTN_LEFT, | |
uinput.BTN_RIGHT, | |
uinput.KEY_W, | |
uinput.KEY_A, | |
uinput.KEY_S, | |
uinput.KEY_D, | |
uinput.KEY_J, | |
uinput.KEY_K, | |
uinput.KEY_H, | |
uinput.KEY_F | |
) | |
#Function for handling connections. This will be used to create threads | |
def clientthread(conn): | |
#Sending message to connected client | |
conn.send('Welcome to the server. Type something and hit enter\n') #send only takes string | |
device = uinput.Device(events) | |
keyspressed = [] | |
keysMapper = { | |
"UpLeft": [uinput.KEY_A, uinput.KEY_W], | |
"UpRight": [uinput.KEY_D, uinput.KEY_W], | |
"DownLeft": [uinput.KEY_S, uinput.KEY_A], | |
"DownRight": [uinput.KEY_S, uinput.KEY_D], | |
"Up": [uinput.KEY_W], | |
"Right": [uinput.KEY_D], | |
"Down": [uinput.KEY_S], | |
"Left": [uinput.KEY_A] | |
} | |
#infinite loop so that function do not terminate and thread do not end. | |
while True: | |
#Receiving from client | |
data = conn.recv(1024) | |
#time.sleep(5) | |
print data | |
if len(keyspressed) > 0: | |
for key in keyspressed: | |
device.emit(key, 0) | |
if data in keysMapper: | |
for key in keysMapper[data]: | |
device.emit(key, 1) | |
keyspressed.append(key) | |
#came out of loop | |
conn.close() | |
def main(): | |
# for i in range(20): | |
# # syn=False to emit an "atomic" (5, 5) event. | |
# device.emit(uinput.REL_X, 5, syn=False) | |
# device.emit(uinput.REL_Y, 5) | |
# # Just for demonstration purposes: shows the motion. In real | |
# # application, this is of course unnecessary. | |
# time.sleep(0.01) | |
# time.sleep(2) | |
# device.emit_click(uinput.KEY_H) | |
# device.emit_click(uinput.KEY_E) | |
# device.emit_click(uinput.KEY_L) | |
# device.emit_click(uinput.KEY_L) | |
# device.emit_click(uinput.KEY_O) | |
# device.emit(uinput.KEY_D,1) | |
# time.sleep(5) | |
# device.emit(uinput.KEY_D,0) | |
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
print 'Socket created' | |
#Bind socket to local host and port | |
try: | |
s.bind((HOST, PORT)) | |
except socket.error as msg: | |
print 'Bind failed. Error Code : ' + str(msg[0]) + ' Message ' + msg[1] | |
sys.exit() | |
print 'Socket bind complete' | |
#Start listening on socket | |
s.listen(10) | |
print 'Socket now listening' | |
#now keep talking with the client | |
while 1: | |
#wait to accept a connection - blocking call | |
conn, addr = s.accept() | |
print 'Connected with ' + addr[0] + ':' + str(addr[1]) | |
start_new_thread(clientthread ,(conn,)) | |
s.close() | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment