tui_keys.rs 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. use dioxus::events::WheelEvent;
  2. use dioxus::prelude::*;
  3. use dioxus_html::geometry::ScreenPoint;
  4. use dioxus_html::input_data::MouseButtonSet;
  5. use dioxus_html::on::{KeyboardEvent, MouseEvent};
  6. use dioxus_html::KeyCode;
  7. fn main() {
  8. dioxus::tui::launch(app);
  9. }
  10. fn app(cx: Scope) -> Element {
  11. let key = use_state(&cx, || "".to_string());
  12. let mouse = use_state(&cx, || ScreenPoint::zero());
  13. let count = use_state(&cx, || 0);
  14. let buttons = use_state(&cx, || MouseButtonSet::empty());
  15. let mouse_clicked = use_state(&cx, || false);
  16. cx.render(rsx! {
  17. div {
  18. width: "100%",
  19. height: "10px",
  20. background_color: "red",
  21. justify_content: "center",
  22. align_items: "center",
  23. flex_direction: "column",
  24. onkeydown: move |evt: KeyboardEvent| {
  25. match evt.data.key_code {
  26. KeyCode::LeftArrow => count.set(count + 1),
  27. KeyCode::RightArrow => count.set(count - 1),
  28. KeyCode::UpArrow => count.set(count + 10),
  29. KeyCode::DownArrow => count.set(count - 10),
  30. _ => {},
  31. }
  32. key.set(format!("{:?} repeating: {:?}", evt.key, evt.repeat));
  33. },
  34. onwheel: move |evt: WheelEvent| {
  35. count.set(count + evt.data.delta_y as i64);
  36. },
  37. ondrag: move |evt: MouseEvent| {
  38. mouse.set(evt.data.screen_coordinates());
  39. },
  40. onmousedown: move |evt: MouseEvent| {
  41. mouse.set(evt.data.screen_coordinates());
  42. buttons.set(evt.data.held_buttons());
  43. mouse_clicked.set(true);
  44. },
  45. onmouseup: move |evt: MouseEvent| {
  46. buttons.set(evt.data.held_buttons());
  47. mouse_clicked.set(false);
  48. },
  49. "count: {count:?}",
  50. "key: {key}",
  51. "mouse buttons: {buttons:?}",
  52. "mouse pos: {mouse:?}",
  53. "mouse button pressed: {mouse_clicked}"
  54. }
  55. })
  56. }