Skip to content

Commit

Permalink
Add key press and key release functions for the mouse device
Browse files Browse the repository at this point in the history
Move todos to README
  • Loading branch information
bendahl committed Mar 29, 2018
1 parent 487ec5a commit 99be8e7
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 19 deletions.
26 changes: 17 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@ Uinput [![Build Status](https://travis-ci.org/bendahl/uinput.svg?branch=master)]
This package provides pure go wrapper functions for the LINUX uinput device, which allows to create virtual input devices
in userspace. At the moment this package offers a virtual keyboard implementation as well as a virtual mouse device and
a touch pad device.

\
The keyboard can be used to either send single key presses or hold down a specified key and release it later
(useful for building game controllers). The mouse device issues relative positional change events to the x and y axis
of the mouse pointer and may also fire click events (left and right click). More functionality will be added in future
version.
of the mouse pointer and may also fire click events (left and right click). For implementing things like region selects
via a virtual mouse pointer, press and release functions for the mouse device are also included.

The touch pad, on the other hand can be used to move the mouse cursor to the specified position on the screen and to
issue left and right clicks. Note that you'll need to specify the region size of your screen first though (happens during
device creation).
Expand All @@ -31,11 +34,16 @@ License
--------
The package falls under the MIT license. Please see the "LICENSE" file for details.

ToDos
------------------
All testing has been done on Ubunu 14.04 and 16.04 x86\_64.
Testing for other platforms will need to be done.
To get an idea of the things that are on the current todo list, check out the file "TODO.md".
As always, helpful comments and ideas are always welcome.
Feel free to do some testing on your own if you're up to it.
TODO
----
Most functionality is implemented. However, more testing (including testing on different target
platforms) is definitely an area for improvement.

- [x] Create Tests for the uinput package
- [x] Migrate code from C to GO
- [x] Implement relative input
- [x] Implement absolute input
- [ ] Test on different platforms (besides x86_64)
- [x] Implement functions to allow mouse button up and down events (for region selects)
- [ ] Extend test cases

10 changes: 0 additions & 10 deletions TODO.md

This file was deleted.

68 changes: 68 additions & 0 deletions uinput.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,20 @@ type Mouse 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
}

Expand Down Expand Up @@ -294,6 +308,60 @@ func (vRel vMouse) 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 (vRel vMouse) LeftPress() error {
err := sendBtnEvent(vRel.deviceFile, evBtnLeft, btnStatePressed)
if err != nil {
return fmt.Errorf("Failed press the left mouse button: %v", err)
}
err = syncEvents(vRel.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 (vRel vMouse) LeftRelease() error {
err := sendBtnEvent(vRel.deviceFile, evBtnLeft, btnStateReleased)
if err != nil {
return fmt.Errorf("Failed to release the left mouse button: %v", err)
}
err = syncEvents(vRel.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 vMouse) 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 (vRel vMouse) RightRelease() error {
err := sendBtnEvent(vRel.deviceFile, evBtnRight, btnStateReleased)
if err != nil {
return fmt.Errorf("Failed to release 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
}

// Close closes the device and releases the device.
func (vRel vMouse) Close() error {
return closeDevice(vRel.deviceFile)
Expand Down
20 changes: 20 additions & 0 deletions uinput_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,26 @@ func TestBasicMouseMoves(t *testing.T) {
t.Fatalf("Failed to perform right click. Last error was: %s\n", err)
}

err = relDev.LeftPress()
if err != nil {
t.Fatalf("Failed to perform left key press. Last error was: %s\n", err)
}

err = relDev.LeftRelease()
if err != nil {
t.Fatalf("Failed to perform left key release. Last error was: %s\n", err)
}

err = relDev.RightPress()
if err != nil {
t.Fatalf("Failed to perform right key press. Last error was: %s\n", err)
}

err = relDev.RightRelease()
if err != nil {
t.Fatalf("Failed to perform right key release. Last error was: %s\n", err)
}

err = relDev.Close()
if err != nil {
t.Fatalf("Failed to close device. Last error was: %s\n", err)
Expand Down

0 comments on commit 99be8e7

Please sign in to comment.