Detect Mouse plug/unplug

A

Alessandro

Hi to all

I develop with VB.NET 2005 on WinCE 6.0 platform. By default the USB
mouse is not plugged and the cursor is hidden. I want to show the
cursor when the mouse is plugged and hide the cursor when the mouse is
unplugged.
To show/hide the cursor I can use the Cursor.Show and Cursor.Hide
methods. To detect plug/unplug I have tried to use the
OpenNETCF.WindowsCE.DeviceStatusMonitor class. This is my code:

Public Class frmMain

Dim WithEvents monitor As OpenNETCF.WindowsCE.DeviceStatusMonitor
= New OpenNETCF.WindowsCE.DeviceStatusMonitor(DeviceClass.Any, False)
Delegate Sub AddMessage(ByVal msg As String)
Private myDelegate As AddMessage = New AddMessage(AddressOf
ShowMessage)

Private Sub StatusChanged(ByVal sender As Object, ByVal e As
DeviceNotificationArgs) Handles monitor.DeviceNotification
Dim message As String = String.Format("Device '{0}' has been
{1}.", e.DeviceName, IIf(e.DeviceAttached, "inserted",
"removed").ToString)
message &= vbCrLf & "Device Class = " & e.DeviceClass.ToString
& " - DeviceInterfaceGUID = " & e.DeviceInterfaceGUID.ToString
Invoke(myDelegate, message)
End Sub

Private Sub ShowMessage(ByVal msg As String)
txtMsg.Text &= msg & vbCrLf
End Sub

Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles btnStart.Click
If monitor.Active Then Exit Sub
monitor.StartStatusMonitoring()
lblMonitor.Text = "--> M O N I T O R I N G <--"
End Sub

Private Sub btnStop_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles btnStop.Click
If Not monitor.Active Then Exit Sub
monitor.StopStatusMonitoring()
lblMonitor.Text = "--> S T O P P E D <--"
End Sub

