|
|
|
@ -145,8 +145,20 @@ impl Reader {
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn next(&mut self) -> Result<Console::INPUT_RECORD> {
|
|
|
|
|
// All buffered items have been processed, ask the OS for new event records
|
|
|
|
|
pub fn next(&mut self) -> Result<Event> {
|
|
|
|
|
let rec = self.next_rec()?;
|
|
|
|
|
if rec.EventType as u32 == Console::KEY_EVENT {
|
|
|
|
|
unsafe {
|
|
|
|
|
let event = rec.Event.KeyEvent;
|
|
|
|
|
if event.wVirtualKeyCode == 0 && event.uChar.UnicodeChar == 27 {
|
|
|
|
|
return Ok(self.next_escape_sequence()?);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Ok(rec.into())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn next_rec(&mut self) -> Result<Console::INPUT_RECORD> {
|
|
|
|
|
if self.buf_idx as u32 >= self.buf_len {
|
|
|
|
|
unsafe {
|
|
|
|
|
Error::check(Console::ReadConsoleInputA(
|
|
|
|
@ -162,6 +174,49 @@ impl Reader {
|
|
|
|
|
self.buf_idx += 1;
|
|
|
|
|
return Ok(rec);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn next_escape_sequence(&mut self) -> Result<Event> {
|
|
|
|
|
self.take_bracket()?;
|
|
|
|
|
match self.next_escape_char()? {
|
|
|
|
|
'D' => Ok(Event::Left),
|
|
|
|
|
e => Err(Error::input_error(format!("unexpected escape char: {}", e)).into()),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn take_bracket(&mut self) -> Result<()> {
|
|
|
|
|
let rec = self.next_rec()?;
|
|
|
|
|
if rec.EventType as u32 != Console::KEY_EVENT {
|
|
|
|
|
Err(Error::input_error("failed to read escape sequence: not a key event").into())
|
|
|
|
|
} else {
|
|
|
|
|
unsafe {
|
|
|
|
|
let event = rec.Event.KeyEvent;
|
|
|
|
|
if event.wVirtualKeyCode == 0 && event.uChar.UnicodeChar == 91 {
|
|
|
|
|
Ok(())
|
|
|
|
|
} else {
|
|
|
|
|
Err(Error::input_error("failed to read escape sequence: not a [").into())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn next_escape_char(&mut self) -> Result<char> {
|
|
|
|
|
let rec = self.next_rec()?;
|
|
|
|
|
if rec.EventType as u32 != Console::KEY_EVENT {
|
|
|
|
|
Err(
|
|
|
|
|
Error::input_error("failed to read char in escape sequence: not a key event")
|
|
|
|
|
.into(),
|
|
|
|
|
)
|
|
|
|
|
} else {
|
|
|
|
|
unsafe {
|
|
|
|
|
let n = rec.Event.KeyEvent.uChar.UnicodeChar as u32;
|
|
|
|
|
let c = char::from_u32(n);
|
|
|
|
|
c.ok_or_else(|| {
|
|
|
|
|
let msg = format!("escape key value is not a valid unicode character: {}", n);
|
|
|
|
|
Error::input_error(msg).into()
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
@ -169,11 +224,9 @@ pub enum Event {
|
|
|
|
|
Focus(bool),
|
|
|
|
|
Menu(u32),
|
|
|
|
|
Key(key::Event),
|
|
|
|
|
Mouse{
|
|
|
|
|
x: i16,
|
|
|
|
|
y: i16,
|
|
|
|
|
},
|
|
|
|
|
Mouse { x: i16, y: i16 },
|
|
|
|
|
Size,
|
|
|
|
|
Left,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const ALT_KEYS: u32 = 0x0002 | 0x0001;
|
|
|
|
@ -221,7 +274,7 @@ impl From<Console::INPUT_RECORD> for Event {
|
|
|
|
|
// pub dwButtonState: u32,
|
|
|
|
|
// pub dwControlKeyState: u32,
|
|
|
|
|
// pub dwEventFlags: u32,
|
|
|
|
|
Event::Mouse{
|
|
|
|
|
Event::Mouse {
|
|
|
|
|
x: event.dwMousePosition.X,
|
|
|
|
|
y: event.dwMousePosition.Y,
|
|
|
|
|
}
|
|
|
|
|