Right arrow key won't register unless I press it twice

Hi!
I've been working on a TUI and I'm trying to make a bool true if you press the right arrow key (this is to switch to a different menu), but for some reason, I have to press the right arrow key twice.

Full code:

extern crate termion;
 use std::io::{stdin, stdout, Write};
use termion::event::Key;
use termion::input::TermRead;
use termion::raw::IntoRawMode;
 fn main() {
let stdin = stdin();
let mut stdout = stdout().into_raw_mode().unwrap();
struct Area {
selection: i32,
menu: i32,
go: bool,
}
let mut area = Area {
selection: 0,
menu: 0,
go: false,
};
for c in stdin.keys() {
match c.unwrap() {
Key::Ctrl('q') => break,
Key::Down if area.selection > 0 => area.selection -= 1,
Key::Up if area.selection < 2 => area.selection += 1,
Key::Right => area.go = true,
_ => (),
};
if area.menu == 0 {
println!(
"{}{}FIRST TIME SETUP {}",
termion::cursor::Goto(4, 2),
termion::clear::All,
termion::cursor::Hide,
);
  println!(
"{}CONFIGURATION {}",
termion::cursor::Goto(4, 4),
termion::cursor::Hide
);
println!(
"{}RUN AIO-BACKUP {}",
termion::cursor::Goto(4, 6),
termion::cursor::Hide
);
match area.selection {
2 => {
println!(
"{}---> {}",
termion::cursor::Goto(21, 2),
termion::cursor::Hide
);
if area.go == true {
area.menu = 1;
}
}
1 => {
println!(
"{}---> {}",
termion::cursor::Goto(21, 4),
termion::cursor::Hide
);
if area.go == true {
area.go = false;
area.menu = 2;
}
}
0 => {
println!(
"{}---> {}",
termion::cursor::Goto(21, 6),
termion::cursor::Hide
);
if area.go == true {
area.go = false;
area.menu = 3;
}
}
_ => (),
}
} else if area.menu == 1 {
println!( }"{}{}Test, menu one {}",
termion::clear::All,
termion::cursor::Goto(4, 2),
termion::cursor::Hide,
);
}
stdout.flush().unwrap();
write!(stdout, "{}", termion::cursor::Show).unwrap();
}


And just so you know, I haven't finished all the menus yet, that's why most of the time it just leads to an empty menu :stuck_out_tongue:

I think you need to remove the else on this line:

} else if area.menu == 1 {

Working code: Rust Playground

Oh, Thank you very much @mbrubeck

Looking closer at the code, I think you'll ultimately want to restructure your loop to do things in this order:

loop {
    // 1. Draw the screen based on the current state.
    // 2. Get the next key.
    // 3. Update the state depending on which key was pressed.
}

Hmm, ok

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.