@@ -28,10 +28,14 @@ import (
2828 "unsafe"
2929)
3030
31- // VKeyboard represents a virtual keyboard device. There are several
31+ // VKeyboard represents a virtual keyboard device. There are several
3232// methods available to work with this virtual device. Devices can be
3333// created, receive events, and closed.
3434type VKeyboard struct {
35+ // The Name of the uinput device. Will be trimmed to a Max Length of 80 bytes.
36+ // If left blank the device will have a default name.
37+ Name string
38+
3539 id int
3640}
3741
@@ -41,7 +45,8 @@ type VKeyboard struct {
4145func (vk * VKeyboard ) Create (path string ) (err error ) {
4246 vk .id = - 1
4347 var ret error
44- vk .id , ret = createVKeyboardDevice (path )
48+
49+ vk .id , ret = createVKeyboardDevice (path , vk .Name )
4550 return ret
4651}
4752
@@ -65,22 +70,29 @@ func (vk *VKeyboard) SendKeyRelease(key int) (err error) {
6570 return sendBtnEvent (vk .id , key , 0 )
6671}
6772
68- // Close will close the device and free resources.
73+ // Close will close the device and free resources.
6974// It's usually a good idea to use defer to call this function.
70- func (vk * VKeyboard ) Close () (err error ) {
75+ func (vk * VKeyboard ) Close () (err error ) {
7176 if vk .id < 0 {
7277 return errors .New ("Keyboard not initialized. Closing device failed." )
7378 }
7479 return closeDevice (vk .id )
7580}
7681
77- func createVKeyboardDevice (path string ) (deviceId int , err error ) {
78- var fd C.int
79- var deviceName = C .CString (path )
80- defer C .free (unsafe .Pointer (deviceName ))
82+ func createVKeyboardDevice (path , name string ) (deviceId int , err error ) {
83+ uinputDevice := C .CString (path )
84+ defer C .free (unsafe .Pointer (uinputDevice ))
8185
82- fd = C .initVKeyboardDevice (deviceName )
86+ if name == "" {
87+ name = "uinput_default_vkeyboard"
88+ }
89+ virtDeviceName := C .CString (name )
90+ defer C .free (unsafe .Pointer (virtDeviceName ))
91+
92+ var fd C.int
93+ fd = C .initVKeyboardDevice (uinputDevice , virtDeviceName )
8394 if fd < 0 {
95+ // TODO: Map ErrValues into more specific Errors
8496 return 0 , errors .New ("Could not initialize device." )
8597 }
8698
0 commit comments