how to get input from various keyboards with directinput

First sorry for my bad english...

I want to get input from various USB keyboard, and I need to know from which keyboard I get the keystroke, I prove the directinput objects in VB 2005. This are the code that I use in the declaration section (in this case I use 2 USB Keyboard)

Implements DirectXEvent8

Private MiDirectX As New DirectX8

Private MiDirectInput As DirectInput8

'the object to my first keyboard

Private diDev As DirectInputDevice8

'the object to my second keyboard

Private diDev2 As DirectInputDevice8

'Private keyState As DIKEYBOARDSTATE

Private keyState2 As DIKEYBOARDSTATE

Private ManejaEvento As Long

Private ManejaEvento2 As Long

-------------------------------------------------------------------------

To get the GUID for my keyboards I use the next code

Dim diDevEnum As DirectInputEnumDevices8

diDevEnum = MiDirectInput.GetDIDevices(CONST_DI8DEVICETYPE.DI8DEVCLASS_DEVICE, CONST_DIENUMDEVICESFLAGS.DIEDFL_ALLDEVICES)

For i = 1 To diDevEnum.GetCount

Debug.Print(diDevEnum.GetItem(i).GetGuidInstance)

Next i

Then I get the GUID for all keyboards that I have connected to my PC.

The code return string like that for all keyboard that are connected to PC

{A3AA2DD0-18C3-11DB-8001-444553540000}

{A3AC9ED0-18C3-11DB-8003-444553540000}

-----------------------------------------------------------------------------------

In the form load secction I use the next code

Private Sub Prueba_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

MiDirectInput = MiDirectX.DirectInputCreate()

'for the keyboard ONE I use the GUID string that I get using

diDev = MiDirectInput.CreateDevice("{A3AA2DD0-18C3-11DB-8001-444553540000}")

'diDev = MiDirectInput.CreateDevice("GUID_SYSKeyboard")

' I'm not use the string "GUID_SYSKeyboard" to create a keyboard device, because the code don't diference from a keyboard from others, else this interpret the input from various keyboard like if I have only one

diDev.SetCommonDataFormat(CONST_DICOMMONDATAFORMATS.DIFORMAT_KEYBOARD)

diDev.SetCooperativeLevel(Me.Handle, CONST_DISCLFLAGS.DISCL_NONEXCLUSIVE Or CONST_DISCLFLAGS.DISCL_BACKGROUND)

'this print that I have a USB_Keyboard device

Debug.Print(diDev.GetDeviceInfo().GetInstanceName())

ManejaEvento = MiDirectX.CreateEvent(Me)

diDev.SetEventNotification(ManejaEvento)

diDev.Acquire()

'for my keyboard 2 I use the next code

diDev2 = MiDirectInput.CreateDevice("{A3AC9ED0-18C3-11DB-8003-444553540000}")

diDev2.SetCommonDataFormat(CONST_DICOMMONDATAFORMATS.DIFORMAT_KEYBOARD)

diDev2.SetCooperativeLevel(Me.Handle, CONST_DISCLFLAGS.DISCL_NONEXCLUSIVE Or CONST_DISCLFLAGS.DISCL_BACKGROUND)

'this print that I have a USB_Keyboard device

Debug.Print(diDev2.GetDeviceInfo().GetInstanceName())

ManejaEvento2 = MiDirectX.CreateEvent(Me)

diDev2.SetEventNotification(ManejaEvento2)

diDev2.Acquire()

End Sub

-------------------------------------------------------

Now I use a DXCallback event, but it doesn't found..

Public Sub DXCallback(ByVal eventid As Integer) Implements DxVBLibA.DirectXEvent8.DXCallback

If eventid = ManejaEvento Then

Call diDev.GetDeviceStateKeyboard(keyState)

'the function Teclas return a key name

TextBox1.Text = TextBox1.Text & Teclas(keyState)

End If

If eventid = ManejaEvento2 Then

Call diDev2.GetDeviceStateKeyboard(keyState2)

TextBox2.Text = TextBox2.Text & Teclas(keyState2)

End If

End Sub

---------------------------------------------------------------------------------------

Someone can tell me what I do wrong




Answer this question

