事件监听
# 包
from ascript.android.event import KeyEvent
from ascript.android.event import TouchEvent
from ascript.android.event import NotificationEvent
监听设备的按键、触摸、通知等事件,并在事件发生时执行回调函数.
KeyEvent 按键监听
监听设备的物理按键事件(音量键、返回键等).
需要无障碍服务
使用按键监听前,请确保已开启无障碍服务.
监听所有按键
- 函数
@KeyEvent.on()
def callback(event):
pass
- 回调参数 event
| 属性 | 类型 | 备注 |
|---|---|---|
| key_code | int | 按键码,如 24=音量+, 25=音量- |
| action | str | 按键动作, "down" 或 "up" |
- 示例
from ascript.android.event import KeyEvent
@KeyEvent.on()
def on_key(event):
print("按键码:", event.key_code, "动作:", event.action)
监听指定按键
通过 key_code 参数过滤,只监听指定按键.
- 函数
@KeyEvent.on(key_code=24)
def callback(event):
pass
- 常用键码
| 常量 | 值 | 备注 |
|---|---|---|
| KeyEvent.VOLUME_UP | 24 | 音量+ |
| KeyEvent.VOLUME_DOWN | 25 | 音量- |
| KeyEvent.BACK | 4 | 返回键 |
| KeyEvent.HOME | 3 | Home键 |
| KeyEvent.MENU | 82 | 菜单键 |
| KeyEvent.POWER | 26 | 电源键 |
- 示例
from ascript.android.event import KeyEvent
@KeyEvent.on(key_code=KeyEvent.VOLUME_UP)
def on_volume_up(event):
if event.action == "down":
print("音量+被按下")
@KeyEvent.on(key_code=KeyEvent.VOLUME_DOWN)
def on_volume_down(event):
if event.action == "down":
print("音量-被按下")
停止按键监听
- 函数
KeyEvent.stop()
- 示例
from ascript.android.event import KeyEvent
import time
@KeyEvent.on()
def on_key(event):
print(event.key_code)
# 监听10秒后停止
time.sleep(10)
KeyEvent.stop()
TouchEvent 触摸监听
监听屏幕上的控件交互事件(点击、长按、滚动).
说明
TouchEvent 基于无障碍服务,监听的是控件交互事件. 坐标为被交互控件的中心点位置.
监听所有触摸事件
- 函数
@TouchEvent.on()
def callback(event):
pass
- 回调参数 event
| 属性 | 类型 | 备注 |
|---|---|---|
| action | str | 交互类型: "click" / "long_click" / "scroll" |
| x | float | 被交互控件中心点 X 坐标 |
| y | float | 被交互控件中心点 Y 坐标 |
- 示例
from ascript.android.event import TouchEvent
@TouchEvent.on()
def on_touch(event):
print(f"动作:{event.action} 坐标:({event.x}, {event.y})")
监听指定类型
通过 action 参数过滤,只监听指定交互类型.
- 函数
@TouchEvent.on(action="click")
def callback(event):
pass
- 示例
from ascript.android.event import TouchEvent
@TouchEvent.on(action="click")
def on_click(event):
print(f"点击了 ({event.x}, {event.y})")
@TouchEvent.on(action="long_click")
def on_long_click(event):
print(f"长按了 ({event.x}, {event.y})")
停止触摸监听
- 函数
TouchEvent.stop()
NotificationEvent 通知监听
监听设备收到的通知消息.
需提前开启权限
AS程序: 开发者 -> 通知 -> 权限申请
监听所有通知
- 函数
@NotificationEvent.on()
def callback(event):
pass
- 回调参数 event
| 属性 | 类型 | 备注 |
|---|---|---|
| package | str | 通知来源的包名 |
| title | str | 通知标题 |
| text | str | 通知内容 |
| raw | object | 原始 StatusBarNotification 对象,可获取更多信息 |
| time | int | 通知时间戳(毫秒) |
- 示例
from ascript.android.event import NotificationEvent
@NotificationEvent.on()
def on_noti(event):
print(f"来自:{event.package}")
print(f"标题:{event.title}")
print(f"内容:{event.text}")
监听指定应用通知
通过 package 参数过滤,只监听指定应用的通知.
- 函数
@NotificationEvent.on(package="com.tencent.mm")
def callback(event):
pass
- 示例
from ascript.android.event import NotificationEvent
# 只监听微信通知
@NotificationEvent.on(package="com.tencent.mm")
def on_wechat(event):
print(f"微信消息 - {event.title}: {event.text}")
# 只监听QQ通知
@NotificationEvent.on(package="com.tencent.mobileqq")
def on_qq(event):
print(f"QQ消息 - {event.title}: {event.text}")
模拟点击通知
调用 event.send() 可以模拟点击该通知,跳转到对应应用.
- 示例
from ascript.android.event import NotificationEvent
@NotificationEvent.on(package="com.tencent.mm")
def on_wechat(event):
print(f"微信消息: {event.title}")
# 模拟点击通知,跳转到微信
event.send()
访问原始通知对象
通过 event.raw 可以获取原始的 StatusBarNotification Java对象,用于高级操作.
- 示例
from ascript.android.event import NotificationEvent
from android.app import Notification as AndroidNoti
@NotificationEvent.on()
def on_noti(event):
print(event.title, event.text)
# 访问原始对象获取更多信息
if event.raw:
print("标签:", event.raw.getTag())
print("ID:", event.raw.getId())
# 获取通知的额外数据
extras = event.raw.getNotification().extras
sub_text = extras.getString(AndroidNoti.EXTRA_SUB_TEXT)
print("子文本:", sub_text)
停止通知监听
- 函数
NotificationEvent.stop()
完整示例
from ascript.android.event import KeyEvent, TouchEvent, NotificationEvent
import time
# 按键监听
@KeyEvent.on(key_code=KeyEvent.VOLUME_UP)
def on_vol_up(event):
if event.action == "down":
print("音量+按下")
# 触摸监听
@TouchEvent.on(action="click")
def on_click(event):
print(f"点击 ({event.x}, {event.y})")
# 通知监听 - 微信
@NotificationEvent.on(package="com.tencent.mm")
def on_wechat(event):
print(f"微信: {event.title} - {event.text}")
# 保持脚本运行
while True:
time.sleep(1)