@@ -318,13 +318,13 @@ def _is_paste(keys) -> bool:
318
318
319
319
return newline_count >= 1 and text_count > 1
320
320
321
- def _event_to_key_presses (self , ev ) :
321
+ def _event_to_key_presses (self , ev : KEY_EVENT_RECORD ) -> List [ KeyPress ] :
322
322
"""
323
323
For this `KEY_EVENT_RECORD`, return a list of `KeyPress` instances.
324
324
"""
325
325
assert type (ev ) == KEY_EVENT_RECORD and ev .KeyDown
326
326
327
- result = None
327
+ result : Optional [ KeyPress ] = None
328
328
329
329
u_char = ev .uChar .UnicodeChar
330
330
ascii_char = u_char .encode ("utf-8" )
@@ -356,7 +356,7 @@ def _event_to_key_presses(self, ev):
356
356
and ev .ControlKeyState & self .SHIFT_PRESSED
357
357
and result
358
358
):
359
- result . key = {
359
+ mapping : Dict [ str , str ] = {
360
360
Keys .Left : Keys .ControlShiftLeft ,
361
361
Keys .Right : Keys .ControlShiftRight ,
362
362
Keys .Up : Keys .ControlShiftUp ,
@@ -366,14 +366,15 @@ def _event_to_key_presses(self, ev):
366
366
Keys .Insert : Keys .ControlShiftInsert ,
367
367
Keys .PageUp : Keys .ControlShiftPageUp ,
368
368
Keys .PageDown : Keys .ControlShiftPageDown ,
369
- }.get (result .key , result .key )
369
+ }
370
+ result .key = mapping .get (result .key , result .key )
370
371
371
372
# Correctly handle Control-Arrow/Home/End and Control-Insert keys.
372
373
if (
373
374
ev .ControlKeyState & self .LEFT_CTRL_PRESSED
374
375
or ev .ControlKeyState & self .RIGHT_CTRL_PRESSED
375
376
) and result :
376
- result . key = {
377
+ mapping = {
377
378
Keys .Left : Keys .ControlLeft ,
378
379
Keys .Right : Keys .ControlRight ,
379
380
Keys .Up : Keys .ControlUp ,
@@ -383,12 +384,13 @@ def _event_to_key_presses(self, ev):
383
384
Keys .Insert : Keys .ControlInsert ,
384
385
Keys .PageUp : Keys .ControlPageUp ,
385
386
Keys .PageDown : Keys .ControlPageDown ,
386
- }.get (result .key , result .key )
387
+ }
388
+ result .key = mapping .get (result .key , result .key )
387
389
388
390
# Turn 'Tab' into 'BackTab' when shift was pressed.
389
391
# Also handle other shift-key combination
390
392
if ev .ControlKeyState & self .SHIFT_PRESSED and result :
391
- result . key = {
393
+ mapping = {
392
394
Keys .Tab : Keys .BackTab ,
393
395
Keys .Left : Keys .ShiftLeft ,
394
396
Keys .Right : Keys .ShiftRight ,
@@ -399,7 +401,8 @@ def _event_to_key_presses(self, ev):
399
401
Keys .Insert : Keys .ShiftInsert ,
400
402
Keys .PageUp : Keys .ShiftPageUp ,
401
403
Keys .PageDown : Keys .ShiftPageDown ,
402
- }.get (result .key , result .key )
404
+ }
405
+ result .key = mapping .get (result .key , result .key )
403
406
404
407
# Turn 'Space' into 'ControlSpace' when control was pressed.
405
408
if (
@@ -443,23 +446,42 @@ def _event_to_key_presses(self, ev):
443
446
else :
444
447
return []
445
448
446
- def _handle_mouse (self , ev ) :
449
+ def _handle_mouse (self , ev : MOUSE_EVENT_RECORD ) -> List [ KeyPress ] :
447
450
"""
448
451
Handle mouse events. Return a list of KeyPress instances.
449
452
"""
450
453
FROM_LEFT_1ST_BUTTON_PRESSED = 0x1
454
+ MOUSE_MOVED = 0x0001
455
+ MOUSE_WHEELED = 0x0004
451
456
452
457
result = []
458
+ event_type : Optional [MouseEventType ] = None
453
459
454
- # Check event type.
455
- if ev .ButtonState == FROM_LEFT_1ST_BUTTON_PRESSED :
456
- # On a key press, generate both the mouse down and up event.
457
- for event_type in [MouseEventType .MOUSE_DOWN , MouseEventType .MOUSE_UP ]:
458
- data = ";" .join (
459
- [event_type .value , str (ev .MousePosition .X ), str (ev .MousePosition .Y )]
460
- )
461
- result .append (KeyPress (Keys .WindowsMouseEvent , data ))
460
+ # Move events.
461
+ if ev .EventFlags & MOUSE_MOVED :
462
+ if ev .ButtonState == FROM_LEFT_1ST_BUTTON_PRESSED :
463
+ event_type = MouseEventType .MOUSE_DOWN_MOVE
462
464
465
+ # Scroll events.
466
+ elif ev .EventFlags & MOUSE_WHEELED :
467
+ if ev .ButtonState > 0 :
468
+ event_type = MouseEventType .SCROLL_UP
469
+ else :
470
+ event_type = MouseEventType .SCROLL_DOWN
471
+
472
+ # Mouse down (left button).
473
+ elif ev .ButtonState == FROM_LEFT_1ST_BUTTON_PRESSED :
474
+ event_type = MouseEventType .MOUSE_DOWN
475
+
476
+ # No key pressed anymore: mouse up.
477
+ else :
478
+ event_type = MouseEventType .MOUSE_UP
479
+
480
+ if event_type is not None :
481
+ data = ";" .join (
482
+ [event_type .value , str (ev .MousePosition .X ), str (ev .MousePosition .Y )]
483
+ )
484
+ result .append (KeyPress (Keys .WindowsMouseEvent , data ))
463
485
return result
464
486
465
487
0 commit comments