diff --git a/examples/mouse.rs b/examples/mouse.rs index 37eaec9..98d47b5 100644 --- a/examples/mouse.rs +++ b/examples/mouse.rs @@ -9,20 +9,24 @@ fn main() { let stdin = stdin(); let mut stdout = MouseTerminal::from(stdout().into_raw_mode().unwrap()); - write!(stdout, "{}{}q to exit. Type stuff, use alt, click around...", termion::clear::All, termion::cursor::Goto(1, 1)).unwrap(); + writeln!(stdout, + "{}{}q to exit. Type stuff, use alt, click around...", + termion::clear::All, + termion::cursor::Goto(1, 1)) + .unwrap(); - let mut x = 1; - let mut y = 1; + let mut x = 5; + let mut y = 5; for c in stdin.events() { let evt = c.unwrap(); - writeln!(stdout, "{:?}{}{}", evt, termion::cursor::Goto(5, 5), termion::clear::CurrentLine).unwrap(); match evt { Event::Key(Key::Char('q')) => break, Event::Mouse(me) => { match me { MouseEvent::Press(_, a, b) | - MouseEvent::Release(a, b) => { + MouseEvent::Release(a, b) | + MouseEvent::Hold(a, b) => { x = a; y = b; } @@ -30,7 +34,13 @@ fn main() { } _ => {} } - writeln!(stdout, "{:?}{}", evt, termion::cursor::Goto(x, y)).unwrap(); + write!(stdout, + "{}{} {:?}{}", + termion::clear::All, + termion::cursor::Goto(x, y), + evt, + termion::cursor::Goto(x, y)) + .unwrap(); stdout.flush().unwrap(); } diff --git a/src/event.rs b/src/event.rs index 387257a..4823439 100644 --- a/src/event.rs +++ b/src/event.rs @@ -26,6 +26,10 @@ pub enum MouseEvent { /// /// The coordinates are one-based. Release(u16, u16), + /// A mouse button is held over the given coordinates. + /// + /// The coordinates are one-based. + Hold(u16, u16), } /// A mouse button. @@ -159,20 +163,28 @@ where I: Iterator> let cx = nums.next().unwrap().parse::().unwrap(); let cy = nums.next().unwrap().parse::().unwrap(); - let button = match cb { - 0 => MouseButton::Left, - 1 => MouseButton::Middle, - 2 => MouseButton::Right, - 64 => MouseButton::WheelUp, - 65 => MouseButton::WheelDown, + let event = match cb { + 0...2 | 64...65 => { + let button = match cb { + 0 => MouseButton::Left, + 1 => MouseButton::Middle, + 2 => MouseButton::Right, + 64 => MouseButton::WheelUp, + 65 => MouseButton::WheelDown, + _ => return error, + }; + match c { + b'M' => MouseEvent::Press(button, cx, cy), + b'm' => MouseEvent::Release(cx, cy), + _ => return error, + + } + } + 32 => MouseEvent::Hold(cx, cy), _ => return error, }; - Event::Mouse(match c { - b'M' => MouseEvent::Press(button, cx, cy), - b'm' => MouseEvent::Release(cx, cy), - _ => return error, - }) + Event::Mouse(event) } Some(Ok(c @ b'0'...b'9')) => { // Numbered escape code. @@ -203,6 +215,7 @@ where I: Iterator> 33 => MouseEvent::Press(MouseButton::Middle, cx, cy), 34 => MouseEvent::Press(MouseButton::Right, cx, cy), 35 => MouseEvent::Release(cx, cy), + 64 => MouseEvent::Hold(cx, cy), 96 | 97 => MouseEvent::Press(MouseButton::WheelUp, cx, cy), _ => return error,