reading from a USB keyboard (HID)

From: rs (aa_at_d.com)
Date: 11/10/04


Date: Tue, 9 Nov 2004 23:01:01 -0500

Hi guys,

I am trying to read from a USB keyboard using vb.net and HID classes. the
USB keyboard is not my primary keyboard. I have a ps2 keyboard connected and
is detected in device manager as my keyboard. the USB keyboard is detected
as HID keyboard device. the program finds the keyboard if it is attached.
and I am getting valid handles. however, everytime I use the readfile
function I am getting "object reference not set to an instant of an object"
exception. if i use any other function it fails and I am getting -1 in the
"success" parameter. I am including my code:

HidD_GetHidGuid(HidGuid)

'Friend Function FindDeviceFromGuid _

'(ByVal myGuid As System.Guid, _

'ByRef devicePathName() As String) _

'As Boolean

'Purpose : Uses SetupDi API functions to retrieve the device path name of an

' : attached device that belongs to an interface class.

'Accepts : myGuid - an interface class GUID.

' : devicePathName - a pointer to an array of strings that will contain

' : the device path names of attached devices.

'Returns : True if at least one device is found, False if not.

'Fill an array with the device path names of all attached HIDs.

DeviceFound = MyDeviceManagement.FindDeviceFromGuid _

(HidGuid, _

DevicePathName)

'If there is at least one HID, attempt to read the Vendor ID and Product ID

'of each device until there is a match or all devices have been examined.

If DeviceFound = True Then

MemberIndex = 0

Do

HIDHandle = CreateFile _

(DevicePathName(MemberIndex), _

0, _

FILE_SHARE_READ, _

Security, _

OPEN_EXISTING, _

0, _

0)

If (HIDHandle <> INVALID_HANDLE_VALUE) Then

MyHID.DeviceAttributes.Size = Marshal.SizeOf(MyHID.DeviceAttributes)

Result = HidD_GetAttributes(HIDHandle, MyHID.DeviceAttributes)

If (Result <> 0) Then

'Find out if the device matches the one we're looking for.

If (MyHID.DeviceAttributes.VendorID = MyVendorID) And _

(MyHID.DeviceAttributes.ProductID = MyProductID) Then

MyDeviceDetected = True

'Save the DevicePathName so OnDeviceChange() knows which name is my device.

MyDevicePathName = DevicePathName(MemberIndex)

Else

'It's not a match, so close the handle.

MyDeviceDetected = False

Result = CloseHandle(HIDHandle)

End If

Else

'There was a problem in retrieving the information.

MyDeviceDetected = False

Result = CloseHandle(HIDHandle)

End If

End If

'Keep looking until we find the device or there are no more left to examine.

MemberIndex = MemberIndex + 1

Loop Until ((MyDeviceDetected = True) Or _

(MemberIndex = UBound(DevicePathName) + 1))

If MyDeviceDetected Then

rtbstatus.Text = rtbstatus.Text + vbCrLf + "keyboard found"

'The device was detected.

'Register to receive notifications if the device is removed or attached.

Success = MyDeviceManagement.RegisterForDeviceNotifications _

(MyDevicePathName, _

Me.Handle, _

HidGuid, _

DeviceNotificationHandle)

'Debug.WriteLine("RegisterForDeviceNotifications = " & Success)

'Learn the capabilities of the device.

MyHID.Capabilities = MyHID.GetDeviceCapabilities _

(HIDHandle)

If Success Then

'Get and display the Input report buffer size.

'GetInputReportBufferSize()

rtbstatus.Text = rtbstatus.Text + vbCrLf + "register notification is set"

'*******************************

ReadHandle = CreateFile _

(MyDevicePathName, _

GENERIC_READ, _

0, _

Security, _

OPEN_EXISTING, _

0, _

0) I also tried it with overlaping. nogo

rtbstatus.Text = rtbstatus.Text + vbCrLf + "handle created"

'(optional)

'Flush any waiting reports in the input buffer.

MyHID.FlushQueue(ReadHandle)

MyHID.FlushQueue(HIDHandle)

'*****************************************

'start reading

Dim InputReportBuffer() As Byte

Dim NumberOfBytesRead As Long

ReDim InputReportBuffer(32)

success = HidD_GetFeature _

(hidHandle, _

InputReportBuffer(0), _

CInt(UBound(inFeatureReportBuffer) + 1))

'sucess always = false and input report buffer = 0s. I also tried

HidD_GetInputReport and read file functions. nogo.

any idea what i am doing wrong? any input is appreciated.



Relevant Pages

  • reading from a keyboard as HID
    ... I am trying to read from a USB keyboard using vb.net and HID classes. ... Success = MyDeviceManagement.RegisterForDeviceNotifications _ ...
    (microsoft.public.dotnet.academic)
  • reading from secondary USB keyboard
    ... I am trying to read from a USB keyboard using vb.net and HID classes. ... Success = MyDeviceManagement.RegisterForDeviceNotifications _ ...
    (microsoft.public.dotnet.languages.vb)
  • [PATCH] Input quirk for the Fn key on Powerbooks with an USB keyboard
    ... keyboard has been replaced with an USB one conforming to the HID spec. ... The 'Fn' modifier key on this keyboard is a soft key reported using an ...
    (Linux-Kernel)
  • Re: Injecting keyboard scan codes at driver level
    ... as a hid miniport you cannot create a control device object. ... use the WDK 6001 osrfx2hid sample which is really a full blown KMDF driver using a HID miniport that is purely pass through. ... You will need to create a control object to communictate (you cant open the keyboard directly) ... scan codes to the keyboard data stream based on data received from the user ...
    (microsoft.public.development.device.drivers)
  • Re: windows noob needs pointers to writing a device driver
    ... no need to hack kbdhid. ... kbdhid uses the hid parser ... >> keyboard stack, they are handled by windows input subsystem itself (the ...
    (microsoft.public.development.device.drivers)