python 中来自套接字连接的事件类型的按键

keypress on event type from a socket connection in python

首先,我对编程完全陌生。我最后一次编程经历是在 15 年前在学校学习 turbo pascal :) 到目前为止,我已经在 Internet 上搜索了我的代码,并从类似的 javascript.

中获得了一些想法

如果特定事件类型来自套接字连接,我需要按键。连接到服务器的代码已经在工作,但我无法过滤来自套接字的类型。

这就是我的连接代码:

# Imports #
import socketio

# Vars #
sio = socketio.Client()

# Your Socket API Token from streamlabs /settings/api-settings #
token = ''

# Connection #
sio.connect('https://sockets.streamlabs.com?token=' + token)

# Code #
@sio.on("connect")
def on_connect():
    print("Connected to Streamlabs, Wait for Events")

@sio.on("event")
def on_message(data):
    print((data))

@sio.on("disconnect")
def on_disconnect():
    print("Disconnected.....")

现在,如果我在服务器上模拟跟随事件,我会得到以下输出:

{'type': 'follow', 'message': [{'name': 'KayPure', 'isTest': True, '_id': 
'31a0f9db75b6f815c0e25cc6f14d015a'}], 'for': 'twitch_account', 'event_id': 
'evt_db9fc4f099a6bd83aa9779d43fccf4a9'}

或订阅活动:

{'type': 'subscription', 'message': [{'name': 'KayPure', 'isTest': True, 
'months': 1, 'message': 'This is a test', 'emotes': None, 'sub_plan': 
'1000', '_id': '725cb1e1cbdbb31d4122ccf266d4a7bf'}], 'for': 
'twitch_account', 'event_id': 'evt_6ad1f8d2f38e5410eaaed3cbf40843b5'}

我只想输出事件的类型和名称。我不需要其余的活动信息。然后,我想模拟例如按键 "a" 跟随,按键 "b" 订阅。

背景是:某闪电软件按键触发场景。该软件与脚本在同一台机器上运行,但它不在 foreground.so 那是另一个问题。如何在后台或最小化的特定 window 中按键。

我得到了一个与此类似的 javascript,并且已经可以使用了。但软件必须在前台。因为我以后想在 python 中编写更多代码,所以我很想在 python 中得到这个 运行。

有什么想法吗?我试了很多只输出事件的类型,但我不知道把代码放在哪里。

来自德国的问候

k

希望我理解正确。要限制输出,您需要在字典 data:

中获取键 typename 的相关值
@sio.on("event")
def on_message(data):
    print(data.get('type'), data.get('message')[0].get('name'))

data 是字典。使用 get 您可以访问相应键的值。但是,要获得 name,您首先需要获得 message 的值,这是一个列表。列表的第一个也是唯一的元素是另一个字典。因此,您访问列表的第一个元素 [0],然后再次访问 get 字典中相应键的值。

要模拟按键,您可以使用 PyAutoGUI 模块。像这样:

import pyautogui
pyautogui.typewrite('a')

这应该让你继续。