From fdd6f63cb696cc5796ee167c93266de350620ae4 Mon Sep 17 00:00:00 2001 From: Benjamin Dahlmanns Date: Fri, 30 Mar 2018 13:42:46 +0200 Subject: [PATCH] Add press and release functions to touch pad device --- uinput.go | 67 ++++++++++++++++++++++++++++++++++++++++++++++++++ uinput_test.go | 20 +++++++++++++++ 2 files changed, 87 insertions(+) diff --git a/uinput.go b/uinput.go index 17574b4..2c341d0 100644 --- a/uinput.go +++ b/uinput.go @@ -162,6 +162,20 @@ type TouchPad interface { // RightClick will issue a right click. RightClick() error + // LeftPress will simulate a press of the left mouse button. Note that the button will not be released until + // LeftRelease is invoked. + LeftPress() error + + // LeftRelease will simulate the release of the left mouse button. + LeftRelease() error + + // RightPress will simulate the press of the right mouse button. Note that the button will not be released until + // RightRelease is invoked. + RightPress() error + + // RightRelease will simulate the release of the right mouse button. + RightRelease() error + io.Closer } @@ -227,6 +241,59 @@ func (vTouch vTouchPad) RightClick() error { } return nil } +// LeftPress will simulate a press of the left mouse button. Note that the button will not be released until +// LeftRelease is invoked. +func (vTouch vTouchPad) LeftPress() error { + err := sendBtnEvent(vTouch.deviceFile, evBtnLeft, btnStatePressed) + if err != nil { + return fmt.Errorf("Failed press the left mouse button: %v", err) + } + err = syncEvents(vTouch.deviceFile) + if err != nil { + return fmt.Errorf("sync to device file failed: %v", err) + } + return nil +} + +// LeftRelease will simulate the release of the left mouse button. +func (vTouch vTouchPad) LeftRelease() error { + err := sendBtnEvent(vTouch.deviceFile, evBtnLeft, btnStateReleased) + if err != nil { + return fmt.Errorf("Failed to release the left mouse button: %v", err) + } + err = syncEvents(vTouch.deviceFile) + if err != nil { + return fmt.Errorf("sync to device file failed: %v", err) + } + return nil +} + +// RightPress will simulate the press of the right mouse button. Note that the button will not be released until +// RightRelease is invoked. +func (vRel vTouchPad) RightPress() error { + err := sendBtnEvent(vRel.deviceFile, evBtnRight, btnStatePressed) + if err != nil { + return fmt.Errorf("Failed to press the right mouse button: %v", err) + } + err = syncEvents(vRel.deviceFile) + if err != nil { + return fmt.Errorf("sync to device file failed: %v", err) + } + return nil +} + +// RightRelease will simulate the release of the right mouse button. +func (vTouch vTouchPad) RightRelease() error { + err := sendBtnEvent(vTouch.deviceFile, evBtnRight, btnStateReleased) + if err != nil { + return fmt.Errorf("Failed to release the right mouse button: %v", err) + } + err = syncEvents(vTouch.deviceFile) + if err != nil { + return fmt.Errorf("sync to device file failed: %v", err) + } + return nil +} func (vTouch vTouchPad) Close() error { return closeDevice(vTouch.deviceFile) diff --git a/uinput_test.go b/uinput_test.go index de37d9e..527224f 100644 --- a/uinput_test.go +++ b/uinput_test.go @@ -132,6 +132,26 @@ func TestBasicTouchPadMoves(t *testing.T) { t.Fatalf("Failed to perform right click. Last error was: %s\n", err) } + err = absDev.LeftPress() + if err != nil { + t.Fatalf("Failed to perform left key press. Last error was: %s\n", err) + } + + err = absDev.LeftRelease() + if err != nil { + t.Fatalf("Failed to perform left key release. Last error was: %s\n", err) + } + + err = absDev.RightPress() + if err != nil { + t.Fatalf("Failed to perform right key press. Last error was: %s\n", err) + } + + err = absDev.RightRelease() + if err != nil { + t.Fatalf("Failed to perform right key release. Last error was: %s\n", err) + } + err = absDev.Close() if err != nil { t.Fatalf("Failed to close device. Last error was: %s\n", err)