Private Sub frmMain_Closing(ByVal sender As Object, ByVal e As
System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
monitor.Dispose()
End Sub

End Class


When I plug the USB mouse I receive this message:

"Device 'HID1:' has been inserted.
Device Class = StreamDevice - DeviceInterfaceGUID =
f8a6ba98-087a-43ac-a9d8-b7f13c5bae31

BUT if i plug an USB keyboard before the USB mouse I receive two
message for the keyboard and no message when I plug the mouse. The two
message (keyboard) are:

Device 'HID1:' has been inserted.
Device Class = StreamDevice - DeviceInterfaceGUID =
f8a6ba98-087a-43ac-a9d8-b7f13c5bae31
Device 'KBD1:' has been inserted.
Device Class = Keyboard - DeviceInterfaceGUID = cbe6ddf2-
f5d4-4e16-9f61-4ccc0b6695f3

How can I detect the connection/disconnection of a USB mouse WITHOUT
modify the mouse driver ?
(I don't program with C-language and it is difficult for me to modify
the driver).

Thankyou very much!
Ale
 
P

Paul G. Tobey [eMVP]

I'd say that you probably can't. On our devices, we have a special class
that is sent via that notification for the mouse, specifically, being
inserted and removed.

It sounds to me like you should be saying something about what the actual
system behavior is. Does the device already handle showing and hiding the
cursor for you when a mouse is connected? It sounds like your answer is
"no". If that's the case, I'm not sure that Cursor.Show will do anything
for you. Give us a little more information about how the device actually
works, completely aside from your application, with respect to the mouse
cursor. That will probably tell us enough to suggest what you'll have to
do.

Paul T.
 
A

Alessandro

I'd say that you probably can't.  On our devices, we have a special class
that is sent via that notification for the mouse, specifically, being
inserted and removed.

It sounds to me like you should be saying something about what the actual
system behavior is. Does the device already handle showing and hiding the
cursor for you when a mouse is connected?  It sounds like your answer is
"no".  If that's the case, I'm not sure that Cursor.Show will do anything
for you.  Give us a little more information about how the device actually
works, completely aside from your application, with respect to the mouse
cursor.  That will probably tell us enough to suggest what you'll have to
do.

Paul T.

Hi Paul, thaks for your reply.

The mouse works fine and Cursor.Hide/Show works fine too.
I have a x86 device with some USB port. We have only one full screen
application.
The device have a touch screen.

I hope this informations help you to understand.
Thanks in advance!
Ale
 
P

Paul G. Tobey [eMVP]

Hi Paul, thaks for your reply.
The mouse works fine and Cursor.Hide/Show works fine too.
I have a x86 device with some USB port. We have only one full screen
application.
The device have a touch screen.

I hope this informations help you to understand.
Thanks in advance!
Ale

No, that made things worse. The mouse cursor automatically appears and
disappears on the screen when you plug the mouse in or remove the mouse? If
so, what's the problem? Seems like your work to show/hide the cursor when
the mouse is present/absent is already done.

I understand fine about the touchscreen. What I'm trying to figure out is
whether you are trying to implement a system-level behavior, showing and
hiding the mouse cursor as a mouse is dynamically added or removed, or
trying to do something else that is not a system-level behavior but which I
don't understand.

Paul T.
 
C

Chris Tacke, eMVP

That's certainly a system-level behavior that should come as a standard
behavior. If your device doesn't already do that, I'd be complaining the
the OEM's support channel.


--

Chris Tacke, Embedded MVP
OpenNETCF Consulting
Giving back to the embedded community
http://community.OpenNETCF.com


I'd say that you probably can't. On our devices, we have a special class
that is sent via that notification for the mouse, specifically, being
inserted and removed.

It sounds to me like you should be saying something about what the actual
system behavior is. Does the device already handle showing and hiding the
cursor for you when a mouse is connected? It sounds like your answer is
"no". If that's the case, I'm not sure that Cursor.Show will do anything
for you. Give us a little more information about how the device actually
works, completely aside from your application, with respect to the mouse
cursor. That will probably tell us enough to suggest what you'll have to
do.

Paul T.

Hi Paul, thaks for your reply.

The mouse works fine and Cursor.Hide/Show works fine too.
I have a x86 device with some USB port. We have only one full screen
application.
The device have a touch screen.

I hope this informations help you to understand.
Thanks in advance!
Ale
 
A

Alessandro

No, that made things worse.  Themousecursor automatically appears and
disappears on the screen when you plug themousein or remove themouse?  If
so, what's the problem?  Seems like your work to show/hide the cursor when
themouseis present/absent is already done.

I understand fine about the touchscreen.  What I'm trying to figure outis
whether you are trying to implement a system-level behavior, showing and
hiding themousecursor as amouseis dynamically added or removed, or
trying to do something else that is not a system-level behavior but whichI
don't understand.

Paul T.

The mouse cursor doesn't automatically appears and disappears. It is
always showed.
I need show the cursor only when mouse is plugged and i need hide the
cursor when the mouse is not plugged.
I dont need to implement a system-level behavior. The device runs
always the same application. Into this application i need execute
Cursor.Show when a usb mouse is plug-in and i need execute Cursor.Hide
when the mouse is detach/not present. But, (with my application in
VB.NET) how can I detect the connection/disconnection of a USB mouse ?
Like you say, i need "showing and hiding the mouse cursor as a mouse
is dynamically added or removed" but i dont know C language therefore
system-level implementation is not good for me.

Thaks again for your patience.
Ale
 
P

Paul G. Tobey [eMVP]

OK, then to really have it work "right", you'd want to change both the mouse
driver and the display driver, potentially. The display driver needs to
know itself whether there is a mouse attached and should not show the cursor
unless there is a mouse. The mouse driver should send a device notification
that indicates "Mouse" for attach and detach of the mouse. For this, I'd
recommend the following IClass value suggested by Steve Maillet a while
back:

#define DEVCLASS_MOUSE_GUID_STRING
TEXT("{FFBE5A05-87B4-4591-B067-CEBA6FAFD298}")
#define DEVCLASS_MOUSE_GUID { 0xFFBE5A05, 0x87B4, 0x4591, { 0xB0, 0x67,
0xCE, 0xBA, 0x6F, 0xAF, 0xD2, 0x98 } }

Call AdvertiseInterface on that value with TRUE or FALSE to indicate
connection/disconnect in the mouse driver.

You can then set up the display driver to, by default, not display the
cursor. The driver can create a thread to monitor for mouse connect and,
when it decides that there is a mouse connected, turn the cursor on.
Obviously, since you've already adjusted your application to look for the
mouse and since you are the only application ever visible on the screen, you
could bypass adjusting the display driver and just control the cursor for
your application. If any other application ever comes to the front, the
cursor will be ON, regardless of what you've done in your application,
though.

Paul T.

No, that made things worse. Themousecursor automatically appears and
disappears on the screen when you plug themousein or remove themouse? If
so, what's the problem? Seems like your work to show/hide the cursor when
themouseis present/absent is already done.

I understand fine about the touchscreen. What I'm trying to figure out is
whether you are trying to implement a system-level behavior, showing and
hiding themousecursor as amouseis dynamically added or removed, or
trying to do something else that is not a system-level behavior but which
I
don't understand.

Paul T.

