Keyboard input?

How do I read keyboard input with pancurses?

The README has an example using Window::getch.

Thanks! But I have a few questions:
What does



What does

Some(input) => { window.addstr(&format!("{:?}", input)); },


Some is the name of an Option variant. Option is defined as such:

pub enum Option<T> {

Which means every Option is either Some(value) or None.

That's part of this match block:

match window.getch() {
    Some(Input::Character(c)) => { window.addch(c); },
    Some(Input::KeyDC) => break,
    Some(input) => { window.addstr(&format!("{:?}", input)); },
    None => ()
  • If window.getch() returns Some(Input::Character(c)) for some value of c, it'll call window.addch(c).
  • If it gets Some(Input::KeyDC) then it'll break out of the loop.
  • If it gets any other Some(x) for any x (which they name input), the call the code in that branch.
  • It does nothing if it returns None.

I see, thanks! BTW, I like your name @OptimisticPeach

1 Like

One more question, how do I check if KeyLeft was pressed with an if statement?

I believe that the value is Input::KeySLeft, so to do this you'd need either an if or if let:

let key = window.getch().unwrap();
if key == Input::KeySLeft { /* */ }

// Or

if let Input::KeySLeft = key { /* */ }

Where the if let statement lets you pattern match like a match statement would, whereas just the `if| can only test for one specific case.

What do I put in key { }?

The braces are the body of the if-statement, put the code you want to run when the key is pressed in there. key is part of the test expression.

Oh, I get it now. Sorry, I didn’t see that.

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.