Return an Event instance. Returns None if |block| is false and there is no event pending, otherwise waits for the completion of an event.
(self, block: bool = True)
| 422 | return rec, read.value |
| 423 | |
| 424 | def get_event(self, block: bool = True) -> Event | None: |
| 425 | """Return an Event instance. Returns None if |block| is false |
| 426 | and there is no event pending, otherwise waits for the |
| 427 | completion of an event.""" |
| 428 | |
| 429 | if not block and not self.wait(timeout=0): |
| 430 | return None |
| 431 | |
| 432 | while self.event_queue.empty(): |
| 433 | rec = self._read_input() |
| 434 | if rec is None: |
| 435 | return None |
| 436 | |
| 437 | if rec.EventType == WINDOW_BUFFER_SIZE_EVENT: |
| 438 | return Event("resize", "") |
| 439 | |
| 440 | if rec.EventType != KEY_EVENT or not rec.Event.KeyEvent.bKeyDown: |
| 441 | # Only process keys and keydown events |
| 442 | if block: |
| 443 | continue |
| 444 | return None |
| 445 | |
| 446 | key_event = rec.Event.KeyEvent |
| 447 | raw_key = key = key_event.uChar.UnicodeChar |
| 448 | |
| 449 | if key == "\r": |
| 450 | # Make enter unix-like |
| 451 | return Event(evt="key", data="\n") |
| 452 | elif key_event.wVirtualKeyCode == 8: |
| 453 | # Turn backspace directly into the command |
| 454 | key = "backspace" |
| 455 | elif key == "\x00": |
| 456 | # Handle special keys like arrow keys and translate them into the appropriate command |
| 457 | key = VK_MAP.get(key_event.wVirtualKeyCode) |
| 458 | if key: |
| 459 | if key_event.dwControlKeyState & CTRL_ACTIVE: |
| 460 | key = f"ctrl {key}" |
| 461 | elif key_event.dwControlKeyState & ALT_ACTIVE: |
| 462 | # queue the key, return the meta command |
| 463 | self.event_queue.insert(Event(evt="key", data=key)) |
| 464 | return Event(evt="key", data="\033") # keymap.py uses this for meta |
| 465 | return Event(evt="key", data=key) |
| 466 | if block: |
| 467 | continue |
| 468 | |
| 469 | return None |
| 470 | elif self.__vt_support: |
| 471 | # If virtual terminal is enabled, scanning VT sequences |
| 472 | for char in raw_key.encode(self.event_queue.encoding, "replace"): |
| 473 | self.event_queue.push(char) |
| 474 | continue |
| 475 | |
| 476 | if key_event.dwControlKeyState & ALT_ACTIVE: |
| 477 | # Do not swallow characters that have been entered via AltGr: |
| 478 | # Windows internally converts AltGr to CTRL+ALT, see |
| 479 | # https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-vkkeyscanw |
| 480 | if not key_event.dwControlKeyState & CTRL_ACTIVE: |
| 481 | # queue the key, return the meta command |