The mouse cursor doesn't automatically appears and disappears. It is
always showed.
I need show the cursor only when mouse is plugged and i need hide the
cursor when the mouse is not plugged.
I dont need to implement a system-level behavior. The device runs
always the same application. Into this application i need execute
Cursor.Show when a usb mouse is plug-in and i need execute Cursor.Hide
when the mouse is detach/not present. But, (with my application in
VB.NET) how can I detect the connection/disconnection of a USB mouse ?
Like you say, i need "showing and hiding the mouse cursor as a mouse
is dynamically added or removed" but i dont know C language therefore
system-level implementation is not good for me.

Thaks again for your patience.
Ale
 
A

Alessandro

That's certainly a system-level behavior that should come as a standard
behavior.  If your device doesn't already do that, I'd be complaining the
the OEM's support channel.

--

Chris Tacke, Embedded MVP
OpenNETCF Consulting
Giving back to the embedded communityhttp://community.OpenNETCF.com







Hi Paul, thaks for your reply.

The mouse works fine and Cursor.Hide/Show works fine too.
I have a x86 device with some USB port. We have only one full screen
application.
The device have a touch screen.

I hope this informations help you to understand.
Thanks in advance!
Ale

Then, modify the mouse driver is the only way (I suppose the mouse
driver - MOUHID.DLL - must send a system message when mouse is plugged/
unplugged). Right ?
Thanks
Ale
 
A

Alessandro

OK, then to really have it work "right", you'd want to change both the mouse
driver and the display driver, potentially.  The display driver needs to
know itself whether there is a mouse attached and should not show the cursor
unless there is a mouse.  The mouse driver should send a device notification
that indicates "Mouse" for attach and detach of the mouse.  For this, I'd
recommend the following IClass value suggested by Steve Maillet a while
back:

#define DEVCLASS_MOUSE_GUID_STRING
TEXT("{FFBE5A05-87B4-4591-B067-CEBA6FAFD298}")
#define DEVCLASS_MOUSE_GUID   { 0xFFBE5A05, 0x87B4, 0x4591, { 0xB0, 0x67,
0xCE, 0xBA, 0x6F, 0xAF, 0xD2, 0x98 } }

Call AdvertiseInterface on that value with TRUE or FALSE to indicate
connection/disconnect in the mouse driver.

You can then set up the display driver to, by default, not display the
cursor.  The driver can create a thread to monitor for mouse connect and,
when it decides that there is a mouse connected, turn the cursor on.
Obviously, since you've already adjusted your application to look for the
mouse and since you are the only application ever visible on the screen, you
could bypass adjusting the display driver and just control the cursor for
your application.  If any other application ever comes to the front, the
cursor will be ON, regardless of what you've done in your application,
though.

Paul T.







The mouse cursor doesn't automatically appears and disappears. It is
always showed.
I need show the cursor only when mouse is plugged and i need hide the
cursor when the mouse is not plugged.
I dont need to implement a system-level behavior. The device runs
always the same application. Into this application i need execute
Cursor.Show when a usb mouse is plug-in and i need execute Cursor.Hide
when the mouse is detach/not present. But, (with my application in
VB.NET) how can I detect the connection/disconnection of a USB mouse ?
Like you say, i need "showing and hiding the mouse cursor as a mouse
is dynamically added or removed" but i dont know C language therefore
system-level implementation is not good for me.

Thaks again for your patience.
Ale

Thank Paul and Chris !

About Paul's reply:

