Mensagens de eventos HID
Declaração
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)
Estado
- val
- Depois que a escuta é registrada, todas as informações de eventos HID são enviadas a este callback. Como isso afeta significativamente o desempenho do script, cancele o registro assim que ele não for mais necessário.
- Todas as coordenadas de toque nos eventos HID usam como sistema inicial a orientação retrato, com o botão HOME na parte inferior. Para convertê-las, use screen.rotate_xy.
Exemplo
-- limpa a fila de mensagens
proc_queue_clear("xxtouch.hid_event")
--
-- configura o callback de escuta
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) -- aguarda 20 segundos
--
-- cancela o registro do callback; sem cancelar a escuta, o script não termina aqui
thread.unregister_event("xxtouch.hid_event", eid)
Nota: o código acima usa as funções sys.toast, sys.msleep, proc_queue_clear, thread.register_event e thread.unregister_event, que não pertencem a este capítulo.