pi0-keyboard-server/keyserver.py

84 lines
2.7 KiB
Python

from bottle import run, post, request, response, get, route
from time import sleep
from key import *
NULL_CHAR = chr(0)
def write_report(report):
with open('/dev/hidg0', 'rb+') as fd:
fd.write(report.encode())
def press_key(keytype):
write_report(NULL_CHAR*2+chr(key[keytype])+NULL_CHAR*5)
sleep(0.1)
write_report(NULL_CHAR*8)
def send_string(neededstring):
stringlist = [*neededstring.upper()]
for l in stringlist:
if l == '' or l == ' ':
l = 'SPACEBAR'
press_key(l)
@route('/marco/<path>',method = 'POST')
def process(path):
if path == 'linuxtowindows':
print('swtiching pc to windows')
write_report(chr(0x04)*2+chr(key['SPACE'])+NULL_CHAR*5)
sleep(0.1)
write_report(NULL_CHAR*8)
# execute the shutdown reboot command for linux
send_string('reboot')
press_key('ENTER')
for x in range(10):
press_key('F9')
sleep(0.3)
for x in range('3'):
press_key('DOWN_ARROW')
press_key('ENTER')
print('booting...')
elif path == 'windowstolinux':
print('swtiching pc to linux')
write_report(NULL_CHAR*2+chr(key['APPLICATION'])+NULL_CHAR*5)
write_report(NULL_CHAR*2+chr(key['R'])+NULL_CHAR*5)
sleep(0.1)
write_report(NULL_CHAR*8)
# execute the shutdown reboot command for windows
send_string('shutdown')
write_report(NULL_CHAR*2+chr(key['FORWARD_SLASH'])+NULL_CHAR*5)
write_report(NULL_CHAR*8)
write_report(NULL_CHAR*2+chr(key['R'])+NULL_CHAR*5)
write_report(NULL_CHAR*8)
write_report(NULL_CHAR*2+chr(key['SPACEBAR'])+NULL_CHAR*5)
write_report(NULL_CHAR*8)
write_report(NULL_CHAR*2+chr(key['FORWARD_SLASH'])+NULL_CHAR*5)
write_report(NULL_CHAR*8)
write_report(NULL_CHAR*2+chr(key['N'])+NULL_CHAR*5)
write_report(NULL_CHAR*8)
press_key('ENTER')
sleep(2)
for x in range(10):
press_key('F9')
sleep(0.3)
for x in range('2'):
press_key('DOWN_ARROW')
press_key('ENTER')
print('booting...')
@route('/presskey/<path>',method = 'POST')
def process(path):
newpath = eval(path)
write_report(NULL_CHAR*2+chr(key[newpath])+NULL_CHAR*5)
write_report(NULL_CHAR*8)
@route('/presswithoutrelease/<path>',method = 'POST')
def process(path):
if path == 'release':
write_report(NULL_CHAR*8)
else:
newpath = eval(path)
write_report(NULL_CHAR*2+chr(key[newpath])+NULL_CHAR*5)
run(host='0.0.0.0', port=80, debug=True)