I will bypass adjusting the display driver and I will just control the
cursor for
my application. Then I must modify only the mouse driver. But I
unfortunately need further help :-(
I use standard CEPC mouse and keyboard driver. Then where I can/must
insert the #Define and where i can/must call
the AdvertiseInterface ?

Thankyou very much
Ale
 
P

Paul G. Tobey [eMVP]

Well, you have to modify the MOUHID component, so you have to clone that and
put it in your platform. The information on how to do that is, to some
extent, in the help and you can find other documents on the topic online,
I'm sure. Somewhere near the top, you'll probably add an #include like
this:

// PGT: Get the GUID for the mouse device class that we will
// advertise when the mouse is detected.
#include <devclassmouse.h>

where that header file includes the information I gave you in the last
message. Then, assuming you are using the Microsoft driver, in the function
HIDDeviceAttach(), you'll need to add code like this:

// PGT: Notify the system that there is a new mouse device
// attaching. Advertise our custom GUID for the device,
// DEVCLASS_MOUSE_GUID.
DEBUGMSG( 1, ( TEXT( "USBMouse::HidDeviceAttach - Got a mouse. Advertise
it.\r\n" ) ) );

{
GUID devclass = DEVCLASS_MOUSE_GUID;
if ( AdvertiseInterface( &devclass, _T( "USBMouse" ), TRUE ) )
{
DEBUGMSG( 1, ( TEXT( "USBMouse::HidDeviceAttach - Successfully advertised
interface %s.\r\n" ), DEVCLASS_MOUSE_GUID_STRING ) );
}
else
{
DEBUGMSG( 1, ( TEXT( "USBMouse::HidDeviceAttach - AdvertiseInterface
failed %s. Err = 0x%x.\r\n" ), DEVCLASS_MOUSE_GUID_STRING,
GetLastError() ) );
}
}

I do it just before the MouseThreadProc thread is created. Then, in
HIDDeviceNotifications(), you need to notify that the mouse is removed in
the HID_CLOSE_DEVICE case. There, you'll do something like this:

// PGT: Notify everyone that a mouse has been
// disconnected.
DEBUGMSG( 1, ( TEXT( "USBMouse::HIDDeviceNotifications - Mouse removed.
Advertise that.\r\n" ) ) );

{
GUID devclass = DEVCLASS_MOUSE_GUID;
if ( AdvertiseInterface( &devclass, _T( "USBMouse" ), FALSE ) )
{
DEBUGMSG( 1, ( TEXT( "USBMouse::HIDDeviceNotifications - Successfully
deadvertised interface %s.\r\n" ), DEVCLASS_MOUSE_GUID_STRING ) );
}
else
{
DEBUGMSG( 1, ( TEXT( "USBMouse::HIDDeviceNotifications -
AdvertiseInterface failed %s. Err = 0x%x.\r\n" ),
DEVCLASS_MOUSE_GUID_STRING, GetLastError() ) );
}
}

I do this just before FreeHidMouse() is called in the Microsoft code.

Paul T.
 
A

Alessandro

Well, you have to modify the MOUHID component, so you have to clone that and
put it in your platform.  The information on how to do that is, to some
extent, in the help and you can find other documents on the topic online,
I'm sure.  Somewhere near the top, you'll probably add an #include like
this:

// PGT: Get the GUID for the mouse device class that we will
// advertise when the mouse is detected.
#include <devclassmouse.h>

where that header file includes the information I gave you in the last
message.  Then, assuming you are using the Microsoft driver, in the function
HIDDeviceAttach(), you'll need to add code like this:

 // PGT: Notify the system that there is a new mouse device
 // attaching.  Advertise our custom GUID for the device,
 // DEVCLASS_MOUSE_GUID.
 DEBUGMSG( 1, ( TEXT( "USBMouse::HidDeviceAttach - Got a mouse.  Advertise
it.\r\n" ) ) );

 {
  GUID devclass = DEVCLASS_MOUSE_GUID;
  if ( AdvertiseInterface( &devclass, _T( "USBMouse" ), TRUE ) )
  {
   DEBUGMSG( 1, ( TEXT( "USBMouse::HidDeviceAttach - Successfully advertised
interface %s.\r\n" ), DEVCLASS_MOUSE_GUID_STRING ) );
  }
  else
  {
   DEBUGMSG( 1, ( TEXT( "USBMouse::HidDeviceAttach - AdvertiseInterface
failed %s.  Err = 0x%x.\r\n" ), DEVCLASS_MOUSE_GUID_STRING,
GetLastError() ) );
  }
 }

I do it just before the MouseThreadProc thread is created.  Then, in
HIDDeviceNotifications(), you need to notify that the mouse is removed in
the HID_CLOSE_DEVICE case.  There, you'll do something like this:

   // PGT: Notify everyone that a mouse has been
   // disconnected.
   DEBUGMSG( 1, ( TEXT( "USBMouse::HIDDeviceNotifications - Mouse removed.
Advertise that.\r\n" ) ) );

   {
    GUID devclass = DEVCLASS_MOUSE_GUID;
    if ( AdvertiseInterface( &devclass, _T( "USBMouse" ), FALSE ) )
    {
     DEBUGMSG( 1, ( TEXT( "USBMouse::HIDDeviceNotifications - Successfully
deadvertised interface %s.\r\n" ), DEVCLASS_MOUSE_GUID_STRING ) );
    }
    else
    {
     DEBUGMSG( 1, ( TEXT( "USBMouse::HIDDeviceNotifications -
AdvertiseInterface failed %s.  Err = 0x%x.\r\n" ),
DEVCLASS_MOUSE_GUID_STRING, GetLastError() ) );
    }
   }

I do this just before FreeHidMouse() is called in the Microsoft code.

Paul T.

Thank you very much,

I will try to follow your instructions and to implement what you told
me.
In any case, without your help I do not ever be successful.

Regards!
Ale
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top