Aller au contenu principal

Messages d’événements HID

Déclaration

thread.register_event("xxtouch.hid_event", function(val)
local event = json.decode(val)
if event.event_type=="touch" then
if event.event_name=="touch.on" then
sys.toast("触摸接触位置: ("..event.x..", "..event.y..")\n"..event.time)
elseif event.event_name=="touch.move" then
sys.toast("触摸移动到位置: ("..event.x..", "..event.y..")\n"..event.time)
elseif event.event_name=="touch.off" then
sys.toast("触摸从位置: ("..event.x..", "..event.y..") 离开屏幕\n"..event.time)
end
else
if event.event_name=="key.down" then
sys.toast("按下按键: "..event.key_name.."\n"..event.time)
elseif event.event_name=="key.up" then
sys.toast("抬起按键: "..event.key_name.."\n"..event.time)
end
end
end)

État

  • val
    • Après l’enregistrement de l’écoute, toutes les informations d’événements HID sont transmises à ce rappel. Comme cela affecte fortement les performances du script, désenregistrez l’écoute dès qu’elle n’est plus nécessaire.
    • Dans les événements HID, toutes les coordonnées tactiles utilisent comme repère initial l’orientation portrait avec le bouton HOME en bas. Pour les convertir, utilisez screen.rotate_xy.

Exemple

-- Vide la file de messages
proc_queue_clear("xxtouch.hid_event")
--
-- Établit le rappel d’écoute
local eid = thread.register_event("xxtouch.hid_event", function(val)
local event = json.decode(val)
if event.event_type=="touch" then
if event.event_name=="touch.on" then
sys.toast("触摸接触位置: ("..event.x..", "..event.y..")\n"..event.time)
elseif event.event_name=="touch.move" then
sys.toast("触摸移动到位置: ("..event.x..", "..event.y..")\n"..event.time)
elseif event.event_name=="touch.off" then
sys.toast("触摸从位置: ("..event.x..", "..event.y..") 离开屏幕\n"..event.time)
end
else
if event.event_name=="key.down" then
sys.toast("按下按键: "..event.key_name.."\n"..event.time)
elseif event.event_name=="key.up" then
sys.toast("抬起按键: "..event.key_name.."\n"..event.time)
end
end
end)
--
touch.on(100, 100):off()
sys.msleep(1000)
key.press('homebutton')
--
sys.msleep(20000) -- Attend 20 secondes
--
-- Désenregistre le rappel ; sans désenregistrer l’écoute, le script ne se termine pas ici
thread.unregister_event("xxtouch.hid_event", eid)

Remarque : le code ci-dessus utilise les fonctions hors de ce chapitre sys.toast, sys.msleep, proc_queue_clear, thread.register_event et thread.unregister_event.