Fix mouse input

This commit is contained in:
ticki 2016-07-23 19:03:03 +02:00
parent 30afb4c32e
commit 59d4ae427a
2 changed files with 39 additions and 4 deletions

31
examples/click.rs Normal file
View File

@ -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();
}
}

View File

@ -19,8 +19,12 @@ pub enum Event {
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)] #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub enum MouseEvent { pub enum MouseEvent {
/// A mouse button was pressed. /// A mouse button was pressed.
///
/// The coordinates are one-based.
Press(MouseButton, u16, u16), Press(MouseButton, u16, u16),
/// A mouse button was released. /// A mouse button was released.
///
/// The coordinates are one-based.
Release(u16, u16), Release(u16, u16),
} }
@ -114,8 +118,8 @@ where I: Iterator<Item = Result<u8, Error>>
// X10 emulation mouse encoding: ESC [ CB Cx Cy (6 characters only). // X10 emulation mouse encoding: ESC [ CB Cx Cy (6 characters only).
let cb = iter.next().unwrap().unwrap() as i8 - 32; let cb = iter.next().unwrap().unwrap() as i8 - 32;
// (1, 1) are the coords for upper left. // (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).saturating_sub(32) as u16;
let cy = (iter.next().unwrap().unwrap() as u8 - 1).saturating_sub(32) as u16; let cx = (iter.next().unwrap().unwrap() as u8).saturating_sub(32) as u16;
Event::Mouse(match cb & 0b11 { Event::Mouse(match cb & 0b11 {
0 => { 0 => {
if cb & 0x40 != 0 { if cb & 0x40 != 0 {
@ -152,8 +156,8 @@ where I: Iterator<Item = Result<u8, Error>>
let ref mut nums = str_buf.split(';'); let ref mut nums = str_buf.split(';');
let cb = nums.next().unwrap().parse::<u16>().unwrap(); let cb = nums.next().unwrap().parse::<u16>().unwrap();
let cx = nums.next().unwrap().parse::<u16>().unwrap() - 1; let cy = nums.next().unwrap().parse::<u16>().unwrap();
let cy = nums.next().unwrap().parse::<u16>().unwrap() - 1; let cx = nums.next().unwrap().parse::<u16>().unwrap();
let button = match cb { let button = match cb {
0 => MouseButton::Left, 0 => MouseButton::Left,