Mensajes de eventos HID
Declaración
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)
Estados
- val
- Después de registrar la supervisión, toda la información de eventos HID se pasa a este callback. Como afecta considerablemente al rendimiento del script, anula el registro en cuanto ya no lo necesites.
- Todas las coordenadas táctiles de los eventos HID usan como sistema inicial la orientación vertical con el botón HOME abajo. Para convertirlas, puedes usar screen.rotate_xy.
Ejemplo
-- vaciar la cola de mensajes
proc_queue_clear("xxtouch.hid_event")
--
-- establecer el callback de supervisión
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) -- esperar 20 segundos
--
-- anular el registro del callback; si no se anula la supervisión, el script no terminará aquí
thread.unregister_event("xxtouch.hid_event", eid)
Nota: el código anterior utiliza las funciones sys.toast, sys.msleep, proc_queue_clear, thread.register_event y thread.unregister_event, que no pertenecen a este capítulo.