这个是我在哔哩哔哩中发现的感觉比较实用在这里发表一下使用过程中的问题和见解
感谢这两位博主
如果不能用或者出了BUG可以把KeyboardListener.py的代码改成如下所示
-
import keyboard
-
import time
-
from screen_shot
import ScreenCapture
-
import io
-
import pyautogui
-
-
class
KeyboardListener:
-
def
__init__(
self, tcpServer):
-
self.tcpServer = tcpServer
-
self.t =
0
-
self.c =
0
-
self.key_state_map={}
-
self.screen_capture =
None
-
-
def
listen_keyboard(
self,callback):
-
self.callback = callback
-
keyboard.hook(self.onKeyEvent)
-
keyboard.wait()
-
-
def
onImgCapture(
self,pic):
-
imgByteArr = io.BytesIO()
-
pic.save(imgByteArr,
format=
'JPEG')
-
bytes_data = imgByteArr.getvalue()
-
self.tcpServer.send_img(bytes_data)
-
-
def
isCtrlHolding(
self):
-
return (
'ctrl'
in self.key_state_map
and self.key_state_map[
'ctrl']==
'down')\
-
or (
'left ctrl'
in self.key_state_map
and self.key_state_map[
'left ctrl']==
'down')\
-
or (
'right ctrl'
in self.key_state_map
and self.key_state_map[
'right ctrl']==
'down')
-
-
def
isAltHolding(
self):
-
return (
'alt'
in self.key_state_map
and self.key_state_map[
'alt']==
'down')\
-
or (
'left alt'
in self.key_state_map
and self.key_state_map[
'left alt']==
'down')\
-
or (
'right alt'
in self.key_state_map
and self.key_state_map[
'right alt']==
'down')
-
-
def
isKeyHolding(
self,key):
-
return (key
in self.key_state_map
and self.key_state_map[key]==
'down')
-
-
-
def
onKeyEvent(
self,key):
-
#update key_state_map
-
self.key_state_map[key.name.lower()]=key.event_type
-
-
#is screenshoot?
-
-
if self.isKeyHolding(
"caps lock")\
-
and key.event_type==
"down"\
-
and key.name.lower()==
"a":
-
self.screen_capture = ScreenCapture()
-
self.screen_capture.are_capture(self.onImgCapture)
-
-
#print(self.key_state_map)
-
#is triple c?
-
# if key.event_type=="down" \
-
# and key.name.lower()=="c" \
-
# and self.isCtrlHolding():
-
#
-
# if self.t == 0:
-
# self.t=time.time()
-
# self.c += 1
-
# print("wait for nex c",self.c)
-
# return
-
#
-
# if (time.time()-self.t<0.5):
-
# self.t=time.time()
-
# self.c += 1
-
# print("wait for nex c:",self.c)
-
#
-
# else:
-
# self.c = 0
-
# self.t=0
-
# print("wait for nex c",self.c)
-
#
-
# if self.c>=2:
-
# self.c=0
-
# print("need trans")
-
# if self.callback:
-
# self.callback()
-
-
-
-
-
if key.event_type==
"down" \
-
and key.name.lower()==
"q" \
-
and self.isCtrlHolding():
-
pyautogui.hotkey(
'ctrl',
'c')
-
-
print(
"need trans")
-
if self.callback:
-
self.callback()
如果自动杀进程的的话修改Controller.py为如下代码
-
import json
-
import time
-
-
from ComputerMonitor
import ComputerMonitor
-
from KeyboardListener
import KeyboardListener
-
from TcpServer
import TcpServer
-
from service
import *
-
import threading
-
-
def
on_message_received(
data):
-
command_message = json.loads(data)
-
script = command_message[
"script"]
-
params = command_message[
"params"]
-
exec(script)
-
-
def
on_screen_locked():
-
print(
"screen locked")
-
data = json.dumps({
"command":
2,
"message":
""})
-
print(data)
-
tcpServer.send_text(data)
-
-
computerMonitor = ComputerMonitor(on_screen_locked)
-
-
def
on_tcp_connected():
-
if
not computerMonitor.started:
-
computerMonitor.start()
-
-
-
tcpServer = TcpServer()
-
tcpServer.set_receive_listener(on_message_received)
-
tcpServer.connected_listener = on_tcp_connected
-
tcpServer.start()
-
-
keyboardListener = KeyboardListener(tcpServer)
-
-
def
onTrans():
-
print(
"need trans1111")
-
content = getClipContent()
-
text = json.dumps({
"command":
1,
"message":content})
-
-
tcpServer.send_text(text)
-
-
-
def
Trans_alive():
#用来进行TCP保活
-
content =
''
-
# text = json.dumps({"command":1,"message":content})
-
text = json.dumps({
"command":
11,
"message":content})
#这里之所以用11,而不是1,是因为原作者的command1对应的命令是翻译,会在手机端触发对应的翻译任务看,而11就只相当于是一条空命令了,既可以完成保活,又不会干扰手机端的命令执行,一举两得
-
print(
"text:", text)
-
tcpServer.send_text(text)
-
-
def
run():
-
print(
'用来保活的,不用管我')
-
t = threading.Timer(
3, run)
-
t.start()
-
-
Trans_alive()
-
-
-
t = threading.Timer(
3,run)
-
t.start()
-
-
keyboardListener.listen_keyboard(onTrans)
如果连接不到的话可以按下键盘的win+R
输入cmd
查看IP配置
有几个适配器就用它减一找到TcpServer.py编辑它把红圈圈住的数字改为刚才得到的数字
好了这就是我的一些见解
转载:https://blog.csdn.net/m0_66674794/article/details/128780703
查看评论