多指觸控

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

注意

多指觸控介面需要您將客戶端升級至最低版本 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()

# 呼叫 record() 後,會在逾時時間(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)