当前位置: 首页 > 面试题库 >

如何使用AccessibilityService在Android上执行拖动(基于X,Y鼠标坐标)?

东郭凯捷
2023-03-14
问题内容

我想知道如何在基于X,Y鼠标坐标的android上执行拖动吗?
考虑两个简单的示例,Team Viewer / QuickSupport 分别
在远程智能手机和Windows Paint Pen上绘制“密码模式”


我所能做的就是模拟触摸(使用dispatchGesture()和也AccessibilityNodeInfo.ACTION_CLICK)。

我找到了这些相关链接,但不知道它们是否有用:

使用AccessibilityService在屏幕上执行滑动例子1
继续手势下面是我的工作代码,该代码用于将鼠标坐标(在PictureBox控件内部)发送到远程电话并模拟触摸。

Windows窗体应用程序:

private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
    foreach (ListViewItem item in lvConnections.SelectedItems)
    {
        // Remote screen resolution
        string[] tokens = item.SubItems[5].Text.Split('x'); // Ex: 1080x1920

        int xClick = (e.X * int.Parse(tokens[0].ToString())) / (pictureBox1.Size.Width);
        int yClick = (e.Y * int.Parse(tokens[1].ToString())) / (pictureBox1.Size.Height);

        Client client = (Client)item.Tag;

        if (e.Button == MouseButtons.Left)
            client.sock.Send(Encoding.UTF8.GetBytes("TOUCH" + xClick + "<|>" + yClick + Environment.NewLine));
    }
}

编辑:

我最后的尝试是分别使用鼠标坐标(“ C#Windows窗体
应用程序”)和自定义android例程(参考
上面链接的“滑动屏幕”的代码)进行“滑动屏幕”:

private Point mdownPoint = new Point();

private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
    foreach (ListViewItem item in lvConnections.SelectedItems)
    {
        // Remote screen resolution
        string[] tokens = item.SubItems[5].Text.Split('x'); // Ex: 1080x1920

        Client client = (Client)item.Tag;

        if (e.Button == MouseButtons.Left)
        {
            xClick = (e.X * int.Parse(tokens[0].ToString())) / (pictureBox1.Size.Width); 
            yClick = (e.Y * int.Parse(tokens[1].ToString())) / (pictureBox1.Size.Height);

            // Saving start position:

            mdownPoint.X = xClick; 
            mdownPoint.Y = yClick;

            client.sock.Send(Encoding.UTF8.GetBytes("TOUCH" + xClick + "<|>" + yClick + Environment.NewLine));
        }
    }
}

private void PictureBox1_MouseMove(object sender, MouseEventArgs e)
{
    foreach (ListViewItem item in lvConnections.SelectedItems)
    {
        // Remote screen resolution
        string[] tokens = item.SubItems[5].Text.Split('x'); // Ex: 1080x1920

        Client client = (Client)item.Tag;

        if (e.Button == MouseButtons.Left)
        {
            xClick = (e.X * int.Parse(tokens[0].ToString())) / (pictureBox1.Size.Width);
            yClick = (e.Y * int.Parse(tokens[1].ToString())) / (pictureBox1.Size.Height);

            client.sock.Send(Encoding.UTF8.GetBytes("MOUSESWIPESCREEN" + mdownPoint.X + "<|>" + mdownPoint.Y + "<|>" + xClick + "<|>" + yClick + Environment.NewLine));
        }
    }
}

android AccessibilityService :

public void Swipe(int x1, int y1, int x2, int y2, int time) {

if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
    System.out.println(" ======= Swipe =======");

    GestureDescription.Builder gestureBuilder = new GestureDescription.Builder();
    Path path = new Path();
    path.moveTo(x1, y1);
    path.lineTo(x2, y2);

    gestureBuilder.addStroke(new GestureDescription.StrokeDescription(path, 100, time));
    dispatchGesture(gestureBuilder.build(), new GestureResultCallback() {
        @Override
        public void onCompleted(GestureDescription gestureDescription) {
            System.out.println("SWIPE Gesture Completed :D");
            super.onCompleted(gestureDescription);
        }
    }, null);
}

}

