From 93bec55da27bc73bd4c4a94bc7d153f7d74876b2 Mon Sep 17 00:00:00 2001 From: Ayman Bagabas Date: Thu, 10 Nov 2022 10:31:29 -0500 Subject: [PATCH] feat(mouse): add shift press support Support parsing shift key press --- mouse.go | 23 ++++++++++++----------- mouse_test.go | 27 +++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 11 deletions(-) diff --git a/mouse.go b/mouse.go index 5ff3a2faf0..1b712d12b9 100644 --- a/mouse.go +++ b/mouse.go @@ -13,11 +13,12 @@ type MouseMsg MouseEvent // MouseEvent represents a mouse event, which could be a click, a scroll wheel // movement, a cursor movement, or a combination. type MouseEvent struct { - X int - Y int - Type MouseEventType - Alt bool - Ctrl bool + X int + Y int + Type MouseEventType + Shift bool + Alt bool + Ctrl bool } // String returns a string representation of a mouse event. @@ -28,6 +29,9 @@ func (m MouseEvent) String() (s string) { if m.Alt { s += "alt+" } + if m.Shift { + s += "shift+" + } s += mouseEventTypes[m.Type] return s } @@ -131,12 +135,9 @@ func parseX10MouseEvents(buf []byte) ([]MouseEvent, error) { } } - if e&bitAlt != 0 { - m.Alt = true - } - if e&bitCtrl != 0 { - m.Ctrl = true - } + m.Shift = e&bitShift != 0 + m.Alt = e&bitAlt != 0 + m.Ctrl = e&bitCtrl != 0 // (1,1) is the upper left. We subtract 1 to normalize it to (0,0). m.X = int(v[1]) - byteOffset - 1 diff --git a/mouse_test.go b/mouse_test.go index d9c108f2f7..8aac7086bf 100644 --- a/mouse_test.go +++ b/mouse_test.go @@ -48,6 +48,23 @@ func TestMouseEvent_String(t *testing.T) { event: MouseEvent{Type: MouseMotion}, expected: "motion", }, + { + name: "shift+left", + event: MouseEvent{ + Type: MouseLeft, + Shift: true, + }, + expected: "shift+left", + }, + { + name: "ctrl+shift+left", + event: MouseEvent{ + Type: MouseLeft, + Shift: true, + Ctrl: true, + }, + expected: "ctrl+shift+left", + }, { name: "alt+left", event: MouseEvent{ @@ -73,6 +90,16 @@ func TestMouseEvent_String(t *testing.T) { }, expected: "ctrl+alt+left", }, + { + name: "ctrl+alt+shift+left", + event: MouseEvent{ + Type: MouseLeft, + Alt: true, + Ctrl: true, + Shift: true, + }, + expected: "ctrl+alt+shift+left", + }, { name: "ignore coordinates", event: MouseEvent{