diff --git a/examples/click.rs b/examples/click.rs new file mode 100644 index 0000000..895ed31 --- /dev/null +++ b/examples/click.rs @@ -0,0 +1,31 @@ +extern crate termion; + +use termion::event::{Key, Event, MouseEvent}; +use termion::input::TermRead; +use termion::raw::IntoRawMode; +use std::io::{Write, stdout, stdin}; + +fn main() { + let stdin = stdin(); + let mut stdout = stdout().into_raw_mode().unwrap().with_mouse().unwrap(); + + write!(stdout, "{}{}q to exit. Click, click, click!", termion::clear::All, termion::cursor::Goto(1, 1)).unwrap(); + stdout.flush().unwrap(); + + for c in stdin.events() { + let evt = c.unwrap(); + match evt { + Event::Key(Key::Char('q')) => break, + Event::Mouse(me) => { + match me { + MouseEvent::Press(_, x, y) => { + write!(stdout, "{}x", termion::cursor::Goto(x, y)).unwrap(); + }, + _ => (), + } + } + _ => {} + } + stdout.flush().unwrap(); + } +} diff --git a/src/event.rs b/src/event.rs index 8d533d9..1b960e3 100644 --- a/src/event.rs +++ b/src/event.rs @@ -19,8 +19,12 @@ pub enum Event { #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)] pub enum MouseEvent { /// A mouse button was pressed. + /// + /// The coordinates are one-based. Press(MouseButton, u16, u16), /// A mouse button was released. + /// + /// The coordinates are one-based. Release(u16, u16), } @@ -114,8 +118,8 @@ where I: Iterator> // X10 emulation mouse encoding: ESC [ CB Cx Cy (6 characters only). let cb = iter.next().unwrap().unwrap() as i8 - 32; // (1, 1) are the coords for upper left. - let cx = (iter.next().unwrap().unwrap() as u8 - 1).saturating_sub(32) as u16; - let cy = (iter.next().unwrap().unwrap() as u8 - 1).saturating_sub(32) as u16; + let cy = (iter.next().unwrap().unwrap() as u8).saturating_sub(32) as u16; + let cx = (iter.next().unwrap().unwrap() as u8).saturating_sub(32) as u16; Event::Mouse(match cb & 0b11 { 0 => { if cb & 0x40 != 0 { @@ -152,8 +156,8 @@ where I: Iterator> let ref mut nums = str_buf.split(';'); let cb = nums.next().unwrap().parse::().unwrap(); - let cx = nums.next().unwrap().parse::().unwrap() - 1; - let cy = nums.next().unwrap().parse::().unwrap() - 1; + let cy = nums.next().unwrap().parse::().unwrap(); + let cx = nums.next().unwrap().parse::().unwrap(); let button = match cb { 0 => MouseButton::Left,