Preview Ruby 4.1 is a preview. It can change before the final release. Switch to Ruby 4.0 →
IO#console_input_events
static VALUE
console_input_events(int argc, VALUE *argv, VALUE io)
{
VALUE vmax = Qnil, vopts = Qnil, vtimeout = Qundef;
VALUE values[1];
ID keywords[1] = {id_timeout};
DWORD max_events = 1;
read_console_input_args_t args;
rb_scan_args(argc, argv, "01:", &vmax, &vopts);
if (rb_get_kwargs(vopts, keywords, 0, 1, values)) {
vtimeout = values[0];
}
if (!NIL_P(vmax)) {
max_events = NUM2UINT(vmax);
if (max_events == 0) rb_raise(rb_eArgError, "max_events must be positive");
}
args.timeout = INFINITE;
if (!NIL_OR_UNDEF_P(vtimeout)) {
struct timeval timeout = rb_time_interval(vtimeout);
uint64_t milliseconds = (uint64_t)timeout.tv_sec * 1000;
milliseconds += ((uint64_t)timeout.tv_usec + 999) / 1000;
args.timeout = milliseconds < INFINITE ? (DWORD)milliseconds : INFINITE - 1;
}
args.handles[console_input_handle] =
(HANDLE)rb_w32_get_osfhandle(GetReadFD(io));
args.records = ALLOC_N(INPUT_RECORD, max_events);
args.handles[console_input_wakeup] = CreateEvent(NULL, FALSE, FALSE, NULL);
if (!args.handles[console_input_wakeup]) {
int error = LAST_ERROR;
xfree(args.records);
rb_syserr_fail(error, 0);
}
args.length = max_events;
args.count = 0;
args.wait_result = WAIT_FAILED;
args.error = ERROR_SUCCESS;
args.result = FALSE;
return rb_ensure(console_input_events_read, (VALUE)&args,
console_input_events_ensure, (VALUE)&args);
}
Reads up to max_events console input events, preserving their order. The default is one event. Blocks until at least one event is available, or for timeout seconds if specified. Returns an empty Array on timeout.
Each event is returned as a Hash. The :type and remaining keys are:
-
:key::key_down,:repeat_count,:virtual_key_code,:virtual_scan_code,:unicode_char, and:control_key_state. -
:mouse::position([row, column]),:button_state,:control_key_state, and:event_flags. -
:window_buffer_size::size([rows, columns]). -
:menu::command_id. -
:focus::set_focus.
This method is Windows only.
You must require ‘io/console’ to use this method.