Jump to content

Help / Reading usb controller with _WinAPI_RegisterRawInputDevices


corgano
 Share

Recommended Posts

Edit: If you already read this post, I updated it to better fit the scope of what I want to do.

My goal is to be able to read the raw output from a usb type controller (or mouse or keyboard, don't want it to be controller-only) so i can see how / what changes when i hit buttons. I've gotten as far as finding HID page documentation and documentation for the device Struct, but I don't know where to go from here

  • How do I tell what and Useage to use for any given controller / Keyboard / Mouse?
  • How do I get a list of all HID devices connected and their UsagePage / Usage?
  • How do I register multiple devices / get raw input from multiple devices at once?

Here is the modified example script i am using:

#include <APISysConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WinAPIGdi.au3>
#include <WinAPIMisc.au3>
#include <WinAPISys.au3>
#include <WindowsConstants.au3>

Opt('TrayAutoPause', 0)

Global $iFlagsOld = 0, $iDataOld = 0


; Create GUI
Global $g_hForm = GUICreate('Test ' & StringReplace(@ScriptName, '.au3', '()'), 160, 212, @DesktopWidth - 179, @DesktopHeight - 283, BitOR($WS_CAPTION, $WS_POPUP, $WS_SYSMENU), $WS_EX_TOPMOST)


; To obtain the values of "UsagePage" and "Usage" members of this structure read HID Usage Tables documentation
; http://www.usb.org/developers/devclass_docs/HID1_11.pdf

;   Disregaurd that, official USB documention = useless. Here is a human/normal-person friendly list
;       http://www.freebsddiary.org/APC/usb_hid_usages.php
;   Information on the Struct (eg what Flags and hTarget do) can be found here
;       https://msdn.microsoft.com/en-us/library/ms645565(v=vs.85).aspx
Local $tRID = DllStructCreate($tagRAWINPUTDEVICE)
DllStructSetData($tRID, 'UsagePage', 0x01) ; Generic Desktop Controls
DllStructSetData($tRID, 'Usage', 0x06) ; Mouse
DllStructSetData($tRID, 'Flags', $RIDEV_INPUTSINK) ; This flag makes window hTarget accept input even when it's not active. MUST define hTarget
DllStructSetData($tRID, 'hTarget', $g_hForm) ; The target window that will be sent events, used with some Flags

; Register HID input to obtain row input
_WinAPI_RegisterRawInputDevices($tRID)

; Register WM_INPUT message
GUIRegisterMsg($WM_INPUT, 'WM_INPUT')

;~ GUISetState(@SW_SHOW)

Do
Until GUIGetMsg() = $GUI_EVENT_CLOSE

Func WM_INPUT($hWnd, $iMsg, $wParam, $lParam)
    #forceref $iMsg, $wParam

    Switch $hWnd
        Case $g_hForm
            Local $tRIM = DllStructCreate($tagRAWINPUTMOUSE)
            If _WinAPI_GetRawInputData($lParam, $tRIM, DllStructGetSize($tRIM), $RID_INPUT) Then
                Local $iFlags = DllStructGetData($tRIM, 'Flags')

                Local $sFlag = "", $sData = ""

                $iFlags = DllStructGetData($tRIM, 'ButtonFlags')
                If $iFlags <> $iFlagsOld Then
                    $sFlag = $iFlags
                    $iFlagsOld = $iFlags
                EndIf

                $iData = DllStructGetData($tRIM, 'ButtonData')
                If $iData <> $iDataOld Then
                    $sData = $iData
                    $iDataOld = $iData
                EndIf

                If $sFlag&$sData <> "" Then
                    ConsoleWrite($sFlag&"   "&$sData&@CRLF)
                EndIf

            EndIf
    EndSwitch
    Return $GUI_RUNDEFMSG
EndFunc   ;==>WM_INPUT


Old post:

Spoiler

I am looking for help with _WinAPI_RegisterRawInputDevices, more specifically a simpler example than the one provided in the help file. What I want is an example script that will show a list of HID devices, let me select one, and then show me the hex readout of the device (so i can push buttons and see how it changes). 

MY end goal is to read outputs from a USB HID game controller

So far, I've looked through a few threads about RawInput.au3 and that eventually lead me to _WinAPI_RegisterRawInputDevices. I have a very basic understanding of structs / pointers, but if someone could explain in a bit more detail how this works it would be greatly appreciated.

 

Edited by corgano

0x616e2069646561206973206c696b652061206d616e20776974686f7574206120626f64792c20746f206669676874206f6e6520697320746f206e657665722077696e2e2e2e2e

Link to comment
Share on other sites

I've been reading through http://www.usb.org/developers/devclass_docs/HID1_11.pdf (the link in the help file is WRONG, needs to be updated to the correct link at http://www.usb.org/developers/hidpage/HID1_11.pdf) to try and make sense of "UseagePage" and "Useage" - to try and make it read the keyboard or a usb HID controller instead, but have not had any luck so far.

EDIT: Found a more readable list here: http://www.freebsddiary.org/APC/usb_hid_usages.php. I have no idea how or where in the documentation linked from the help file this info is.
EDIT2: https://msdn.microsoft.com/query/dev10.query?appId=Dev10IDEF1&l=EN-US&k=k(RegisterRawInputDevices);k(DevLang-C);k(TargetOS-WINDOWS)&rd=true has good info on what the flags do

I also have a second question, the help file says 

Quote

A pointer to an array of $tagRAWINPUTDEVICE structures

Does that mean I can have it listen to multiple things, say the keyboard AND a usb controller? If so, how do I accomplish this?

Edited by corgano

0x616e2069646561206973206c696b652061206d616e20776974686f7574206120626f64792c20746f206669676874206f6e6520697320746f206e657665722077696e2e2e2e2e

Link to comment
Share on other sites

I've hit a wall. I can use _WinAPI_EnumRawInputDevices() to get a list of devices and device handles, but I can't see any way to use _WinAPI_RegisterRawInputDevices() with a device handle, and I can't figure out how to get the Struct _WinAPI_RegisterRawInputDevices() needs from the device handle. I need help from someone who actually knows what they are doing :(

0x616e2069646561206973206c696b652061206d616e20776974686f7574206120626f64792c20746f206669676874206f6e6520697320746f206e657665722077696e2e2e2e2e

Link to comment
Share on other sites

18 hours ago, Nikolas92 said:

 

DllStructSetData($tRID, 'Usage', 0x06) ; Mouse

isnt 0x06 for keyboard and 0x02 for mouse?

Somehow i missed this comment. Yes, 0x02 is mouse I was trying to experiment by seeing my keyboard. As per this list, 06 is keyboard. Indeed i was also able to see when i hit keys on my keyboard.... but not when i released them! I am obviously missing something, or am I wrong to assume a HID keyboard would send a code for both pressing and releasing a key?

Edited by corgano

0x616e2069646561206973206c696b652061206d616e20776974686f7574206120626f64792c20746f206669676874206f6e6520697320746f206e657665722077696e2e2e2e2e

Link to comment
Share on other sites

My main goal is to read gamepads, not just keyboards, but I'll read through and see what I can learn from that doc. Thanks for the link.

I JUST found this tool and it shows me EXACTLY the data I am after. Perhaps my understanding isn't right, but I am looking for this exact data, but directly from autoit. Link here: http://www.virtualdj.com/download/hidtrace.exe
P1San6U.png
I want to get exactly this data in autoit, but how?

0x616e2069646561206973206c696b652061206d616e20776974686f7574206120626f64792c20746f206669676874206f6e6520697320746f206e657665722077696e2e2e2e2e

Link to comment
Share on other sites

2 hours ago, corgano said:

Indeed i was also able to see when i hit keys on my keyboard.... but not when i released them!

Cool, was addressing this statement.  Good Luck,

kylomas

P.S. Was going to suggest that there are easier ways to get KB input but suspected that you already knew that.

Edited by kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Link to comment
Share on other sites

  • 2 weeks later...

I think I'm trying to do something similar, What I want to do is capture raw data from a barcode reader (it acts like a keyboard)  and send it to one guictrl

and all other keyboard input to the appropriate active control.   So I need to be able to separate data based upon the HWID of the device.

My program is pretty sequential, (only first window of mult-window program, actually needs barcode input, and no other input on that page) so that I can call DEVCon to remove the HID  device when I don't need it and rescan to add it back in.  but that is more of a hack then a solution. 

You are farther than I am, so I'm hoping that you can help.

 

Link to comment
Share on other sites

Off topic, but I posted how i captured barcode scanner output here:

TL:DR my scanner let me configure on the scanner itself a pre- and -post scan action. I set the pre-scan action to be send F9 and wait 100ms, the post-scan action to be hit enter.
Then made F9 trigger an inputBox() with a timeout of 500ms to get input. It worked well enough, and made both the program and software plug-and-play without having to worry about device ID's. 

Edited by corgano

0x616e2069646561206973206c696b652061206d616e20776974686f7574206120626f64792c20746f206669676874206f6e6520697320746f206e657665722077696e2e2e2e2e

Link to comment
Share on other sites

Thank you,  but that is not what I'm trying to do.  I can read the Scanner without issues. My problem is that the Barcode scanner is actually a badge reader that is customer facing. When someone badges in to the Kios or selects [Login] button  a screen on the other side of the customer window is opened (Employee) and mirrored back to the customer .  The Employee has the Keyboard and will type notes etc.   but if the customer rescans their badge while the employee is typing, the notes include the badge number.

So what I wanted to do is separate any text that is being scanned into using the badge reader and deal with it, ether ignore it after login or update a hidden input control. but still allow the Employee to input test in the kios form cleanly using his keyboard.

I can use Devcon to remove the device after input and Rescan when I allow the device to be used  that will to add it back in, but that is a hack and it limits some functionality.

 

Link to comment
Share on other sites

  • 2 years later...

Hi... if you have Windows 7 it's possible that might be part of the problem (If the problem isn't that EA/FIFA doesn't have support for that controller in the game, I don't know if they do or not, hopefully someone else might have that information) not supporting the controller. I know there are some games that unfortunately don't support all USB controllers.

turnkey pcb assembly

Edited by ElsaCline
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...