Skip to content

Commit

Permalink
feat(mouse): add shift press support
Browse files Browse the repository at this point in the history
Support parsing shift key press
  • Loading branch information
aymanbagabas committed Jan 25, 2023
1 parent d2a775e commit 93bec55
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 11 deletions.
23 changes: 12 additions & 11 deletions mouse.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
}
Expand Down Expand Up @@ -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
Expand Down
27 changes: 27 additions & 0 deletions mouse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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{
Expand Down

0 comments on commit 93bec55

Please sign in to comment.