How to resolve the algorithm Keyboard input/Obtain a Y or N response step by step in the V (Vlang) programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Keyboard input/Obtain a Y or N response step by step in the V (Vlang) programming language
Table of Contents
Problem Statement
Obtain a valid Y or N response from the keyboard. The keyboard should be flushed, so that any outstanding key-presses are removed, preventing any existing Y or N key-press from being evaluated. The response should be obtained as soon as Y or N are pressed, and there should be no need to press an enter key.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Keyboard input/Obtain a Y or N response step by step in the V (Vlang) programming language
Source code in the v programming language
import term.ui as tui
struct App {
mut:
tui &tui.Context = 0
}
fn event(e &tui.Event, x voidptr) {
mut app := &App(x)
app.tui.clear()
app.tui.set_cursor_position(0, 0)
app.tui.write('V term.input event viewer (type `y`, `Y`, `n`, or `N` to exit)\n\n')
if e.typ == .key_down {
mut cap := ''
if !e.modifiers.is_empty() && e.modifiers.has(.shift) {
cap = 'capital'
}
match e.code {
.y {
app.tui.write('You typed $cap y')
}
.n {
app.tui.write('You typed $cap n')
}
else {
app.tui.write("You didn't type n or y")
}
}
}
app.tui.flush()
if e.typ == .key_down && (e.code == .y || e.code==.n) {
exit(0)
}
}
fn main() {
mut app := &App{}
app.tui = tui.init(
user_data: app
event_fn: event
window_title: 'V term.ui event viewer'
hide_cursor: true
capture_events: true
frame_rate: 60
use_alternate_buffer: false
)
println('V term.input event viewer (type `y`, `Y`, `n`, or `N` to exit)\n\n')
app.tui.run()?
}
You may also check:How to resolve the algorithm Literals/String step by step in the Lingo programming language
You may also check:How to resolve the algorithm Doubly-linked list/Traversal step by step in the ALGOL 68 programming language
You may also check:How to resolve the algorithm Rosetta Code/Rank languages by popularity step by step in the Run BASIC programming language
You may also check:How to resolve the algorithm CSV to HTML translation step by step in the Bracmat programming language
You may also check:How to resolve the algorithm Image noise step by step in the Scala programming language