Chuyển tới nội dung chính

Thông báo sự kiện HID

Khai bá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("Chạm xuống tại: ("..event.x..", "..event.y..")\n"..event.time)
elseif event.event_name=="touch.move" then
sys.toast("Di chuyển chạm đến: ("..event.x..", "..event.y..")\n"..event.time)
elseif event.event_name=="touch.off" then
sys.toast("Nhấc chạm tại: ("..event.x..", "..event.y..")\n"..event.time)
end
else
if event.event_name=="key.down" then
sys.toast("Nhấn phím: "..event.key_name.."\n"..event.time)
elseif event.event_name=="key.up" then
sys.toast("Nhả phím: "..event.key_name.."\n"..event.time)
end
end
end)

Trạng thái

  • val
    Nếu đã đăng ký lắng nghe, toàn bộ thông tin sự kiện HID sẽ được gửi đến đây. Việc này ảnh hưởng đáng kể đến hiệu năng tập lệnh; hãy hủy đăng ký ngay khi không còn cần thiết.
    Tất cả tọa độ chạm trong sự kiện HID sử dụng hệ tọa độ màn hình dọc với nút Home ở phía dưới. Khi cần, hãy chuyển đổi bằng screen.rotate_xy.

Ví dụ

-- Xóa hàng đợi thông báo
proc_queue_clear("xxtouch.hid_event")
--
-- Bắt đầu lắng nghe
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("Chạm xuống tại: ("..event.x..", "..event.y..")\n"..event.time)
elseif event.event_name=="touch.move" then
sys.toast("Di chuyển chạm đến: ("..event.x..", "..event.y..")\n"..event.time)
elseif event.event_name=="touch.off" then
sys.toast("Nhấc chạm tại: ("..event.x..", "..event.y..")\n"..event.time)
end
else
if event.event_name=="key.down" then
sys.toast("Nhấn phím: "..event.key_name.."\n"..event.time)
elseif event.event_name=="key.up" then
sys.toast("Nhả phím: "..event.key_name.."\n"..event.time)
end
end
end)
--
touch.on(100, 100):off()
sys.msleep(1000)
key.press('homebutton')
--
sys.msleep(20000) -- chờ 20 giây
--
-- Hủy đăng ký lệnh gọi lại; nếu không hủy, tập lệnh sẽ không kết thúc tại đây
thread.unregister_event("xxtouch.hid_event", eid)

Lưu ý: Sử dụng sys.toast, sys.msleep, proc_queue_clear, thread.register_event, thread.unregister_event