473,606 Members | 2,115 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 FindDeviceFromG uid _

'(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 = MyDeviceManagem ent.FindDeviceF romGuid _

(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.DeviceAtt ributes.Size = Marshal.SizeOf( MyHID.DeviceAtt ributes)

Result = HidD_GetAttribu tes(HIDHandle, MyHID.DeviceAtt ributes)


If (Result <> 0) Then

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

If (MyHID.DeviceAt tributes.Vendor ID = MyVendorID) And _

(MyHID.DeviceAt tributes.Produc tID = MyProductID) Then

MyDeviceDetecte d = True

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

MyDevicePathNam e = DevicePathName( MemberIndex)

Else

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

MyDeviceDetecte d = False

Result = CloseHandle(HID Handle)

End If

Else

'There was a problem in retrieving the information.

MyDeviceDetecte d = False

Result = CloseHandle(HID Handle)

End If

End If

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

MemberIndex = MemberIndex + 1

Loop Until ((MyDeviceDetec ted = True) Or _

(MemberIndex = UBound(DevicePa thName) + 1))

If MyDeviceDetecte d Then

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

'The device was detected.

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

Success = MyDeviceManagem ent.RegisterFor DeviceNotificat ions _

(MyDevicePathNa me, _

Me.Handle, _

HidGuid, _

DeviceNotificat ionHandle)
'Debug.WriteLin e("RegisterForD eviceNotificati ons = " & Success)

'Learn the capabilities of the device.

MyHID.Capabilit ies = MyHID.GetDevice Capabilities _

(HIDHandle)

If Success Then

'Get and display the Input report buffer size.

'GetInputReport BufferSize()

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

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

ReadHandle = CreateFile _

(MyDevicePathNa me, _

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.FlushQueu e(ReadHandle)

MyHID.FlushQueu e(HIDHandle)

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

'start reading

Dim InputReportBuff er() As Byte

Dim NumberOfBytesRe ad As Long

ReDim InputReportBuff er(32)

success = HidD_GetFeature _

(hidHandle, _

InputReportBuff er(0), _

CInt(UBound(inF eatureReportBuf fer) + 1))

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

HidD_GetInputRe port and read file functions. nogo.

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

Nov 21 '05 #1
2 8750
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_******** ************@ro gers.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 FindDeviceFromG uid _

'(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 = MyDeviceManagem ent.FindDeviceF romGuid _

(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.DeviceAtt ributes.Size = Marshal.SizeOf( MyHID.DeviceAtt ributes)

Result = HidD_GetAttribu tes(HIDHandle, MyHID.DeviceAtt ributes)


If (Result <> 0) Then

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

If (MyHID.DeviceAt tributes.Vendor ID = MyVendorID) And _

(MyHID.DeviceAt tributes.Produc tID = MyProductID) Then

MyDeviceDetecte d = True

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

MyDevicePathNam e = DevicePathName( MemberIndex)

Else

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

MyDeviceDetecte d = False

Result = CloseHandle(HID Handle)

End If

Else

'There was a problem in retrieving the information.

MyDeviceDetecte d = False

Result = CloseHandle(HID Handle)

End If

End If

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

MemberIndex = MemberIndex + 1

Loop Until ((MyDeviceDetec ted = True) Or _

(MemberIndex = UBound(DevicePa thName) + 1))

If MyDeviceDetecte d Then

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

'The device was detected.

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

Success = MyDeviceManagem ent.RegisterFor DeviceNotificat ions _

(MyDevicePathNa me, _

Me.Handle, _

HidGuid, _

DeviceNotificat ionHandle)
'Debug.WriteLin e("RegisterForD eviceNotificati ons = " & Success)

'Learn the capabilities of the device.

MyHID.Capabilit ies = MyHID.GetDevice Capabilities _

(HIDHandle)

If Success Then

'Get and display the Input report buffer size.

'GetInputReport BufferSize()

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

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

ReadHandle = CreateFile _

(MyDevicePathNa me, _

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.FlushQueu e(ReadHandle)

MyHID.FlushQueu e(HIDHandle)

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

'start reading

Dim InputReportBuff er() As Byte

Dim NumberOfBytesRe ad As Long

ReDim InputReportBuff er(32)

success = HidD_GetFeature _

(hidHandle, _

InputReportBuff er(0), _

CInt(UBound(inF eatureReportBuf fer) + 1))

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

HidD_GetInputRe port 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_******** ************@ro gers.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 FindDeviceFromG uid _

'(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 = MyDeviceManagem ent.FindDeviceF romGuid _

(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.DeviceAtt ributes.Size = Marshal.SizeOf( MyHID.DeviceAtt ributes)

Result = HidD_GetAttribu tes(HIDHandle, MyHID.DeviceAtt ributes)


If (Result <> 0) Then

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

If (MyHID.DeviceAt tributes.Vendor ID = MyVendorID) And _

(MyHID.DeviceAt tributes.Produc tID = MyProductID) Then

MyDeviceDetecte d = True

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

MyDevicePathNam e = DevicePathName( MemberIndex)

Else

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

MyDeviceDetecte d = False

Result = CloseHandle(HID Handle)

End If

Else

'There was a problem in retrieving the information.

MyDeviceDetecte d = False

Result = CloseHandle(HID Handle)

End If

End If

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

MemberIndex = MemberIndex + 1

Loop Until ((MyDeviceDetec ted = True) Or _

(MemberIndex = UBound(DevicePa thName) + 1))

If MyDeviceDetecte d Then

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

'The device was detected.

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

Success = MyDeviceManagem ent.RegisterFor DeviceNotificat ions _

(MyDevicePathNa me, _

Me.Handle, _

HidGuid, _

DeviceNotificat ionHandle)
'Debug.WriteLin e("RegisterForD eviceNotificati ons = " & Success)

'Learn the capabilities of the device.

MyHID.Capabilit ies = MyHID.GetDevice Capabilities _

(HIDHandle)

If Success Then

'Get and display the Input report buffer size.

'GetInputReport BufferSize()

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

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

ReadHandle = CreateFile _

(MyDevicePathNa me, _

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.FlushQueu e(ReadHandle)

MyHID.FlushQueu e(HIDHandle)

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

'start reading

Dim InputReportBuff er() As Byte

Dim NumberOfBytesRe ad As Long

ReDim InputReportBuff er(32)

success = HidD_GetFeature _

(hidHandle, _

InputReportBuff er(0), _

CInt(UBound(inF eatureReportBuf fer) + 1))

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

HidD_GetInputRe port 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
31344
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 would the same set of commands look like in VB. I am reading the
2
16395
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 between those keys. Thank you
1
20435
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 pressed (without hitting return). I looked at serveral faq's on the net and installed the cspan readkey module and neither seems to work most likey its me since I am getting frustrated. but anyway here's a sample code. while (1) { if...
7
2832
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 files still allocated in log directory; when all application disconnect from database or database reactive or database restart the secondary log files will be deleted from log directory. For each transaction log request, database manager always...
50
4916
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 -- the character input is a different matter, at least if I want to remain within the bounds of the standard library.
3
9500
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 the end of this message is the inflate method this is where I get stuck I know that I need a byte array but because I am decompressing a string I have no idea of how big the byte array will need to be in the end (the inflate and deflate methods...
0
6658
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 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...
0
356
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 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...
0
4376
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, it simply uses the built in MS HID srv driver. It sends the results as keyboard commands. I have 2 ways to catch this data as I see it: 1) I can read the keyboard commands as they come in. In order to make sure that I get the commands even when my...
0
8010
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
7942
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8433
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8084
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8300
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
3922
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2443
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1550
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1287
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.