473,289 Members | 1,959 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,289 software developers and data experts.

reading from secondary USB keyboard

rs
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.

Nov 21 '05 #1
2 8711
All keyboards under Windows are aggregated together as a single global
keyboard. You cannot deal with them separately without a filter driver.
You might see what you can do with DirectX.

Marc Reinig
System Solutions

"rs" <aa@d.com> wrote in message news:u_********************@rogers.com...
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.


Nov 21 '05 #2
All keyboards under Windows are aggregated together as a single global
keyboard. You cannot deal with them separately without a filter driver.
You might see what you can do with DirectX.

Marc Reinig
System Solutions

"rs" <aa@d.com> wrote in message news:u_********************@rogers.com...
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.


Nov 21 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Simon | last post by:
Using qbasic, the following accepts input from com1, and prints its output : OPEN "com1:9600,n,8,1,CD0,CS0,DS0,RS" FOR INPUT AS #1 WHILE INKEY$ <> " " INPUT #1, a$ PRINT a$ WEND What...
2
by: Michael Bendzick | last post by:
Is there a simple way in python to read a keyboard scan code? I'm working on a shell script that interfaces with a proprietary keyboard device (extra buttons) and need to be able to distinguish...
1
by: Scott Shaw | last post by:
Hi all, I was wondering if you could help out with this problem that I am having. What I am trying to do is detect keyboard input in a while loop without halting/pausing the loop until the key is...
7
by: hank | last post by:
Hi All In the Circular Logging when the Primary Log file fill up, the database manager will creat a secondary log files for the transaction; when this transaction finished, the secondary log...
50
by: Michael Mair | last post by:
Cheerio, I would appreciate opinions on the following: Given the task to read a _complete_ text file into a string: What is the "best" way to do it? Handling the buffer is not the problem...
3
by: Nick | last post by:
I have found a class that compresses and uncompresses data but need some help with how to use part of it below is the deflate method which compresses the string that I pass in, this works OK. At...
0
by: rs | last post by:
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...
0
by: rs | last post by:
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...
0
by: Chris Herring | last post by:
I am writing a WinForms app that will integrate a handheld voting device system into the app. The handhelds communicate to the PC via a USB connection. The USB device has no manufacturer driver,...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.