会产生以下结果(例如,仍然无法绘制“图案
密码”,例如TeamViewer)。但是,就像下面的评论中所说的那样,我
认为使用类似的方法可以使用“继续
手势”来实现。
欢迎向这个方向提出任何建议。

Edit 2:

Definitely, the solution is continued
gestures
like said on previous Edit.

  • Simulating joystick movement using AccessibilityService
  • Why the continueStroke function is not work

And below is a supposed fixed code that i found
here =>

android AccessibilityService:

// Simulates an L-shaped drag path: 200 pixels right, then 200 pixels down.
Path path = new Path();
path.moveTo(200,200);
path.lineTo(400,200);

final GestureDescription.StrokeDescription sd = new GestureDescription.StrokeDescription(path, 0, 500, true);

// The starting point of the second path must match
// the ending point of the first path.
Path path2 = new Path();
path2.moveTo(400,200);
path2.lineTo(400,400);

final GestureDescription.StrokeDescription sd2 = sd.continueStroke(path2, 0, 500, false); // 0.5 second

HongBaoService.mService.dispatchGesture(new GestureDescription.Builder().addStroke(sd).build(), new AccessibilityService.GestureResultCallback(){

@Override
public void onCompleted(GestureDescription gestureDescription){
super.onCompleted(gestureDescription);
HongBaoService.mService.dispatchGesture(new GestureDescription.Builder().addStroke(sd2).build(),null,null);
}

@Override
public void onCancelled(GestureDescription gestureDescription){
super.onCancelled(gestureDescription);
}
},null);

Then, my doubt is: how send correctly mouse coordinates for code above, of
the way that can perform drag to any direction?
Some idea?

然后,我的疑问是:如何正确发送上面代码的鼠标坐标,
以哪种方式可以向任意方向执行拖动?有想法吗

编辑3:

我发现了两个用于执行拖动的例程,但是它们使用的是
UiAutomation
+ injectInputEvent()。AFAIK,
事件注入仅在像
此处和
此处所述的系统应用程序中有效,我不希望
如此。

这是发现的例程:

