-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpcc.py
53 lines (44 loc) · 1.84 KB
/
pcc.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# Listens on http://192.168.1.104:8800/{hotkey} and when the url is requested, passes the
# requested hotkey to pressKey to press hotkey using pynput module
import socketserver
import socket
import subprocess
from http.server import BaseHTTPRequestHandler
from pynput.keyboard import Key, Controller
import platform
ip = '192.168.1.104'
port = 8800
# A list of hotkeys and their equal for pynput as dictionary
hotkeys_list = {'volume-up' : Key.media_volume_up, 'volume-down' : Key.media_volume_down,
'right' : Key.right, 'left': Key.left, 'pause-play-toggle' : Key.media_play_pause,
'space' : Key.space, 'enter' : Key.enter, 'next-song' : Key.media_next, 'previous-song' : Key.media_previous}
keyboard = Controller()
def pressKey(hotkey):
# Based on the requested url from user, send hotkey to the pc
hotkey = hotkey.lstrip('/')
print(hotkey)
if hotkey in hotkeys_list.keys():
keyboard.press(hotkeys_list[hotkey])
keyboard.release(hotkeys_list[hotkey])
elif hotkey == 'exit':
with keyboard.pressed(Key.alt):
keyboard.press(Key.f4)
keyboard.release(Key.f4)
elif hotkey == 'shutdown':
if platform.system() == 'Linux':
subprocess.call(['shutdown', 'now'])
elif platform.system() == 'Windows':
subprocess.call(['shutdown', '/s'])
class MyHandler(BaseHTTPRequestHandler):
def do_GET(self):
"""get url path (hotkey) and send it to pressKey"""
pressKey(self.path)
self.send_response(200)
class MyTCPServer(socketserver.TCPServer):
def server_bind(self):
self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self.socket.bind(self.server_address)
"""Start serving on localhost and port 8800"""
httpd = MyTCPServer((ip, port), MyHandler)
print("Serving forever...")
httpd.serve_forever()