多點觸控

除了常規自動化外,我們還提供了進階多點觸控介面,讓您可以實現複雜的手指操作。具備硬體軌跡錄製、重播、建構、儲存等功能。

注意

多點觸控介面需要您將用戶端升級至最低 9.22 版本,為了獲得最佳的相容性,建議同時升級伺服器端。

為了讓您能最快了解如何使用,建議您開啟「開發者選項」-「輸入」- 開啟「顯示輕觸回應」以及「指標位置」功能(如果未開啟,您可能看不到任何回饋)。以下範例程式碼向您展示如何在螢幕的左上角畫一個 X 符號。

touch = d.touch()
finger0 = touch.contact(0)
finger1 = touch.contact(1)

# --- 1. 起點按下 ---
finger0.down(150, 320)
finger1.down(450, 320)
touch.wait(20)

# --- 2. 軌跡移動 (10 個座標點) ---
finger0.move(180, 350)
finger1.move(420, 350)
touch.wait(20)

finger0.move(210, 380)
finger1.move(390, 380)
touch.wait(20)

finger0.move(240, 410)
finger1.move(360, 410)
touch.wait(20)

finger0.move(270, 440)
finger1.move(330, 440)
touch.wait(20)

finger0.move(300, 470) # 交叉中心點
finger1.move(300, 470)
touch.wait(20)

finger0.move(330, 500)
finger1.move(270, 500)
touch.wait(20)

finger0.move(360, 530)
finger1.move(240, 530)
touch.wait(20)

finger0.move(390, 560)
finger1.move(210, 560)
touch.wait(20)

finger0.move(420, 590)
finger1.move(180, 590)
touch.wait(20)

finger0.move(450, 620) # 終點
finger1.move(150, 620)
touch.wait(20)

# --- 3. 抬起手指 ---
finger0.up()
finger1.up()

# 執行操作
touch.perform()

錄製軌跡

為了方便您進行軌跡建構,我們同時提供了錄製功能。錄製功能需要您操作實體裝置。

# 取得一個 multi touch 工作階段
touch = d.touch()

# 錄製軌跡,呼叫後將在逾時時間(120 秒)內等待您操作實體裝置螢幕,當您的手指抬起後將會返回。
touch.record()

# 您可以將軌跡儲存為二進位檔案
touch.save("/sdcard/touch/bin")

# 您也可以直接重播軌跡
touch.perform()

軌跡儲存

為了避免每次都透過程式碼重新建構軌跡,我們提供了軌跡儲存功能,您可以將軌跡資料永久儲存到磁碟上。

# 取得一個 multi touch 工作階段
touch = d.touch()

# 您可以儲存剛剛建構的軌跡以供載入
touch.save("/sdcard/track0.bin")

軌跡載入

您可以直接透過儲存在磁碟上的軌跡檔案將軌跡載入到工作階段內。

# 取得一個 multi touch 工作階段
touch = d.touch()

# 從檔案載入軌跡
touch.load("/sdcard/track0.bin")

軌跡建構

除了可以透過錄製進行軌跡建構外,您也可以選擇自行透過程式碼建構觸控軌跡。下面我們來向您介紹手動建構以及其他可用功能。一個合法的觸控操作包含「按下」、「移動」、「抬起」三種事件,可以為按下、移動等事件設定壓力參數。

# 取得一個 multi touch 工作階段
touch = d.touch()

# 取得兩個(或多個)指尖輸入
finger0 = touch.contact(0)
finger1 = touch.contact(1)

# 手指 0 在座標 150 320 按下
# 參數 z 代表手指壓力,可用值為 1-255,預設為 128
finger0.down(150, 320, z=128)

# 手指 0 從 150 320 移動到 180 350
finger0.move(180, 350, z=128)

# 等待 20 毫秒(等同於 finger0.wait(20))
touch.wait(20)

# 手指 0 抬起
finger0.up()

# 清除所有軌跡
touch.reset()

# 執行操作(wait 表示是否等待完成)
touch.perform(wait=True)