公共布尔刷卡(int downX,int downY,int upX,int upY,int步骤,布尔拖动)
公共布尔刷卡(Point []段,int segmentSteps)
然后实现我的目标,我认为2RD例行更拨付给使用
具有表现出对码(逻辑下,不包括事件注)编辑
2和发送的所有点pictureBox1_MouseDown和pictureBox1_MouseMove
(C#Windows窗体应用程序)分别填写Point[]动态,并
在pictureBox1_MouseUp发送cmd执行例程并使用此数组
填充。如果您对第一个例程有想法,请告诉我:D。

如果在阅读完此编辑后您有可能的解决方案,
请在回答中告诉我,而我将尝试测试该想法。


问题答案:

这是一个基于问题Edit 3的解决方案示例。

C#Windows Froms应用程序“ formMain.cs ”:

using System.Net.Sockets;

private List<Point> lstPoints;

private void pictureBox1_MouseDown(object sender, MouseEventArgs e) 
{
    if (e.Button == MouseButtons.Left)
    {
        lstPoints = new List<Point>();
        lstPoints.Add(new Point(e.X, e.Y));
    }
}

private void PictureBox1_MouseMove(object sender, MouseEventArgs e)
{
    if (e.Button == MouseButtons.Left)
    {
        lstPoints.Add(new Point(e.X, e.Y));
    }
}

private void PictureBox1_MouseUp(object sender, MouseEventArgs e)
{
    lstPoints.Add(new Point(e.X, e.Y));

    StringBuilder sb = new StringBuilder();

    foreach (Point obj in lstPoints)
    {
        sb.Append(Convert.ToString(obj) + ":");
    }

    serverSocket.Send("MDRAWEVENT" + sb.ToString() + Environment.NewLine);
}

android service ” SocketBackground.java “:

import java.net.Socket;

String xline;

while (clientSocket.isConnected()) {

    BufferedReader xreader = new BufferedReader(new InputStreamReader(clientSocket.getInputStream(), StandardCharsets.UTF_8));

    if (xreader.ready()) {

        while ((xline = xreader.readLine()) != null) {
                xline = xline.trim();

            if (xline != null && !xline.trim().isEmpty()) {

                if (xline.contains("MDRAWEVENT")) {

                    String coordinates = xline.replace("MDRAWEVENT", "");
                    String[] tokens = coordinates.split(Pattern.quote(":"));
                    Point[] moviments = new Point[tokens.length];

                    for (int i = 0; i < tokens.length; i++) {
                       String[] coordinates = tokens[i].replace("{", "").replace("}", "").split(",");

                       int x = Integer.parseInt(coordinates[0].split("=")[1]);
                       int y = Integer.parseInt(coordinates[1].split("=")[1]);

                       moviments[i] = new Point(x, y);
                    }

                    MyAccessibilityService.instance.mouseDraw(moviments, 2000);
                }
            }
        }
    }
}

android
AccessibilityService
MyAccessibilityService.java “:

public void mouseDraw(Point[] segments, int time) {
    if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {

        Path path = new Path();
        path.moveTo(segments[0].x, segments[0].y);

        for (int i = 1; i < segments.length; i++) {

            path.lineTo(segments[i].x, segments[i].y);

            GestureDescription.StrokeDescription sd = new GestureDescription.StrokeDescription(path, 0, time);

            dispatchGesture(new GestureDescription.Builder().addStroke(sd).build(), new AccessibilityService.GestureResultCallback() {

                @Override
                public void onCompleted(GestureDescription gestureDescription) {
                    super.onCompleted(gestureDescription);
                }

                @Override
                public void onCancelled(GestureDescription gestureDescription) {
                    super.onCancelled(gestureDescription);
                }
            }, null);
        }
    }
}


 类似资料:
  • 我想将鼠标移动到某个位置(0,0)。在上一次点击操作之后,我实际上需要将鼠标从该位置移开,因为它会干扰我需要执行的下一个操作。 我发现遵循Java代码这是一个完美的起点,但我无法将其翻译成VBA并适应我的需要。 资料来源: 如何使用Java在Selenium WebDriver中执行鼠标悬停功能? https://www.guru99.com/keyboard-mouse-events-files

  • 问题内容: 是否可以使用给定的坐标来模拟网页中JavaScript的点击? 问题答案: 您可以调度 点击 事件,尽管这与实际点击不同。例如,它不能用于欺骗跨域iframe文档以使其被点击。 所有现代浏览器都支持和,因为至少IE 6,火狐5,Chrome浏览器和Safari的你很可能任何版本可能任何版本的关心。它甚至会点击链接并提交表格:

  • 我是JavaFX的新手,我似乎找不到如何做到这一点。 我在Vbox中有一个ListView,我用一个可见的字符串列表填充它。我已经将ListView的SelectionMode设置为MULTIPLE,这允许我在按住Ctrl或Shift键的同时选择多个项目。 我希望能够单击一行并向下拖动鼠标并选择多行,但我不知道该怎么做。我已经尝试了几次搜索,似乎只找到了拖放,这不是我需要的。

  • 问题内容: Mathematica 我想在没有Mathematica的家里做上述事情。使用所需的任何工具,我喜欢使用Python和R,但对任何候选解决方案都满意。我想到的第一件事是RStudio,这里是这个问题,但是我不确定是否有更好的方法可以做到这一点。 如何在X上进行交互式GUI创新? Mathematica的步骤-摘要 问题答案: 这是执行您所描述的R函数: 您可以更改参数以更改样条线的样式

  • 所以我正在尝试制作一个程序,在我单击画布的地方它会在我单击的位置创建一个形状。当我单击时,我无法抓取鼠标的x和y坐标并使用我的形状的x和y坐标 我的HTML代码: 我的JavaScript代码: 我正在尝试找出是否可以使用我的鼠标x和y坐标为我的形状生成位置。任何帮助都将不胜感激。谢谢。

  • 问题内容: 我正在使用Java。我想根据mousedrag事件绘制矩形。如果用户拖动鼠标,则小程序上的矩形应根据当前鼠标坐标增加或减少。我有以下代码。 在下面的代码中,我使用[b] SelectionArea [/ b]类扩展了在其上执行绘制操作的画布。我在此类中使用[b] image [/ b]变量进行双缓冲,以减少闪烁并保存小程序的先前状态(即,绘制小程序的内容) 但如果我画第一个矩形,代码工