@@ -64,6 +64,8 @@ def __init__(self, err: int | None, descr: str | None = None) -> None:
64
64
if TYPE_CHECKING :
65
65
from typing import IO
66
66
67
+ INPUT_BUFFER_LEN = 10 * 1024
68
+
67
69
# Virtual-Key Codes: https://learn.microsoft.com/en-us/windows/win32/inputdev/virtual-key-codes
68
70
VK_MAP : dict [int , str ] = {
69
71
0x23 : "end" , # VK_END
@@ -165,6 +167,10 @@ def __init__(
165
167
# Console I/O is redirected, fallback...
166
168
self .out = None
167
169
170
+ self .input_buffer = (INPUT_RECORD * INPUT_BUFFER_LEN )()
171
+ self .input_buffer_pos = 0
172
+ self .input_buffer_count = 0
173
+
168
174
def refresh (self , screen : list [str ], c_xy : tuple [int , int ]) -> None :
169
175
"""
170
176
Refresh the console screen.
@@ -415,16 +421,27 @@ def _getscrollbacksize(self) -> int:
415
421
416
422
return info .srWindow .Bottom # type: ignore[no-any-return]
417
423
424
+ def more_in_buffer (self ) -> bool :
425
+ return self .input_buffer_pos < self .input_buffer_count - 1
426
+
418
427
def _read_input (self , block : bool = True ) -> INPUT_RECORD | None :
419
428
if not block and not self .wait (timeout = 0 ):
420
429
return None
421
430
422
- rec = INPUT_RECORD ()
431
+ if self .more_in_buffer ():
432
+ self .input_buffer_pos += 1
433
+ return self .input_buffer [self .input_buffer_pos ]
434
+
435
+ # read next chunk
436
+ self .input_buffer_pos = 0
437
+ self .input_buffer_count = 0
423
438
read = DWORD ()
424
- if not ReadConsoleInput (InHandle , rec , 1 , read ):
439
+
440
+ if not ReadConsoleInput (InHandle , self .input_buffer , INPUT_BUFFER_LEN , read ):
425
441
raise WinError (GetLastError ())
426
442
427
- return rec
443
+ self .input_buffer_count = read .value
444
+ return self .input_buffer [0 ]
428
445
429
446
def get_event (self , block : bool = True ) -> Event | None :
430
447
"""Return an Event instance. Returns None if |block| is false
@@ -521,9 +538,14 @@ def forgetinput(self) -> None:
521
538
def getpending (self ) -> Event :
522
539
"""Return the characters that have been typed but not yet
523
540
processed."""
524
- return Event ("key" , "" , b"" )
541
+ e = Event ("key" , "" , b"" )
542
+
543
+ while not self .event_queue .empty ():
544
+ e2 = self .event_queue .get ()
545
+ e .data += e2 .data
546
+ e .raw += e .raw
525
547
526
- def wait (self , timeout : float | None ) -> bool :
548
+ def wait_for_event (self , timeout : float | None ) -> bool :
527
549
"""Wait for an event."""
528
550
if timeout is None :
529
551
timeout = INFINITE
@@ -536,6 +558,16 @@ def wait(self, timeout: float | None) -> bool:
536
558
return False
537
559
return True
538
560
561
+ def wait (self , timeout : float | None = None ) -> bool :
562
+ """
563
+ Wait for events on the console.
564
+ """
565
+ return (
566
+ not self .event_queue .empty ()
567
+ or self .more_in_buffer ()
568
+ or self .wait_for_event (timeout )
569
+ )
570
+
539
571
def repaint (self ) -> None :
540
572
raise NotImplementedError ("No repaint support" )
541
573
0 commit comments