how to get input from various keyboards with directinput

  • JamesZ

    the solution it's to use a SDGToolkit ;)

    reagards

    echavez



  • zkhan

    Hi guys, My name is Paolo and i need to read the pressed key on diferents keyboards. I have 30 keyboards an i need to diferentiate from where keys are produced. I am program with visual basic 6.

    Please i need code to try resolve this situation.

    Thanks Again



  • ggergrer

    this topic is about 3 months old, i see different people need the code for multiple keyboard, so maybe i'm gone write some sample code of what i try to explain to use different keyboards.

    If i get it correctly the biggest issue is not getting input from different keyboards but know what keyboard is used (so lets say, what user is pressing a key). This depends on your program. Cause there are different users i guess each user must enter some user code or something at start

    What i like to know for making a sample code for this is, how your program works, what happens if someone go sit behind the keyboard what needs to happen when someone is pressing a key.

    This might be tricky to write sample code cause i only have 1 keyboard connected to my pc (old PS2 keyboard)


  • Shirvo

    Thanks Nightmare_BE for your comments

    I will try to prove that you tell me.

    Well ,answering your "little question" ;)... I'm working in a proyect that need use various keyboards in a only PC (explicity numeric keyboard), It's for use to question/answer multiple choices that require identify from which user the answer input is comming..

    Because the PC in general have only 2 or 4 USB port for the USBKeyboard, I use a USB Hub D-Link that allow me have connected more than 4 keyboards at the same time.

    To this moment, I can identify all my keyboards that are connected to my PC, and I can see the GUID for everyone of my keyboards, but I can't know from what keyboard the input is comming..

    Thanks, and I hope your comments again.

    Regards

    echavez



  • search and deploy

    The Raw Input API is standard part of Windows XP and is documented in the Platform SDK. It can be used to support multiple independent mice in applications, but from what I can tell it doesn't work with multiple keyboards.
  • Leon Mayne

    Firstly thanks! I would like that you commented something about Raw Input API that you mentioned, maybe that can help me in my proyect.

    Thanks again

    echavez



  • rahulsk1947

    Unfortunately there isn't any way to tell which keyboard your input came from. Windows merges all keyboards into one virtual "system keyboard" device and that's the device DirectInput reads from. Even if you were to use the Raw Input API I don't think you can identify which key strokes come from which keyboards.
  • EGShaw

    Ross Ridge wrote:
    Unfortunately there isn't any way to tell which keyboard your input came from. Windows merges all keyboards into one virtual "system keyboard" device and that's the device DirectInput reads from. Even if you were to use the Raw Input API I don't think you can identify which key strokes come from which keyboards.

    Yes there is a way. U don't need to use the system keyboard guid when creating the device. Instead enumerator all devices connected to your pc like u do for a joystick. Then u can enumerator multiple keyboards / mouses where each device has its own unique guid. So create 2 IDirectInputDevice8's using the guids and u can detect from what keyboard the input is comming.

    but little question ;) ... whats the use of having 2 keyboards connected to your pc ... i never tried that ... if u are in application like word and typing on both keyboard ... will word accept input from both


  • Dany V

    k :)

    What language are u using

    Create a class containing a guid and a IDirectInputDevice8 interface.
    Enumerate all keyboards and create the class and input devices using that guid.

    Loop trough all keyboard classes and update the direct input state.
    In case u are using events/callbacks, pass the keyboard class as parameter so u can see what keyboard invoked the event or callback

    This way u should be able to see what keys are pressed on each keyboard, cause u created a custom class containing the direct input device and the keyboard guid u should be able to detect what keyboard it is.


  • Closer

    Thanks Nihtmare_BE :

    I try to buid an application that control 30 external keyboard and the main computer keyboard too..

    My application consist in the teacher show a question on the datashow, and the 30 students respond the question in each keyboard.

    I think i must register each keyboard with unique id , i know i can program this application using direct input or using raw input. But in both cases i need visual basic code. Where can i get this code for sample maybe.

    Please respond as soon as possible.

    I develop in visual basic 6.

    Thanks Paolo



  • buster2001

    Hi echavez,

    I'm working on some project which require to have mutiple keyboard as you do right now. However, my project is different from you in terms of usage. I just would like to know whether do you able to find the way yet I would gretly appreiate if you could share me some idea of yours. You may contact me at bingkuntod@hotmail.com . Thanks so much in advance.

    Bingkuntod : )


  • Claudiu Chiorean

    I'm using VB 2005. I will try that you tell me.

    By chance you don't have a code that does that

    I believe that you must to send me your account of Western Union to send you a gift… he he ;)

    My msn its topo_chavez@hotmail.com, if it seems to you well , we could speak of businesses.

    Thanks again

    echavez



  • oceanhai

    lol, 30 keyboards all at the same computer

    Its easy, just enumerate the connected devices using direct input, when enum all devices u can see what type of devices those are, each device has an unique guid, this guid is needed to create the keyboard device.

    Create a new keyboard class for each keyboard using the unique guid :)


  • Leaf.

    Thanks Ross Ridge for your help

    I will search more info about the Raw Input API, but I will continue trying with directx.. ;)

    regards

    echavez



  • how to get input from various keyboards with directinput