1 (изменено: serzh82saratov, 2017-06-20 17:41:28)

Тема: AHK: Работа с джойстиками через HID

Отслеживание нажатий джойстиков. Нужна библиотека AHKHID, она под спойлером.
У кого более одного джойстика, проверьте меняются ли их имена.


#SingleInstance Force
#NoEnv
ListLines Off
SetBatchLines -1  

Global Joys := {Index:0} 
AHKHID_UseConstants()

Loop % AHKHID_GetDevCount()
{  
	If (AHKHID_GetDevType(A_Index) = RIM_TYPEHID)
	{  
		If (AHKHID_GetDevInfo(A_Index, DI_HID_USAGEPAGE) = 1 && AHKHID_GetDevInfo(A_Index, DI_HID_USAGE) = 5)
			Joys[AHKHID_GetDevName(A_Index)] := (++Joys.Index) "Joy"
	}
} 

AHKHID_Register(1,5,A_ScriptHWND,0x00000100)   ; RIDEV_INPUTSINK := 0x00000100
OnMessage(0x00FF, "InputMsg")   

Esc::ExitApp
 
InputMsg(wParam, lParam) { 
    Critical
	AHKHID_GetInputData(lParam, uData) 
	SetFormat, IntegerFast, d
	Tip := ""
	. " Name: "  Joys[AHKHID_GetDevName(AHKHID_GetInputInfo(lParam, 8), True)]   ; II_DEVHANDLE := 8 
	. "`n`n x1: "  NumGet(&uData, 2, "UChar")
	. "`n y1: "  NumGet(&uData, 4, "UChar") 
	. "`n x2: "  NumGet(&uData, 6, "UChar")
	. "`n y2: "  NumGet(&uData, 8, "UChar")
	. "`n z: "  NumGet(&uData, 10, "UChar") 
	SetFormat, IntegerFast, h
	Tip .= ""
	. "`n первые 8 кнопок: "  NumGet(&uData, 11, "UChar") "`t`t ""обычные"" кнопки."
	. "`n вторые 8 кнопок: "  NumGet(&uData, 12, "UChar") "`t`t нажатия на стики и крестовина."
	. "`n третьи 8 кнопок: "  NumGet(&uData, 13, "UChar")
	. "`n четвёртые 8 кнопок: "  NumGet(&uData, 14, "UChar")  
	ToolTip % Tip
} 
+ AHKHID

/*! TheGood
    AHKHID - An AHK implementation of the HID functions.
    Last updated: August 22nd, 2010

USING THE CONSTANTS:

If you explicitly #include AHKHID in your script, you will have all the constants available to you. Otherwise, if AHKHID is
in your library folder and you do not wish to explicitly #include it, you can call AHKHID_UseConstants() to have the
constants available to you.

FUNCTION LIST:
_____________________
AHKHID_UseConstants()

See the section above titled "USING THE CONSTANTS"
___________________________________
AHKHID_Initialize(bRefresh = False)

You don't have to call this function manually. It is automatically called by other functions to get the pointer of the
RAWINPUTDEVICELIST struct array. However, if a new device is plugged in, you will have to refresh the listing by calling it
with bRefresh = True. Returns -1 on error (with error message in ErrorLevel).
____________________
AHKHID_GetDevCount()

Returs the number of HID devices connected to this computer.
Returns -1 on error (with error message in ErrorLevel).
______________________
AHKHID_GetDevHandle(i)

Returns the handle of device i (starts at 1).
Mostly used internally for API calls.
__________________________
AHKHID_GetDevIndex(Handle)

Returns the index (starts at 1) of the device in the enumeration with matching handle.
Returns 0 if not found.
______________________________________
AHKHID_GetDevType(i, IsHandle = False)

Returns the type of the device. See the RIM_ constants for possible values.
If IsHandle is false, then i is considered the index (starts at 1) of the device in the enumeration.
Otherwise it is the handle of the device.
______________________________________
AHKHID_GetDevName(i, IsHandle = False)

Returns the name of the device (or empty string on error, with error message in ErrorLevel).
If IsHandle is false, then i is considered the index (starts at 1) of the device in the enumeration.
Otherwise it is the handle of the device.
____________________________________________
AHKHID_GetDevInfo(i, Flag, IsHandle = False)

Retrieves info from the RID_DEVICE_INFO struct. To retrieve a member, simply use the corresponding flag. A list of flags
can be found at the top of the script (the constants starting with DI_). Each flag corresponds to a member in the struct.
If IsHandle is false, then i is considered the index (starts at 1) of the device in the enumeration. Otherwise it is the
handle of the device. Returns -1 on error (with error message in ErrorLevel).

See Example 1 for an example on how to use it. 
_______________________________________________________________________________
AHKHID_AddRegister(UsagePage = False, Usage = False, Handle = False, Flags = 0)

Allows you to queue up RAWINPUTDEVICE structures before doing the registration. To use it, you first need to initialize the
variable by calling AHKHID_AddRegister(iNumberOfElements). To then add to the stack, simply call it with the parameters you
want (eg. AHKHID_AddRegister(1,6,MyGuiHandle) for keyboards). When you're finally done, you just have to call
AHKHID_Register() with no parameters. The function returns -1 if the struct is full. Redimensioning the struct will erase
all previous structs added. On success, it returns the address of the array of structs (if you'd rather manipulate it
yourself).

See Example 2 for an example on how to use it.

You will need to do this if you want to use advance features of the RAWINPUTDEVICE flags. For example, if you want to
register all devices using Usage Page 1 but would like to exclude devices of Usage Page 1 using Usage 2 (keyboards), then
you need to place two elements in the array. The first one is AHKHID_AddRegister(1,0,MyGuiHandle,RIDEV_PAGEONLY) and the
second one is AHKHID_AddRegister(1,2,MyGuiHandle,RIDEV_EXCLUDE).

Tip: Have a look at all the flags you can use (see the constants starting with RIDEV_). The most useful is RIDEV_INPUTSINK.
Tip: Set Handle to 0 if you want the WM_INPUT messages to go to the window with keyboard focus.
Tip: To unregister, use the flag RIDEV_REMOVE. Note that you also need to use the RIDEV_PAGEONLY flag if the TLC was
registered with it.
____________________________________________________________________________
AHKHID_Register(UsagePage = False, Usage = False, Handle = False, Flags = 0)

This function can be used in two ways. If no parameters are specified, it will use the RAWINPUTDEVICE array created through
AHKHID_AddRegister() and register. Otherwise, it will register only the specified parameters. For example, if you just want
to register the mouse, you can simply do AHKHID_Register(1,2,MyGuiHandle). Returns 0 on success, returns -1 on error (with
error message in ErrorLevel).

See Example 2 for an example on how to use it with the RAWINPUTDEVICE.
See Example 3 for an example on how to use it only with the specified parameters.
____________________________________
AHKHID_GetRegisteredDevs(ByRef uDev)

This function allows you to get an array of the TLCs that have already been registered.
It fills uDev with an array of RAWINPUTDEVICE and returns the number of elements in the array.
Returns -1 on error (with error message in ErrorLevel).

See Example 2 for an example on how to use it.
______________________________________
AHKHID_GetInputInfo(InputHandle, Flag)

This function is used to retrieve the data upon receiving WM_INPUT messages. By passing the lParam of the WM_INPUT (0xFF00)
messages, it can retrieve all the members of the RAWINPUT structure, except the raw data coming from HID devices (use
AHKHID_GetInputData for that). To retrieve a member, simply specify the flag corresponding to the member you want, and call
the function. A list of all the flags can be found at the top of this script (the constants starting with II_). Returns -1
on error (with error message in ErrorLevel).

See Example 2 for an example on how to use it to retrieve each member of the structure.
See Example 3 for an example on how to interpret members which represent flags.

Tip: You have to use Critical in your message function or you might get invalid handle errors.
Tip: You can check the value of wParam to know if the application was in the foreground upon reception (see RIM_INPUT).
_____________________________________________
AHKHID_GetInputData(InputHandle, ByRef uData)

This function is used to retrieve the data sent by HID devices of type RIM_TYPEHID (ie. neither keyboard nor mouse) upon
receiving WM_INPUT messages. CAUTION: it does not check if the device is indeed of type HID. It is up to you to do so (you
can use GetInputInfo for that). Specify the lParam of the WM_INPUT (0xFF00) message and the function will put in uData the
raw data received from the device. It will then return the size (number of bytes) of uData. Returns -1 on error (with error
message in ErrorLevel).

See Example 2 for an example on how to use it (although you need an HID device of type RIM_TYPEHID to test it).

*/

AHKHID_Included := True
AHKHID_SetConstants:
;______________________________________
;Flags you can use in AHKHID_GetDevInfo
;http://msdn.microsoft.com/en-us/library/ms645581
DI_DEVTYPE                  := 4    ;Type of the device. See RIM_ constants.

DI_MSE_ID                   := 8    ;ID for the mouse device.
DI_MSE_NUMBEROFBUTTONS      := 12   ;Number of buttons for the mouse.
DI_MSE_SAMPLERATE           := 16   ;Number of data points per second. This information may not be applicable for every
                                    ;mouse device.
DI_MSE_HASHORIZONTALWHEEL   := 20   ;Vista and later only: TRUE if the mouse has a wheel for horizontal scrolling;
                                    ;otherwise, FALSE.

DI_KBD_TYPE                 := 8    ;Type of the keyboard. 
DI_KBD_SUBTYPE              := 12   ;Subtype of the keyboard. 
DI_KBD_KEYBOARDMODE         := 16   ;Scan code mode. 
DI_KBD_NUMBEROFFUNCTIONKEYS := 20   ;Number of function keys on the keyboard.
DI_KBD_NUMBEROFINDICATORS   := 24   ;Number of LED indicators on the keyboard.
DI_KBD_NUMBEROFKEYSTOTAL    := 28   ;Total number of keys on the keyboard. 

DI_HID_VENDORID             := 8    ;Vendor ID for the HID.
DI_HID_PRODUCTID            := 12   ;Product ID for the HID. 
DI_HID_VERSIONNUMBER        := 16   ;Version number for the HID. 
DI_HID_USAGEPAGE            := 20 | 0x0100  ;Top-level collection Usage Page for the device.
DI_HID_USAGE                := 22 | 0x0100  ;Top-level collection Usage for the device.
;________________________________________
;Flags you can use in AHKHID_GetInputInfo
;http://msdn.microsoft.com/en-us/library/ms645562
II_DEVTYPE          := 0    ;Type of the device generating the raw input data. See RIM_ constants.
II_DEVHANDLE        := 8    ;Handle to the device generating the raw input data.

II_MSE_FLAGS        := (08+A_PtrSize*2) | 0x0100  ;Mouse state. This member can be any reasonable combination of the
                                    ;following values -> see MOUSE constants.
II_MSE_BUTTONFLAGS  := (12+A_PtrSize*2) | 0x0100  ;Transition state of the mouse buttons. This member can be one or more of
                                    ;the following values -> see RI_MOUSE constants.
II_MSE_BUTTONDATA   := (14+A_PtrSize*2) | 0x1100  ;If usButtonFlags is RI_MOUSE_WHEEL, this member is a signed value that
                                    ;specifies the wheel delta.
II_MSE_RAWBUTTONS   := (16+A_PtrSize*2)           ;Raw state of the mouse buttons. 
II_MSE_LASTX        := (20+A_PtrSize*2) | 0x1000  ;Motion in the X direction. This is signed relative motion or absolute
                                    ;motion, depending on the value of usFlags.
II_MSE_LASTY        := (24+A_PtrSize*2) | 0x1000  ;Motion in the Y direction. This is signed relative motion or absolute
                                    ;motion, depending on the value of usFlags. 
II_MSE_EXTRAINFO    := (28+A_PtrSize*2)           ;Device-specific additional information for the event. 

II_KBD_MAKECODE     := (08+A_PtrSize*2) | 0x0100  ;Scan code from the key depression. The scan code for keyboard overrun is
                                    ;KEYBOARD_OVERRUN_MAKE_CODE.
II_KBD_FLAGS        := (10+A_PtrSize*2) | 0x0100  ;Flags for scan code information. It can be one or more of the following
                                    ;values -> see RI_KEY constants.
II_KBD_VKEY         := (14+A_PtrSize*2) | 0x0100  ;Microsoft Windows message compatible virtual-key code.
II_KBD_MSG          := (16+A_PtrSize*2)           ;Corresponding window message, for example WM_KEYDOWN, WM_SYSKEYDOWN, and
                                                  ;so forth.
II_KBD_EXTRAINFO    := (20+A_PtrSize*2)           ;Device-specific additional information for the event.

II_HID_SIZE         := (08+A_PtrSize*2)           ;Size, in bytes, of each HID input in bRawData.
II_HID_COUNT        := (12+A_PtrSize*2)           ;Number of HID inputs in bRawData.

;DO NOT USE WITH AHKHID_GetInputInfo. Use AHKHID_GetInputData instead to retrieve the raw data.
II_HID_DATA         := (16+A_PtrSize*2)           ;Raw input data as an array of bytes.
;__________________________________________________________________________________
;Device type values returned by AHKHID_GetDevType as well as DI_DEVTYPE and II_DEVTYPE
;http://msdn.microsoft.com/en-us/library/ms645568
RIM_TYPEMOUSE       := 0    ;The device is a mouse.
RIM_TYPEKEYBOARD    := 1    ;The device is a keyboard.
RIM_TYPEHID         := 2    ;The device is an Human Interface Device (HID) that is not a keyboard and not a mouse.
;_______________________________________________________________________________________________
;Different flags for RAWINPUTDEVICE structure (to be used with AHKHID_AddRegister and AHKHID_Register)
;http://msdn.microsoft.com/en-us/library/ms645565
RIDEV_REMOVE        := 0x00000001   ;If set, this removes the top level collection from the inclusion list. This tells the
                                    ;operating system to stop reading from a device which matches the top level collection.
RIDEV_EXCLUDE       := 0x00000010   ;If set, this specifies the top level collections to exclude when reading a complete
                                    ;usage page. This flag only affects a TLC whose usage page is already specified with
                                    ;RIDEV_PAGEONLY.
RIDEV_PAGEONLY      := 0x00000020   ;If set, this specifies all devices whose top level collection is from the specified
                                    ;usUsagePage. Note that usUsage must be zero. To exclude a particular top level
                                    ;collection, use RIDEV_EXCLUDE.
RIDEV_NOLEGACY      := 0x00000030   ;If set, this prevents any devices specified by usUsagePage or usUsage from generating
                                    ;legacy messages. This is only for the mouse and keyboard. See Remarks.
RIDEV_INPUTSINK     := 0x00000100   ;If set, this enables the caller to receive the input even when the caller is not in
                                    ;the foreground. Note that hwndTarget must be specified.
RIDEV_CAPTUREMOUSE  := 0x00000200   ;If set, the mouse button click does not activate the other window.
RIDEV_NOHOTKEYS     := 0x00000200   ;If set, the application-defined keyboard device hotkeys are not handled. However, the
                                    ;system hotkeys; for example, ALT+TAB and CTRL+ALT+DEL, are still handled. By default,
                                    ;all keyboard hotkeys are handled. RIDEV_NOHOTKEYS can be specified even if
                                    ;RIDEV_NOLEGACY is not specified and hwndTarget is NULL.
RIDEV_APPKEYS       := 0x00000400   ;Microsoft Windows XP Service Pack 1 (SP1): If set, the application command keys are
                                    ;handled. RIDEV_APPKEYS can be specified only if RIDEV_NOLEGACY is specified for a
                                    ;keyboard device.
RIDEV_EXINPUTSINK   := 0x00001000   ;Vista and later only: If set, this enables the caller to receive input in the
                                    ;background only if the foreground application does not process it. In other words, if
                                    ;the foreground application is not registered for raw input, then the background
                                    ;application that is registered will receive the input.
RIDEV_DEVNOTIFY     := 0x00002000   ;Vista and later only: If set, this enables the caller to receive WM_INPUT_DEVICE_CHANGE
                                    ;notifications for device arrival and device removal.
;__________________________________________________
;Different values of wParam in the WM_INPUT message
;http://msdn.microsoft.com/en-us/library/ms645590
RIM_INPUT       := 0    ;Input occurred while the application was in the foreground. The application must call
                        ;DefWindowProc so the system can perform cleanup.
RIM_INPUTSINK   := 1    ;Input occurred while the application was not in the foreground. The application must call
                        ;DefWindowProc so the system can perform the cleanup.
;__________________________________
;Flags for GetRawInputData API call
;http://msdn.microsoft.com/en-us/library/ms645596
RID_INPUT    := 0x10000003    ;Get the raw data from the RAWINPUT structure.
RID_HEADER   := 0x10000005    ;Get the header information from the RAWINPUT structure.
;_____________________________________
;Flags for RAWMOUSE (part of RAWINPUT)
;http://msdn.microsoft.com/en-us/library/ms645578

;Flags for the II_MSE_FLAGS member
MOUSE_MOVE_RELATIVE         := 0     ;Mouse movement data is relative to the last mouse position.
MOUSE_MOVE_ABSOLUTE         := 1     ;Mouse movement data is based on absolute position.
MOUSE_VIRTUAL_DESKTOP       := 0x02  ;Mouse coordinates are mapped to the virtual desktop (for a multiple monitor system)
MOUSE_ATTRIBUTES_CHANGED    := 0x04  ;Mouse attributes changed; application needs to query the mouse attributes.

;Flags for the II_MSE_BUTTONFLAGS member
RI_MOUSE_LEFT_BUTTON_DOWN   := 0x0001   ;Self-explanatory
RI_MOUSE_LEFT_BUTTON_UP     := 0x0002   ;Self-explanatory
RI_MOUSE_RIGHT_BUTTON_DOWN  := 0x0004   ;Self-explanatory
RI_MOUSE_RIGHT_BUTTON_UP    := 0x0008   ;Self-explanatory
RI_MOUSE_MIDDLE_BUTTON_DOWN := 0x0010   ;Self-explanatory
RI_MOUSE_MIDDLE_BUTTON_UP   := 0x0020   ;Self-explanatory
RI_MOUSE_BUTTON_4_DOWN      := 0x0040   ;XBUTTON1 changed to down.
RI_MOUSE_BUTTON_4_UP        := 0x0080   ;XBUTTON1 changed to up.
RI_MOUSE_BUTTON_5_DOWN      := 0x0100   ;XBUTTON2 changed to down.
RI_MOUSE_BUTTON_5_UP        := 0x0200   ;XBUTTON2 changed to up.
RI_MOUSE_WHEEL              := 0x0400   ;Raw input comes from a mouse wheel. The wheel delta is stored in usButtonData.
;____________________________________________
;Flags for the RAWKEYBOARD (part of RAWINPUT)
;http://msdn.microsoft.com/en-us/library/ms645575

;Flag for the II_KBD_MAKECODE member in the event of a keyboard overrun
KEYBOARD_OVERRUN_MAKE_CODE  := 0xFF

;Flags for the II_KBD_FLAGS member
RI_KEY_MAKE             := 0
RI_KEY_BREAK            := 1
RI_KEY_E0               := 2
RI_KEY_E1               := 4
RI_KEY_TERMSRV_SET_LED  := 8
RI_KEY_TERMSRV_SHADOW   := 0x10
;____________________________________
;AHKHID FUNCTIONS

If Not AHKHID_Included
    Return

AHKHID_UseConstants() {
    Global ;To make the constants global
    AHKHID_Included := False
    Gosub, AHKHID_SetConstants
}

AHKHID_Initialize(bRefresh = False) {
    Static uHIDList, bInitialized := False
    
    If bInitialized And Not bRefresh
        Return &uHIDList
    
    ;Get the device count
    r := DllCall("GetRawInputDeviceList", "Ptr", 0, "UInt*", iCount, "UInt", A_PtrSize * 2)
    
    ;Check for error
    If (r = -1) Or ErrorLevel {
        ErrorLevel = GetRawInputDeviceList call failed.`nReturn value: %r%`nErrorLevel: %ErrorLevel%`nLine: %A_LineNumber%`nLast Error: %A_LastError%
        Return -1
    }
    
    ;Prep var
    VarSetCapacity(uHIDList, iCount * (A_PtrSize * 2))
    r := DllCall("GetRawInputDeviceList", "Ptr", &uHIDList, "UInt*", iCount, "UInt", A_PtrSize * 2)
    If (r = -1) Or ErrorLevel {
        ErrorLevel = GetRawInputDeviceList call failed.`nReturn value: %r%`nErrorLevel: %ErrorLevel%`nLine: %A_LineNumber%`nLast Error: %A_LastError%
        Return -1
    }
    
    bInitialized := True
    Return &uHIDList
}

AHKHID_GetDevCount() {
    
    ;Get the device count
    r := DllCall("GetRawInputDeviceList", "Ptr", 0, "UInt*", iCount, "UInt", A_PtrSize * 2)
    
    ;Check for error
    If (r = -1) Or ErrorLevel {
        ErrorLevel = GetRawInputDeviceList call failed.`nReturn value: %r%`nErrorLevel: %ErrorLevel%`nLine: %A_LineNumber%`nLast Error: %A_LastError%
        Return -1
    } Else Return iCount
}

AHKHID_GetDevHandle(i) {
    Return NumGet(AHKHID_Initialize(), (i - 1) * (A_PtrSize * 2))
}

AHKHID_GetDevIndex(Handle) {
    Loop % AHKHID_GetDevCount()
        If (NumGet(AHKHID_Initialize(), (A_Index - 1) * (A_PtrSize * 2)) = Handle)
            Return A_Index
    Return 0
}

AHKHID_GetDevType(i, IsHandle = False) {
    Return Not IsHandle ? NumGet(AHKHID_Initialize(), ((i - 1) * (A_PtrSize * 2)) + A_PtrSize, "UInt")
    : NumGet(AHKHID_Initialize(), ((AHKHID_GetDevIndex(i) - 1) * (A_PtrSize * 2)) + A_PtrSize, "UInt")
}

AHKHID_GetDevName(i, IsHandle = False) {
    
    ;Get index if i is handle
    h := IsHandle ? i : AHKHID_GetDevHandle(i)
    
    ;Get device name length.                                RIDI_DEVICENAME
    r := DllCall("GetRawInputDeviceInfo", "Ptr", h, "UInt", 0x20000007, "Ptr", 0, "UInt*", iLength)
    If (r = -1) Or ErrorLevel {
        ErrorLevel = GetRawInputDeviceInfo call failed.`nReturn value: %r%`nErrorLevel: %ErrorLevel%`nLine: %A_LineNumber%`nLast Error: %A_LastError%
        Return ""
    }
    
    ;Get device name.
    VarSetCapacity(s, (iLength + 1) * 2)                         ;RIDI_DEVICENAME
    r := DllCall("GetRawInputDeviceInfo", "Ptr", h, "UInt", 0x20000007, "Str", s, "UInt*", iLength)
    If (r = -1) Or ErrorLevel {
        ErrorLevel = GetRawInputDeviceInfo call failed.`nReturn value: %r%`nErrorLevel: %ErrorLevel%`nLine: %A_LineNumber%`nLast Error: %A_LastError%
        Return ""
    }
    
    Return s
}

AHKHID_GetDevInfo(i, Flag, IsHandle = False) {
    Static uInfo, iLastHandle := 0
    
    ;Get index if i is handle
    h := IsHandle ? i : AHKHID_GetDevHandle(i)
    
    ;Check if the handle changed
    If (h = iLastHandle) ;It's the same device. No need to call again
        Return NumGet(uInfo, Flag, AHKHID_NumIsShort(Flag) ? "UShort" : "UInt")
    Else {
        
        ;Get device info buffer size.                           RIDI_DEVICEINFO
        r := DllCall("GetRawInputDeviceInfo", "Ptr", h, "UInt", 0x2000000b, "Ptr", 0, "UInt*", iLength)
        If (r = -1) Or ErrorLevel {
            ErrorLevel = GetRawInputDeviceInfo call failed.`nReturn value: %r%`nErrorLevel: %ErrorLevel%`nLine: %A_LineNumber%`nLast Error: %A_LastError%
            Return -1
        }
        
        ;Get device info
        VarSetCapacity(uInfo, iLength)
        NumPut(iLength, uInfo, 0, "UInt") ;Put length in struct RIDI_DEVICEINFO
        r := DllCall("GetRawInputDeviceInfo", "Ptr", h, "UInt", 0x2000000b, "Ptr", &uInfo, "UInt*", iLength)
        If (r = -1) Or ErrorLevel {
            ErrorLevel = GetRawInputDeviceInfo call failed.`nReturn value: %r%`nErrorLevel: %ErrorLevel%`nLine: %A_LineNumber%`nLast Error: %A_LastError%
            Return -1
        }
        
        ;Successful. Keep handle.
        iLastHandle := h
        
        ;Retrieve data
        Return NumGet(uInfo, Flag, AHKHID_NumIsShort(Flag) ? "UShort" : "UInt")
    }
    
    Return 0
}

AHKHID_AddRegister(UsagePage = False, Usage = False, Handle = False, Flags = 0) {
    Static uDev, iIndex := 0, iCount := 0
    
    ;Check if we just want the address
    If Not (UsagePage Or Usage Or Handle Or Flags)
        Return &uDev
    ;Check if we just want the count
    Else If (UsagePage = "Count")
        Return iCount
    ;Check if we're dimensioning the struct
    Else If UsagePage And Not (Usage Or Handle Or Flags) {
        iCount := UsagePage
        iIndex := 0
        VarSetCapacity(uDev, iCount * (8 + A_PtrSize))
        Return &uDev
    }
    
    ;Check if there's space before adding data to struct
    If (iIndex = iCount)
        Return -1    ;Full capacity
    
    ;Check if hwnd needs to be null. RIDEV_REMOVE, RIDEV_EXCLUDE
    Handle := ((Flags & 0x00000001) Or (Flags & 0x00000010)) ? 0 : Handle

    ;Put in struct
    NumPut(UsagePage, uDev, (iIndex * (8 + A_PtrSize)) + 0, "UShort")
    NumPut(Usage,     uDev, (iIndex * (8 + A_PtrSize)) + 2, "UShort")
    NumPut(Flags,     uDev, (iIndex * (8 + A_PtrSize)) + 4, "UInt")
    NumPut(Handle,    uDev, (iIndex * (8 + A_PtrSize)) + 8, "Ptr")
    
    ;Move to next slot
    iIndex += 1
    
    Return &uDev
}

AHKHID_Register(UsagePage = False, Usage = False, Handle = False, Flags = 0) {
    
    ;Check if we're using the AddRegister array or only a single struct
    If Not (UsagePage Or Usage Or Handle Or Flags) {
        
        ;Call
        r := DllCall("RegisterRawInputDevices", "Ptr", AHKHID_AddRegister(), "UInt", AHKHID_AddRegister("Count"), "UInt", 8 + A_PtrSize)
        
        ;Check for error
        If Not r {
            ErrorLevel = RegisterRawInputDevices call failed.`nReturn value: %r%`nErrorLevel: %ErrorLevel%`nLine: %A_LineNumber%`nLast Error: %A_LastError%
            Return -1
        }
        
    ;Build struct and call
    } Else {
        
        ;Prep var
        VarSetCapacity(uDev, (8 + A_PtrSize), 0)
        
        ;Check if hwnd needs to be null. RIDEV_REMOVE, RIDEV_EXCLUDE
        Handle := ((Flags & 0x00000001) Or (Flags & 0x00000010)) ? 0 : Handle
        
        NumPut(UsagePage, uDev, 0, "UShort")
        NumPut(Usage,     uDev, 2, "UShort")
        NumPut(Flags,     uDev, 4, "UInt")
        NumPut(Handle,    uDev, 8, "Ptr")
        
        ;Call
        r := DllCall("RegisterRawInputDevices", "Ptr", &uDev, "UInt", 1, "UInt", 8 + A_PtrSize)
        
        ;Check for error
        If Not r Or ErrorLevel {
            ErrorLevel = RegisterRawInputDevices call failed.`nReturn value: %r%`nErrorLevel: %ErrorLevel%`nLine: %A_LineNumber%`nLast Error: %A_LastError%
            Return -1
        }
    }
    
    Return 0
}

AHKHID_GetRegisteredDevs(ByRef uDev) {
    
    ;Get length
    VarSetCapacity(iCount, 4, 0)
    r := DllCall("GetRegisteredRawInputDevices", "Ptr", 0, "UInt*", iCount, "UInt", 8 + A_PtrSize)
    If ErrorLevel {
        ErrorLevel = GetRegisteredRawInputDevices call failed.`nReturn value: %r%`nErrorLevel: %ErrorLevel%`nLine: %A_LineNumber%`nLast Error: %A_LastError%
        Return -1
    }
    
    If (iCount > 0) {
        
        ;Prep var
        VarSetCapacity(uDev, iCount * (8 + A_PtrSize))
        
        ;Call
        r := DllCall("GetRegisteredRawInputDevices", "Ptr", &uDev, "UInt*", iCount, "UInt", 8 + A_PtrSize)
        If (r = -1) Or ErrorLevel {
            ErrorLevel = GetRegisteredRawInputDevices call failed.`nReturn value: %r%`nErrorLevel: %ErrorLevel%`nLine: %A_LineNumber%`nLast Error: %A_LastError%
            Return -1
        }
    }
    
    Return iCount
}

AHKHID_GetInputInfo(InputHandle, Flag) {
    Static uRawInput, iLastHandle := 0
    
    ;Check if it's the same handle
    If (InputHandle = iLastHandle) ;We can retrieve the data without having to call again
        Return NumGet(uRawInput, Flag, AHKHID_NumIsShort(Flag) ? (AHKHID_NumIsSigned(Flag) ? "Short" : "UShort") : (AHKHID_NumIsSigned(Flag) ? "Int" : (Flag = 8 ? "Ptr" : "UInt")))
    Else {    ;We need to get a fresh copy
        
        ;Get raw data size                                           RID_INPUT
        r := DllCall("GetRawInputData", "UInt", InputHandle, "UInt", 0x10000003, "Ptr", 0, "UInt*", iSize, "UInt", 8 + A_PtrSize * 2)
        If (r = -1) Or ErrorLevel {
            ErrorLevel = GetRawInputData call failed.`nReturn value: %r%`nErrorLevel: %ErrorLevel%`nLine: %A_LineNumber%`nLast Error: %A_LastError%
            Return -1
        }
        
        ;Prep var
        VarSetCapacity(uRawInput, iSize)
        
        ;Get raw data                                                RID_INPUT
        r := DllCall("GetRawInputData", "UInt", InputHandle, "UInt", 0x10000003, "Ptr", &uRawInput, "UInt*", iSize, "UInt", 8 + A_PtrSize * 2)
        If (r = -1) Or ErrorLevel {
            ErrorLevel = GetRawInputData call failed.`nReturn value: %r%`nErrorLevel: %ErrorLevel%`nLine: %A_LineNumber%`nLast Error: %A_LastError%
            Return -1
        } Else If (r <> iSize) {
            ErrorLevel = GetRawInputData did not return the correct size.`nSize returned: %r%`nSize allocated: %iSize%
            Return -1
        }
        
        ;Keep handle reference of current uRawInput
        iLastHandle := InputHandle
        
        ;Retrieve data
        Return NumGet(uRawInput, Flag, AHKHID_NumIsShort(Flag) ? (AHKHID_NumIsSigned(Flag) ? "Short" : "UShort") : (AHKHID_NumIsSigned(Flag) ? "Int" : (Flag = 8 ? "Ptr" : "UInt")))
    }
    
    Return 0
}

AHKHID_GetInputData(InputHandle, ByRef uData) {
    
    ;Get raw data size                                           RID_INPUT
    r := DllCall("GetRawInputData", "UInt", InputHandle, "UInt", 0x10000003, "Ptr", 0, "UInt*", iSize, "UInt", 8 + A_PtrSize * 2)
    If (r = -1) Or ErrorLevel {
        ErrorLevel = GetRawInputData call failed.`nReturn value: %r%`nErrorLevel: %ErrorLevel%`nLine: %A_LineNumber%`nLast Error: %A_LastError%
        Return -1
    }
    
    ;Prep var
    VarSetCapacity(uRawInput, iSize)
    
    ;Get raw data                                                RID_INPUT
    r := DllCall("GetRawInputData", "UInt", InputHandle, "UInt", 0x10000003, "Ptr", &uRawInput, "UInt*", iSize, "UInt", 8 + A_PtrSize * 2)
    If (r = -1) Or ErrorLevel {
        ErrorLevel = GetRawInputData call failed.`nReturn value: %r%`nErrorLevel: %ErrorLevel%`nLine: %A_LineNumber%`nLast Error: %A_LastError%
        Return -1
    } Else If (r <> iSize) {
        ErrorLevel = GetRawInputData did not return the correct size.`nSize returned: %r%`nSize allocated: %iSize%
        Return -1
    }
    
    ;Get the size of each HID input and the number of them
    iSize   := NumGet(uRawInput, 8 + A_PtrSize * 2 + 0, "UInt") ;ID_HID_SIZE
    iCount  := NumGet(uRawInput, 8 + A_PtrSize * 2 + 4, "UInt") ;ID_HID_COUNT
    
    ;Allocate memory
    VarSetCapacity(uData, iSize * iCount)
    
    ;Copy bytes
    DllCall("RtlMoveMemory", UInt, &uData, UInt, &uRawInput + 8 + A_PtrSize * 2 + 8, UInt, iSize * iCount)
    
    Return (iSize * iCount)
}

;Internal use only
AHKHID_NumIsShort(ByRef Flag) {
    If (Flag & 0x0100) {
        Flag ^= 0x0100    ;Remove it
        Return True
    } Return False
}

;Internal use only
AHKHID_NumIsSigned(ByRef Flag) {
    If (Flag & 0x1000) {
        Flag ^= 0x1000    ;Remove it
        Return True
    } Return False
}

По вопросам возмездной помощи пишите на E-Mail: serzh82saratov@mail.ru Telegram: https://t.me/sergiol982
Win10x64 AhkSpy, Hotkey, ClockGui

2 (изменено: serzh82saratov, 2020-09-05 22:22:19)

Re: AHK: Работа с джойстиками через HID

Данный класс связывает метки или функции с нажатиями. Стики работают как крестовины в четырёх направлениях, курки (триггеры) как обычные кнопки, или можно получать сообщения от каждой оси. Более подробно в комментарии в начале класса.
Можно связать отпускания кнопок.
Проверено на Microsoft Xbox 360 Controller for Windows, у него XInput. С DirectInput девайсами работать не будет.

+ Пример 1 - проверка нажатий, вывод всех нажатых кнопок

#SingleInstance Force
#NoEnv
ListLines Off
SetBatchLines -1 

Joy.Register()
Return

Esc::ExitApp 
	
Joy_OnJoy:
	for Key in Joy.State, List := "" 
		List .= Key "`n"
	ToolTip % List
	Return
+ Пример 2 - проверка нажатий, приведены все возможные имена меток

#SingleInstance Force
#NoEnv
ListLines Off
SetBatchLines -1
 
Joy.Register()
Return

Esc::ExitApp

Joy_B_A:
Joy_B_B:
Joy_B_X:
Joy_B_Y:
Joy_B_L:
Joy_B_R:
Joy_B_Back:
Joy_B_Start:
Joy_B_GUIDE:

Joy_L_Up:
Joy_L_Down:
Joy_L_Right:
Joy_L_Left:
Joy_L_Button:
Joy_L_Trigger:

Joy_R_Up:
Joy_R_Down:
Joy_R_Left:
Joy_R_Right:
Joy_R_Button:
Joy_R_Trigger:

Joy_C_Up:
Joy_C_Down:
Joy_C_Left:
Joy_C_Right:
Joy_C_Up_Left:
Joy_C_Up_Right:
Joy_C_Down_Left:
Joy_C_Down_Right:


Joy_B_A_Up:
Joy_B_B_Up:
Joy_B_X_Up:
Joy_B_Y_Up:
Joy_B_L_Up:
Joy_B_R_Up:
Joy_B_Back_Up:
Joy_B_Start_Up:
Joy_B_GUIDE_Up:

Joy_L_Left_Up:
Joy_L_Right_Up:
Joy_L_Up_Up:
Joy_L_Down_Up:
Joy_L_Button_Up:
Joy_L_Trigger_Up:

Joy_R_Left_Up:
Joy_R_Right_Up:
Joy_R_Up_Up:
Joy_R_Down_Up:
Joy_R_Button_Up:
Joy_R_Trigger_Up:

Joy_C_Left_Up:
Joy_C_Right_Up:
Joy_C_Up_Up:
Joy_C_Down_Up:
Joy_C_Up_Left_Up:
Joy_C_Up_Right_Up:
Joy_C_Down_Left_Up:
Joy_C_Down_Right_Up:
	ToolTip % A_ThisLabel 
	Return
+ Пример 3 - Левый стик и крестовина отправляют стрелку вверх и вниз

#SingleInstance Force
#NoEnv
ListLines Off
SetBatchLines -1 
 
Joy.Register()
Return

Joy_L_Down:
Joy_C_Down:
	SendInput {Down}
	Return 
	; просто как пример в виде функций 
Joy_L_Up() {
	SendInput {Up}
}
Joy_C_Up() {
	SendInput {Up}
}
+ Пример 4 - оба стика отправляют стрелки клавиатуры, с паузой перед спамом после первого нажатия, крест управляет курсором, кнопка "А" как левая кнопка мыши

#SingleInstance Force
#NoEnv
ListLines Off
SetBatchLines -1 

Joy.Register()
Return

Esc::ExitApp 

Joy_B_A:  
	Send {LButton Down}
	Return 
Joy_B_A_Up:  
	Send {LButton Up}
	Return
	
Joy_L_Up:  
Joy_L_Down: 
Joy_L_Right: 	
Joy_L_Left: 

Joy_R_Up:  
Joy_R_Down: 
Joy_R_Left: 
Joy_R_Right:  
	start := A_TickCount
	key := SubStr(A_ThisLabel, 7)
	name := SubStr(A_ThisLabel, 5)
	While Joy.State[name]
	{
		SendInput {%key%}
		While A_TickCount - start < 300 && Joy.State[name]
			Sleep 1
		Sleep 50
	}
	Return  

Joy_C_Up:
Joy_C_Down:
Joy_C_Left:
Joy_C_Right:
Joy_C_Up_Left:
Joy_C_Up_Right:
Joy_C_Down_Left:
Joy_C_Down_Right: 
	name := SubStr(A_ThisLabel, 7)
	XYMove(5, name, x, y)
	While Joy.State["C_" name]
		MouseMove, x, y, 1, R
	Return
	
XYMove(step, dir, ByRef x, ByRef y) {
	Static odir := {"Up":[0, -1], "Down":[0, 1], "Left":[-1, 0], "Right":[1, 0]
			, "Up_Right":[1, -1], "Down_Right":[1, 1], "Up_Left":[-1, -1], "Down_Left":[-1, 1]} 
	x := odir[dir][1] * step
	y := odir[dir][2] * step
}
+ Пример 5 - вывод всех аналогов и кнопок

#SingleInstance Force
#NoEnv
ListLines Off
SetBatchLines -1 

Joy.AxisReg.LX := 1
Joy.AxisReg.LY := 1
Joy.AxisReg.RX := 1
Joy.AxisReg.RY := 1
Joy.AxisReg.Triggers := 1
Joy.Register() 
Return

Joy_A_LX:
Joy_A_LY:
Joy_A_RX:
Joy_A_RY:
Joy_A_Triggers:
	Axis := "LX:" Joy.Axis.LX "`nLY:" Joy.Axis.LY 
		. "`nRX:" Joy.Axis.RX "`nRY:" Joy.Axis.RY 
		. "`nTriggers:" Joy.Axis.Triggers 
	Gosub ToolTip
	Return
	
Joy_OnJoy:
	for Key in Joy.State, List := "" 
		List .= Key "`n"
	Gosub ToolTip
	Return
	
ToolTip:
	ToolTip % Axis "`n`n" List
	Return
+ Class Joy, это код который надо добавить в конце каждого из примеров


Class Joy {
/*
		v1.20		21:59 05.09.2020
		Автор - serzh82saratov
		E-Mail: serzh82saratov@mail.ru
		Обсуждение - http://forum.script-coding.com/viewtopic.php?pid=141541#p141541
		Связывает метки или функции с нажатиями джойстика.
		Аналоговые стики определяют четыре положения, и работают как обычные кнопки, или смотри префикс "A" ниже.
		Аналоговые кнопки, работают как обычные кнопки но без возможности одновременного удержания, или смотри префикс "A" ниже.
		Можно связать отпускания кнопок.

		Register - запускает отслеживание нажатий, в первом параметре задаётся префикс меток или функций. По умолчанию "Joy_", может быть пусто.
		UnRegister - завершает отслеживание нажатий.
		Joy.Hook - возвращает включен перехват или нет.
		Свойство "Correction" задаёт для аналоговых кнопок отступ от начального положения,
			после которого статус считается нажато или отпущено. По умолчанию = 110 из 128. 
			При меньшем значении у стиков может быть одновременно определён статус - например нажато вверх и влево.
		Joy.State[keyname] - даёт статус нажато или отпущено. 1 или пусто. keyname приведены далее.
		Joy.StateCross возвращает имя одного из 8-ми нажатых положений крестовины или пусто.

		Все имена для меток или функций, по аналогии с джойстиком X-Box. Постфикс "_Up" реакция на отпускание.
		Префикс "L" - левый стик, нажатие на стик, левый тригер.
		Префикс "R" - тоже только для правого стика.
		Префикс "С" - крестовина.
		Префикс "B" - остальные "обычные" кнопки.

		OnJoy - будет запускаться при любом изменении статуса кнопок.

		B_A       
		B_B
		B_X
		B_Y
		B_L
		B_R
		B_Back
		B_Start
		B_Guide

		L_Up
		L_Down
		L_Right
		L_Left
		L_Button
		L_Trigger

		R_Up
		R_Down
		R_Left
		R_Right
		R_Button
		R_Trigger

		C_Up
		C_Down
		C_Left
		C_Right
		C_Up_Left
		C_Up_Right
		C_Down_Left
		C_Down_Right

		Префикс "A" - запускаются при подписке на аналоговые оси.
		
		A_LX
		A_LY
		A_RX
		A_RY
		A_Triggers
		
		Joy.Axis[AxisName] содержит значения. 0 - середина, -128 - минимум, 127 - максимум.
			В случае подписки, ось не работает как кнопки.
			Подписка на оси:
			Joy.AxisReg.LX := 1
			Joy.AxisReg.LY := 1
			Joy.AxisReg.RX := 1
			Joy.AxisReg.RY := 1
			Joy.AxisReg.Triggers := 1
*/
	Static WM_INPUT := 0x00FF, RIDEV_INPUTSINK := 0x00000100, Correction := 110
		, State := {}, prDataState := {}, StateCross, AxisReg := {}, Axis := {}

	Register(UserPrefix = "Joy_")  {
		If this.HIDRegister(1, 5, A_ScriptHWND, this.RIDEV_INPUTSINK) = -1
			Throw Exception(ErrorLevel, -1)
		this.UserPrefix := UserPrefix
		If !this.fOnInput
			this.fOnInput := ObjBindMethod(this, "OnInput")
		If !this.fOnGuide
			this.fOnGuide := ObjBindMethod(this, "OnGuide")
		OnMessage(this.WM_INPUT, this.fOnInput, 2)
		this.Hotkey("vk07", this.fOnGuide, "UseErrorLevel On")
		this.Hotkey("vk07 Up", this.fOnGuide, "UseErrorLevel On")
		this.Hook := 1
	}
	UnRegister()  {
		this.Hook := 0
		OnMessage(this.WM_INPUT, this.fOnInput, 0)
		this.Hotkey("vk07", this.fOnGuide, "UseErrorLevel Off")
		this.Hotkey("vk07 Up", this.fOnGuide, "UseErrorLevel Off")
		this.State := {}
		this.Axis := {}
		this.prDataState := {}
		this.StateCross := ""
		this.HIDRegister()
	}
	OnGuide()  {
		Up := StrLen(A_ThisHotkey) > 4
		Up ? this.State.Delete("B_Guide") : this.State["B_Guide"] := 1
		Try SetTimer, % this.UserPrefix "OnJoy", -10
		Try SetTimer, % this.UserPrefix "B_Guide" (Up ? "_Up" : ""), -10
	}
	OnInput(wParam, lParam) {
		Critical
		Static oDir := {"LX":["L_Left", "L_Right"], "LY":["L_Up", "L_Down"]
					 , "RX":["R_Left", "R_Right"], "RY":["R_Up", "R_Down"]
					 , "Triggers":["R_Trigger", "L_Trigger"]}
		Static oButtons := {Buttons: {"B_A": 1
							  , "B_B": 2
							  , "B_X": 4
							  , "B_Y": 8
							  , "B_L": 16
							  , "B_R": 32
							  , "B_Back": 64
							  , "B_Start": 128}
						  , Crs:{"L_Button": 1, "R_Button": 2}}
		Static oCross := { 4: "C_Up"
						,  8: "C_Up_Right"
						, 12: "C_Right"
						, 16: "C_Down_Right"
						, 20: "C_Down"
						, 24: "C_Down_Left"
						, 28: "C_Left"
						, 32: "C_Up_Left"}

		If this.HIDGetInputData(lParam, uData) = -1
			Return
		LX := NumGet(&uData, 2, "UChar")
		LY := NumGet(&uData, 4, "UChar")
		RX := NumGet(&uData, 6, "UChar")
		RY := NumGet(&uData, 8, "UChar")
		Triggers := NumGet(&uData, 10, "UChar")
		Buttons := NumGet(&uData, 11, "UChar")
		Crs := NumGet(&uData, 12, "UChar")
		Min := 128 - this.Correction
		Max := 128 + this.Correction
		Launch := {}
		; ToolTip % LX "`n" LY "`n" RX "`n" RY "`n" Triggers "`n" Buttons "`n" Cross "`n" A_TickCount, 222, 222, 2
		for key, name in oDir
		{
			If (%key% = this.prDataState[key])
				Continue
			this.prDataState[key] := %key%

			If this.AxisReg[key]
			{
				If !LaunchAnalog
					LaunchAnalog := {}
				this.Axis[key] := %key% - 128
				LaunchAnalog.Push(key)
				Continue
			}
			If (!this.State[name[1]] && %key% < Min)
				this.State[name[1]] := 1, Launch.Push(name[1])
			Else If (this.State[name[1]] && %key% > Min)
				this.State.Delete(name[1]), Launch.Push(name[1] "_Up")

			If (!this.State[name[2]] && %key% > Max)
				this.State[name[2]] := 1, Launch.Push(name[2])
			Else If (this.State[name[2]] && %key% < Max)
				this.State.Delete(name[2]), Launch.Push(name[2] "_Up")
		}
		for key, arr in oButtons
		{
			If (%key% = this.prDataState[key])
				Continue
			this.prDataState[key] := %key%

			for b, v in arr
			{
				If (!this.State[b] && %key% & v)
					this.State[b] := 1, Launch.Push(b)
				Else If (this.State[b]) && !(%key% & v)
					this.State.Delete(b), Launch.Push(b "_Up")
			}
		}
		Crs := Crs - (Crs & 1) - (Crs & 2)
		If (Crs != this.prDataState["Cross"])
		{
			this.prDataState["Cross"] := Crs
			If b := oCross[Crs]
				Launch.Push(b)
			If this.StateCross ; prior
				Launch.Push(this.StateCross "_Up"), this.State.Delete(this.StateCross)
			this.StateCross := b, b ? this.State[b] := 1 : this.State.Delete(b)
		}
		If Launch.Length()
			Try SetTimer, % this.UserPrefix "OnJoy", -10
		for key, v in Launch
			Try SetTimer, % this.UserPrefix v, -10
		for key, v in LaunchAnalog
			Try SetTimer, % this.UserPrefix "A_" v, -10
	}
	Hotkey(HK, Label, Option = "") {
		Hotkey, %HK%, %Label%, %Option%
		Return ErrorLevel
	}

	; Следующие функции взяты из библиотеки AHKHID https://autohotkey.com/board/topic/38015-ahkhid-an-ahk-implementation-of-the-hid-functions/

	HIDGetInputData(InputHandle, ByRef uData) {
		r := DllCall("GetRawInputData", "UInt", InputHandle, "UInt", 0x10000003, "Ptr", 0, "UInt*", iSize, "UInt", 8 + A_PtrSize * 2)
		If (r = -1) Or ErrorLevel {
			ErrorLevel = GetRawInputData call failed.`nReturn value: %r%`nErrorLevel: %ErrorLevel%`nLine: %A_LineNumber%`nLast Error: %A_LastError%
			Return -1
		}
		VarSetCapacity(uRawInput, iSize)
		r := DllCall("GetRawInputData", "UInt", InputHandle, "UInt", 0x10000003, "Ptr", &uRawInput, "UInt*", iSize, "UInt", 8 + A_PtrSize * 2)
		If (r = -1) Or ErrorLevel {
			ErrorLevel = GetRawInputData call failed.`nReturn value: %r%`nErrorLevel: %ErrorLevel%`nLine: %A_LineNumber%`nLast Error: %A_LastError%
			Return -1
		} Else If (r <> iSize) {
			ErrorLevel = GetRawInputData did not return the correct size.`nSize returned: %r%`nSize allocated: %iSize%
			Return -1
		}
		iSize   := NumGet(uRawInput, 8 + A_PtrSize * 2 + 0, "UInt") ;ID_HID_SIZE
		iCount  := NumGet(uRawInput, 8 + A_PtrSize * 2 + 4, "UInt") ;ID_HID_COUNT
		VarSetCapacity(uData, iSize * iCount)
		DllCall("RtlMoveMemory", UInt, &uData, UInt, &uRawInput + 8 + A_PtrSize * 2 + 8, UInt, iSize * iCount)
		Return (iSize * iCount)
	}
	HIDRegister(UsagePage = False, Usage = False, Handle = False, Flags = 0) {
		If Not (UsagePage Or Usage Or Handle Or Flags) {
			r := DllCall("RegisterRawInputDevices", "Ptr", this.HIDAddRegister(), "UInt", this.HIDAddRegister("Count"), "UInt", 8 + A_PtrSize)
			If Not r {
				ErrorLevel = RegisterRawInputDevices call failed.`nReturn value: %r%`nErrorLevel: %ErrorLevel%`nLine: %A_LineNumber%`nLast Error: %A_LastError%
				Return -1
			}
		} Else {
			VarSetCapacity(uDev, (8 + A_PtrSize), 0)
			Handle := ((Flags & 0x00000001) Or (Flags & 0x00000010)) ? 0 : Handle
			NumPut(UsagePage, uDev, 0, "UShort")
			NumPut(Usage,     uDev, 2, "UShort")
			NumPut(Flags,     uDev, 4, "UInt")
			NumPut(Handle,    uDev, 8, "Ptr")
			r := DllCall("RegisterRawInputDevices", "Ptr", &uDev, "UInt", 1, "UInt", 8 + A_PtrSize)
			If Not r Or ErrorLevel {
				ErrorLevel = RegisterRawInputDevices call failed.`nReturn value: %r%`nErrorLevel: %ErrorLevel%`nLine: %A_LineNumber%`nLast Error: %A_LastError%
				Return -1
			}
		}
		Return 0
	}
	HIDAddRegister(UsagePage = False, Usage = False, Handle = False, Flags = 0) {
		Static uDev, iIndex := 0, iCount := 0
		If Not (UsagePage Or Usage Or Handle Or Flags)
			Return &uDev
		Else If (UsagePage = "Count")
			Return iCount
		Else If UsagePage And Not (Usage Or Handle Or Flags) {
			iCount := UsagePage
			iIndex := 0
			VarSetCapacity(uDev, iCount * (8 + A_PtrSize))
			Return &uDev
		}
		If (iIndex = iCount)
			Return -1
		Handle := ((Flags & 0x00000001) Or (Flags & 0x00000010)) ? 0 : Handle
		NumPut(UsagePage, uDev, (iIndex * (8 + A_PtrSize)) + 0, "UShort")
		NumPut(Usage,     uDev, (iIndex * (8 + A_PtrSize)) + 2, "UShort")
		NumPut(Flags,     uDev, (iIndex * (8 + A_PtrSize)) + 4, "UInt")
		NumPut(Handle,    uDev, (iIndex * (8 + A_PtrSize)) + 8, "Ptr")
		iIndex += 1
		Return &uDev
	}
}
По вопросам возмездной помощи пишите на E-Mail: serzh82saratov@mail.ru Telegram: https://t.me/sergiol982
Win10x64 AhkSpy, Hotkey, ClockGui

3

Re: AHK: Работа с джойстиками через HID

serzh82saratov, не работают ни тот, ни другой скрипты.

Первый выдает ошибку. 
Error: Call to nonexistent function
Specifically: AHKHID_UseConstants()

Второй просто не реагирует на нажатия джойстика.

4

Re: AHK: Работа с джойстиками через HID

Snegovik2 пишет:

Второй просто не реагирует на нажатия джойстика.

А что за джойстик? Точно не работает, рядом с курсором должно появляться сообщение.

Тогда надо запустить первый, для этого надо добавить код который под спойлером.

По вопросам возмездной помощи пишите на E-Mail: serzh82saratov@mail.ru Telegram: https://t.me/sergiol982
Win10x64 AhkSpy, Hotkey, ClockGui

5

Re: AHK: Работа с джойстиками через HID

serzh82saratov, понятно.
А как это сделать ?
Как запустить первый код, чтобы он работал без ошибок ?

Библиотеку AHKHID.ahk  я кидал в папку рядом со скриптом. Или как-то по-другому надо подключать ?
(джойстик у меня - это обычный геймпад с крестовиной и кнопками, подключенный через USB.)

6

Re: AHK: Работа с джойстиками через HID

Snegovik2 пишет:

Библиотеку AHKHID.ahk  я кидал в папку рядом со скриптом

Нет просто код скопировать в скрипт. Ладно, просто запустите это.

Snegovik2 пишет:

джойстик у меня - это обычный геймпад с крестовиной и кнопками, подключенный через USB

Отличное описание, это очень похоже на джойстик. Может у него ещё есть название, модель, ссылка на описание.


; http://forum.script-coding.com/viewtopic.php?pid=141541#p141541


#SingleInstance Force
#NoEnv
ListLines Off
SetBatchLines -1
 
Joy.Register("Joy_")
Return

Esc::ExitApp

Joy_L_Left: 
Joy_L_Right: 
Joy_L_Up:  
Joy_L_Down: 
Joy_L_Left_Up: 
Joy_L_Right_Up:  
Joy_L_Up_Up: 
Joy_L_Down_Up: 

Joy_R_Left: 
Joy_R_Right: 
Joy_R_Up:  
Joy_R_Down: 
Joy_R_Left_Up: 
Joy_R_Right_Up:  
Joy_R_Up_Up: 
Joy_R_Down_Up: 

Joy_L_Button:
Joy_R_Button:
Joy_L_Button_Up:
Joy_R_Button_Up:

Joy_L_Trigger:
Joy_R_Trigger:
Joy_L_Trigger_Up:
Joy_R_Trigger_Up:

Joy_Button_A:
Joy_Button_B:
Joy_Button_X:
Joy_Button_Y:
Joy_Button_L:
Joy_Button_R:
Joy_Button_Back:
Joy_Button_Start:
Joy_Button_A_Up:
Joy_Button_B_Up:
Joy_Button_X_Up:
Joy_Button_Y_Up:
Joy_Button_L_Up:
Joy_Button_R_Up:
Joy_Button_Back_Up:
Joy_Button_Start_Up: 

Joy_Cross_Left:
Joy_Cross_Right:
Joy_Cross_Up:
Joy_Cross_Down:
Joy_Cross_Left_Up:
Joy_Cross_Right_Up:
Joy_Cross_Up_Up:
Joy_Cross_Down_Up:
	ToolTip % A_ThisLabel
	Return
	
 
 
 
Class Joy { 
/* 
		v1.02		21:06 08.08.2020
		Автор - serzh82saratov
		E-Mail: serzh82saratov@mail.ru
		Обсуждение - http://forum.script-coding.com/viewtopic.php?pid=141541#p141541
		Связывает метки или функции с нажатиями джойстика. 
		Аналоговые стики работают как крестовины, аналоговые кнопки, как обычные кнопки но без возможности одновременного удержания. 
		Можно связать отпускания кнопок.

		Register - запускает отслеживание нажатий, в первом параметре задаётся префикс меток или функций.
		UnRegister - завершает отслеживание нажатий.
		Свойство "Correction" задаёт для аналоговых кнопок отступ от начального положения, 
		после которого статус считается нажато или отпущено. По умолчанию = 70 из 128.
		Joy.State.namekey - даёт статус нажато или отпущено.
		
		Все имена для меток, по аналогии с джойстиком X-Box. Постфикс "_Up" реакция на отпускание.

		L_Left
		L_Right
		L_Up
		L_Down
		L_Button

		R_Left
		R_Right
		R_Up
		R_Down
		R_Button

		L_Trigger
		R_Trigger

		Cross_Left
		Cross_Right
		Cross_Up
		Cross_Down 

		Button_A
		Button_B
		Button_X
		Button_Y
		Button_L
		Button_R
		Button_Back
		Button_Start 
*/
	Static WM_INPUT := 0x00FF, RIDEV_INPUTSINK := 0x00000100, Correction := 70, State := {}
	
	Register(FuncName = "")  {
		this.HIDRegister(1, 5, A_ScriptHWND, this.RIDEV_INPUTSINK)
		this.FuncName := FuncName
		If !this.Message
			this.Message := ObjBindMethod(this, "Input")
		OnMessage(this.WM_INPUT, this.Message, 8)
	}
	UnRegister()  {
		OnMessage(this.WM_INPUT, this.Message, 0)
		this.HIDRegister()
	}
	Input(wParam, lParam) {
	    Critical  
		Static oDir := {"LX":["L_Left", "L_Right"], "LY":["L_Up", "L_Down"]
					 , "RX":["R_Left", "R_Right"], "RY":["R_Up", "R_Down"]
					 , "Triggers":["R_Trigger", "L_Trigger"]} 
		Static oButtons := [["Buttons", "Button_A", 1]
						  , ["Buttons", "Button_B", 2]
						  , ["Buttons", "Button_X", 4]
						  , ["Buttons", "Button_Y", 8]
						  , ["Buttons", "Button_L", 16]
						  , ["Buttons", "Button_R", 32]
						  , ["Buttons", "Button_Back", 64]
						  , ["Buttons", "Button_Start", 128]
						  ,   ["Cross", "L_Button", 1]
						  ,   ["Cross", "R_Button", 2]] 
		Static oCross := [["Cross_Left", 28]
						, ["Cross_Right", 12]
						, ["Cross_Up", 4]
						, ["Cross_Down", 20]]
 
		this.HIDGetInputData(lParam, uData)
		
		LX := NumGet(&uData, 2, "UChar")
		LY := NumGet(&uData, 4, "UChar")
		RX := NumGet(&uData, 6, "UChar")
		RY := NumGet(&uData, 8, "UChar")
		Triggers := NumGet(&uData, 10, "UChar")
		Buttons := NumGet(&uData, 11, "UChar")
		Cross := NumGet(&uData, 12, "UChar")
		Min := 128 - this.Correction
		Max := 128 + this.Correction
		Launch := {}
		ToolTip % LX "`n" LY "`n" RX "`n" RY "`n" Triggers "`n" Buttons "`n" Cross, 222, 222, 2
		for key, name in oDir
		{ 
			If (!this.State[name[1]] && %key% < Min)
				this.State[name[1]] := 1, Launch.Push(name[1])
			Else If (this.State[name[1]] && %key% > Min)
				this.State[name[1]] := 0, Launch.Push(name[1] "_Up")
			
			If (!this.State[name[2]] && %key% > Max)
				this.State[name[2]] := 1, Launch.Push(name[2])
			Else If (this.State[name[2]] && %key% < Max)
				this.State[name[2]] := 0, Launch.Push(name[2] "_Up")
		} 
		for key, v in oButtons
		{ 
			n := v[1] 
			If (!this.State[v[2]] && %n% & v[3])
				this.State[v[2]] := 1, Launch.Push(v[2])
			Else If (this.State[v[2]]) && !(%n% & v[3])
				this.State[v[2]] := 0, Launch.Push(v[2] "_Up") 
		} 
		for key, v in oCross
		{  
			If (!this.State[v[1]] && Cross = v[2])
				this.State[v[1]] := 1, Launch.Push(v[1])
			Else If (this.State[v[1]] && Cross != v[2])
				this.State[v[1]] := 0, Launch.Push(v[1] "_Up") 
		}  
		for k, v in Launch
			Try SetTimer, % this.FuncName v, -10
	}  
	 
	; Следующие функции взяты из библиотеки AHKHID https://autohotkey.com/board/topic/38015-ahkhid-an-ahk-implementation-of-the-hid-functions/
	
	HIDGetInputData(InputHandle, ByRef uData) { 
	    ;Get raw data size                                           RID_INPUT
	    r := DllCall("GetRawInputData", "UInt", InputHandle, "UInt", 0x10000003, "Ptr", 0, "UInt*", iSize, "UInt", 8 + A_PtrSize * 2)
	    If (r = -1) Or ErrorLevel {
	        ErrorLevel = GetRawInputData call failed.`nReturn value: %r%`nErrorLevel: %ErrorLevel%`nLine: %A_LineNumber%`nLast Error: %A_LastError%
	        Return -1
	    }
	    
	    ;Prep var
	    VarSetCapacity(uRawInput, iSize)
	    
	    ;Get raw data                                                RID_INPUT
	    r := DllCall("GetRawInputData", "UInt", InputHandle, "UInt", 0x10000003, "Ptr", &uRawInput, "UInt*", iSize, "UInt", 8 + A_PtrSize * 2)
	    If (r = -1) Or ErrorLevel {
	        ErrorLevel = GetRawInputData call failed.`nReturn value: %r%`nErrorLevel: %ErrorLevel%`nLine: %A_LineNumber%`nLast Error: %A_LastError%
	        Return -1
	    } Else If (r <> iSize) {
	        ErrorLevel = GetRawInputData did not return the correct size.`nSize returned: %r%`nSize allocated: %iSize%
	        Return -1
	    }
	    
	    ;Get the size of each HID input and the number of them
	    iSize   := NumGet(uRawInput, 8 + A_PtrSize * 2 + 0, "UInt") ;ID_HID_SIZE
	    iCount  := NumGet(uRawInput, 8 + A_PtrSize * 2 + 4, "UInt") ;ID_HID_COUNT
	    
	    ;Allocate memory
	    VarSetCapacity(uData, iSize * iCount)
	    
	    ;Copy bytes
	    DllCall("RtlMoveMemory", UInt, &uData, UInt, &uRawInput + 8 + A_PtrSize * 2 + 8, UInt, iSize * iCount)
	    
	    Return (iSize * iCount)
	} 
	HIDAddRegister(UsagePage = False, Usage = False, Handle = False, Flags = 0) {
	    Static uDev, iIndex := 0, iCount := 0
	    
	    ;Check if we just want the address
	    If Not (UsagePage Or Usage Or Handle Or Flags)
	        Return &uDev
	    ;Check if we just want the count
	    Else If (UsagePage = "Count")
	        Return iCount
	    ;Check if we're dimensioning the struct
	    Else If UsagePage And Not (Usage Or Handle Or Flags) {
	        iCount := UsagePage
	        iIndex := 0
	        VarSetCapacity(uDev, iCount * (8 + A_PtrSize))
	        Return &uDev
	    }
	    
	    ;Check if there's space before adding data to struct
	    If (iIndex = iCount)
	        Return -1    ;Full capacity
	    
	    ;Check if hwnd needs to be null. RIDEV_REMOVE, RIDEV_EXCLUDE
	    Handle := ((Flags & 0x00000001) Or (Flags & 0x00000010)) ? 0 : Handle
	
	    ;Put in struct
	    NumPut(UsagePage, uDev, (iIndex * (8 + A_PtrSize)) + 0, "UShort")
	    NumPut(Usage,     uDev, (iIndex * (8 + A_PtrSize)) + 2, "UShort")
	    NumPut(Flags,     uDev, (iIndex * (8 + A_PtrSize)) + 4, "UInt")
	    NumPut(Handle,    uDev, (iIndex * (8 + A_PtrSize)) + 8, "Ptr")
	    
	    ;Move to next slot
	    iIndex += 1
	    
	    Return &uDev
	} 
	HIDRegister(UsagePage = False, Usage = False, Handle = False, Flags = 0) { 
	    ;Check if we're using the AddRegister array or only a single struct
	    If Not (UsagePage Or Usage Or Handle Or Flags) {
	        
	        ;Call
	        r := DllCall("RegisterRawInputDevices", "Ptr", this.HIDAddRegister(), "UInt", this.HIDAddRegister("Count"), "UInt", 8 + A_PtrSize)
	        
	        ;Check for error
	        If Not r {
	            ErrorLevel = RegisterRawInputDevices call failed.`nReturn value: %r%`nErrorLevel: %ErrorLevel%`nLine: %A_LineNumber%`nLast Error: %A_LastError%
	            Return -1
	        }
	        
	    ;Build struct and call
	    } Else {
	        
	        ;Prep var
	        VarSetCapacity(uDev, (8 + A_PtrSize), 0)
	        
	        ;Check if hwnd needs to be null. RIDEV_REMOVE, RIDEV_EXCLUDE
	        Handle := ((Flags & 0x00000001) Or (Flags & 0x00000010)) ? 0 : Handle
	        
	        NumPut(UsagePage, uDev, 0, "UShort")
	        NumPut(Usage,     uDev, 2, "UShort")
	        NumPut(Flags,     uDev, 4, "UInt")
	        NumPut(Handle,    uDev, 8, "Ptr")
	        
	        ;Call
	        r := DllCall("RegisterRawInputDevices", "Ptr", &uDev, "UInt", 1, "UInt", 8 + A_PtrSize)
	        
	        ;Check for error
	        If Not r Or ErrorLevel {
	            ErrorLevel = RegisterRawInputDevices call failed.`nReturn value: %r%`nErrorLevel: %ErrorLevel%`nLine: %A_LineNumber%`nLast Error: %A_LastError%
	            Return -1
	        }
	    } 
	    Return 0
	}
} 
По вопросам возмездной помощи пишите на E-Mail: serzh82saratov@mail.ru Telegram: https://t.me/sergiol982
Win10x64 AhkSpy, Hotkey, ClockGui

7

Re: AHK: Работа с джойстиками через HID

serzh82saratov, нет не работает
Ошибки не выдает, но и на нажатия кнопок не реагирует.

Джойстик называется -  "Геймпад Speedlink Thunderstrike Black"

8

Re: AHK: Работа с джойстиками через HID

Видимо мой код не работает с DirectInput. У меня Microsoft Xbox 360 Controller for Windows, у него XInput.

По вопросам возмездной помощи пишите на E-Mail: serzh82saratov@mail.ru Telegram: https://t.me/sergiol982
Win10x64 AhkSpy, Hotkey, ClockGui

9

Re: AHK: Работа с джойстиками через HID

serzh82saratov,
Но ведь вот этот код - работает:

Loop {
JoyY := GetKeyState("JoyY")
if (JoyY = 0) 
{
msgbox send up
}
sleep 2
}

Единственная проблема - что он - несколько раз нажимает на кнопку UP, а не один раз.
И я спрашивал - как поставить ограничение по времени на это нажатие ?

10

Re: AHK: Работа с джойстиками через HID

Мой код делает то что вам надо, но с DirectInput джойстиками он не работает.

По вопросам возмездной помощи пишите на E-Mail: serzh82saratov@mail.ru Telegram: https://t.me/sergiol982
Win10x64 AhkSpy, Hotkey, ClockGui

11

Re: AHK: Работа с джойстиками через HID

Подскажите, вот в этом коде - как назначить кнопки на команды джойстика?


; http://forum.script-coding.com/viewtopic.php?pid=141541#p141541


#SingleInstance Force
#NoEnv
ListLines Off
SetBatchLines -1
 
Joy.Register("Joy_")
Return

Esc::ExitApp

Joy_L_Left: 
Joy_L_Right: 
Joy_L_Up:  
Joy_L_Down: 
Joy_L_Left_Up: 
Joy_L_Right_Up:  
Joy_L_Up_Up: 
Joy_L_Down_Up: 

Joy_R_Left: 
Joy_R_Right: 
Joy_R_Up:  
Joy_R_Down: 
Joy_R_Left_Up: 
Joy_R_Right_Up:  
Joy_R_Up_Up: 
Joy_R_Down_Up: 

Joy_L_Button:
Joy_R_Button:
Joy_L_Button_Up:
Joy_R_Button_Up:

Joy_L_Trigger:
Joy_R_Trigger:
Joy_L_Trigger_Up:
Joy_R_Trigger_Up:

Joy_Button_A:
Joy_Button_B:
Joy_Button_X:
Joy_Button_Y:
Joy_Button_L:
Joy_Button_R:
Joy_Button_Back:
Joy_Button_Start:
Joy_Button_A_Up:
Joy_Button_B_Up:
Joy_Button_X_Up:
Joy_Button_Y_Up:
Joy_Button_L_Up:
Joy_Button_R_Up:
Joy_Button_Back_Up:
Joy_Button_Start_Up: 

Joy_Cross_Left:
Joy_Cross_Right:
Joy_Cross_Up:
Joy_Cross_Down:
Joy_Cross_Left_Up:
Joy_Cross_Right_Up:
Joy_Cross_Up_Up:
Joy_Cross_Down_Up:
	ToolTip % A_ThisLabel
	Return
	
 
 
 
Class Joy { 
/* 
		v1.02		21:06 08.08.2020
		Автор - serzh82saratov
		E-Mail: serzh82saratov@mail.ru
		Обсуждение - http://forum.script-coding.com/viewtopic.php?pid=141541#p141541
		Связывает метки или функции с нажатиями джойстика. 
		Аналоговые стики работают как крестовины, аналоговые кнопки, как обычные кнопки но без возможности одновременного удержания. 
		Можно связать отпускания кнопок.

		Register - запускает отслеживание нажатий, в первом параметре задаётся префикс меток или функций.
		UnRegister - завершает отслеживание нажатий.
		Свойство "Correction" задаёт для аналоговых кнопок отступ от начального положения, 
		после которого статус считается нажато или отпущено. По умолчанию = 70 из 128.
		Joy.State.namekey - даёт статус нажато или отпущено.
		
		Все имена для меток, по аналогии с джойстиком X-Box. Постфикс "_Up" реакция на отпускание.

		L_Left
		L_Right
		L_Up
		L_Down
		L_Button

		R_Left
		R_Right
		R_Up
		R_Down
		R_Button

		L_Trigger
		R_Trigger

		Cross_Left
		Cross_Right
		Cross_Up
		Cross_Down 

		Button_A
		Button_B
		Button_X
		Button_Y
		Button_L
		Button_R
		Button_Back
		Button_Start 
*/
	Static WM_INPUT := 0x00FF, RIDEV_INPUTSINK := 0x00000100, Correction := 70, State := {}
	
	Register(FuncName = "")  {
		this.HIDRegister(1, 5, A_ScriptHWND, this.RIDEV_INPUTSINK)
		this.FuncName := FuncName
		If !this.Message
			this.Message := ObjBindMethod(this, "Input")
		OnMessage(this.WM_INPUT, this.Message, 8)
	}
	UnRegister()  {
		OnMessage(this.WM_INPUT, this.Message, 0)
		this.HIDRegister()
	}
	Input(wParam, lParam) {
	    Critical  
		Static oDir := {"LX":["L_Left", "L_Right"], "LY":["L_Up", "L_Down"]
					 , "RX":["R_Left", "R_Right"], "RY":["R_Up", "R_Down"]
					 , "Triggers":["R_Trigger", "L_Trigger"]} 
		Static oButtons := [["Buttons", "Button_A", 1]
						  , ["Buttons", "Button_B", 2]
						  , ["Buttons", "Button_X", 4]
						  , ["Buttons", "Button_Y", 8]
						  , ["Buttons", "Button_L", 16]
						  , ["Buttons", "Button_R", 32]
						  , ["Buttons", "Button_Back", 64]
						  , ["Buttons", "Button_Start", 128]
						  ,   ["Cross", "L_Button", 1]
						  ,   ["Cross", "R_Button", 2]] 
		Static oCross := [["Cross_Left", 28]
						, ["Cross_Right", 12]
						, ["Cross_Up", 4]
						, ["Cross_Down", 20]]
 
		this.HIDGetInputData(lParam, uData)
		
		LX := NumGet(&uData, 2, "UChar")
		LY := NumGet(&uData, 4, "UChar")
		RX := NumGet(&uData, 6, "UChar")
		RY := NumGet(&uData, 8, "UChar")
		Triggers := NumGet(&uData, 10, "UChar")
		Buttons := NumGet(&uData, 11, "UChar")
		Cross := NumGet(&uData, 12, "UChar")
		Min := 128 - this.Correction
		Max := 128 + this.Correction
		Launch := {}
		ToolTip % LX "`n" LY "`n" RX "`n" RY "`n" Triggers "`n" Buttons "`n" Cross, 222, 222, 2
		for key, name in oDir
		{ 
			If (!this.State[name[1]] && %key% < Min)
				this.State[name[1]] := 1, Launch.Push(name[1])
			Else If (this.State[name[1]] && %key% > Min)
				this.State[name[1]] := 0, Launch.Push(name[1] "_Up")
			
			If (!this.State[name[2]] && %key% > Max)
				this.State[name[2]] := 1, Launch.Push(name[2])
			Else If (this.State[name[2]] && %key% < Max)
				this.State[name[2]] := 0, Launch.Push(name[2] "_Up")
		} 
		for key, v in oButtons
		{ 
			n := v[1] 
			If (!this.State[v[2]] && %n% & v[3])
				this.State[v[2]] := 1, Launch.Push(v[2])
			Else If (this.State[v[2]]) && !(%n% & v[3])
				this.State[v[2]] := 0, Launch.Push(v[2] "_Up") 
		} 
		for key, v in oCross
		{  
			If (!this.State[v[1]] && Cross = v[2])
				this.State[v[1]] := 1, Launch.Push(v[1])
			Else If (this.State[v[1]] && Cross != v[2])
				this.State[v[1]] := 0, Launch.Push(v[1] "_Up") 
		}  
		for k, v in Launch
			Try SetTimer, % this.FuncName v, -10
	}  
	 
	; Следующие функции взяты из библиотеки AHKHID https://autohotkey.com/board/topic/38015-ahkhid-an-ahk-implementation-of-the-hid-functions/
	
	HIDGetInputData(InputHandle, ByRef uData) { 
	    ;Get raw data size                                           RID_INPUT
	    r := DllCall("GetRawInputData", "UInt", InputHandle, "UInt", 0x10000003, "Ptr", 0, "UInt*", iSize, "UInt", 8 + A_PtrSize * 2)
	    If (r = -1) Or ErrorLevel {
	        ErrorLevel = GetRawInputData call failed.`nReturn value: %r%`nErrorLevel: %ErrorLevel%`nLine: %A_LineNumber%`nLast Error: %A_LastError%
	        Return -1
	    }
	    
	    ;Prep var
	    VarSetCapacity(uRawInput, iSize)
	    
	    ;Get raw data                                                RID_INPUT
	    r := DllCall("GetRawInputData", "UInt", InputHandle, "UInt", 0x10000003, "Ptr", &uRawInput, "UInt*", iSize, "UInt", 8 + A_PtrSize * 2)
	    If (r = -1) Or ErrorLevel {
	        ErrorLevel = GetRawInputData call failed.`nReturn value: %r%`nErrorLevel: %ErrorLevel%`nLine: %A_LineNumber%`nLast Error: %A_LastError%
	        Return -1
	    } Else If (r <> iSize) {
	        ErrorLevel = GetRawInputData did not return the correct size.`nSize returned: %r%`nSize allocated: %iSize%
	        Return -1
	    }
	    
	    ;Get the size of each HID input and the number of them
	    iSize   := NumGet(uRawInput, 8 + A_PtrSize * 2 + 0, "UInt") ;ID_HID_SIZE
	    iCount  := NumGet(uRawInput, 8 + A_PtrSize * 2 + 4, "UInt") ;ID_HID_COUNT
	    
	    ;Allocate memory
	    VarSetCapacity(uData, iSize * iCount)
	    
	    ;Copy bytes
	    DllCall("RtlMoveMemory", UInt, &uData, UInt, &uRawInput + 8 + A_PtrSize * 2 + 8, UInt, iSize * iCount)
	    
	    Return (iSize * iCount)
	} 
	HIDAddRegister(UsagePage = False, Usage = False, Handle = False, Flags = 0) {
	    Static uDev, iIndex := 0, iCount := 0
	    
	    ;Check if we just want the address
	    If Not (UsagePage Or Usage Or Handle Or Flags)
	        Return &uDev
	    ;Check if we just want the count
	    Else If (UsagePage = "Count")
	        Return iCount
	    ;Check if we're dimensioning the struct
	    Else If UsagePage And Not (Usage Or Handle Or Flags) {
	        iCount := UsagePage
	        iIndex := 0
	        VarSetCapacity(uDev, iCount * (8 + A_PtrSize))
	        Return &uDev
	    }
	    
	    ;Check if there's space before adding data to struct
	    If (iIndex = iCount)
	        Return -1    ;Full capacity
	    
	    ;Check if hwnd needs to be null. RIDEV_REMOVE, RIDEV_EXCLUDE
	    Handle := ((Flags & 0x00000001) Or (Flags & 0x00000010)) ? 0 : Handle
	
	    ;Put in struct
	    NumPut(UsagePage, uDev, (iIndex * (8 + A_PtrSize)) + 0, "UShort")
	    NumPut(Usage,     uDev, (iIndex * (8 + A_PtrSize)) + 2, "UShort")
	    NumPut(Flags,     uDev, (iIndex * (8 + A_PtrSize)) + 4, "UInt")
	    NumPut(Handle,    uDev, (iIndex * (8 + A_PtrSize)) + 8, "Ptr")
	    
	    ;Move to next slot
	    iIndex += 1
	    
	    Return &uDev
	} 
	HIDRegister(UsagePage = False, Usage = False, Handle = False, Flags = 0) { 
	    ;Check if we're using the AddRegister array or only a single struct
	    If Not (UsagePage Or Usage Or Handle Or Flags) {
	        
	        ;Call
	        r := DllCall("RegisterRawInputDevices", "Ptr", this.HIDAddRegister(), "UInt", this.HIDAddRegister("Count"), "UInt", 8 + A_PtrSize)
	        
	        ;Check for error
	        If Not r {
	            ErrorLevel = RegisterRawInputDevices call failed.`nReturn value: %r%`nErrorLevel: %ErrorLevel%`nLine: %A_LineNumber%`nLast Error: %A_LastError%
	            Return -1
	        }
	        
	    ;Build struct and call
	    } Else {
	        
	        ;Prep var
	        VarSetCapacity(uDev, (8 + A_PtrSize), 0)
	        
	        ;Check if hwnd needs to be null. RIDEV_REMOVE, RIDEV_EXCLUDE
	        Handle := ((Flags & 0x00000001) Or (Flags & 0x00000010)) ? 0 : Handle
	        
	        NumPut(UsagePage, uDev, 0, "UShort")
	        NumPut(Usage,     uDev, 2, "UShort")
	        NumPut(Flags,     uDev, 4, "UInt")
	        NumPut(Handle,    uDev, 8, "Ptr")
	        
	        ;Call
	        r := DllCall("RegisterRawInputDevices", "Ptr", &uDev, "UInt", 1, "UInt", 8 + A_PtrSize)
	        
	        ;Check for error
	        If Not r Or ErrorLevel {
	            ErrorLevel = RegisterRawInputDevices call failed.`nReturn value: %r%`nErrorLevel: %ErrorLevel%`nLine: %A_LineNumber%`nLast Error: %A_LastError%
	            Return -1
	        }
	    } 
	    Return 0
	}
} 

12

Re: AHK: Работа с джойстиками через HID

Snegovik2
Так зачем вам, вы же сказали что у вас оно не реагирует.

По вопросам возмездной помощи пишите на E-Mail: serzh82saratov@mail.ru Telegram: https://t.me/sergiol982
Win10x64 AhkSpy, Hotkey, ClockGui

13

Re: AHK: Работа с джойстиками через HID

Это я сейчас пробую работу данного скрипта на другом джойстике - Logitech
И на нем как раз - есть какая-то реакция.

Но проблема в том, что непонятно как навесить клавиши на нажатия джойстика - в этом скрипте.

14

Re: AHK: Работа с джойстиками через HID

Joy_L_Left: 
	Send {Left}
	Return
По вопросам возмездной помощи пишите на E-Mail: serzh82saratov@mail.ru Telegram: https://t.me/sergiol982
Win10x64 AhkSpy, Hotkey, ClockGui

15

Re: AHK: Работа с джойстиками через HID

Это надо предварительно удалить.

Joy_L_Left: 
Joy_L_Right: 
Joy_L_Up:  
Joy_L_Down: 
Joy_L_Left_Up: 
Joy_L_Right_Up:  
Joy_L_Up_Up: 
Joy_L_Down_Up: 

Joy_R_Left: 
Joy_R_Right: 
Joy_R_Up:  
Joy_R_Down: 
Joy_R_Left_Up: 
Joy_R_Right_Up:  
Joy_R_Up_Up: 
Joy_R_Down_Up: 

Joy_L_Button:
Joy_R_Button:
Joy_L_Button_Up:
Joy_R_Button_Up:

Joy_L_Trigger:
Joy_R_Trigger:
Joy_L_Trigger_Up:
Joy_R_Trigger_Up:

Joy_Button_A:
Joy_Button_B:
Joy_Button_X:
Joy_Button_Y:
Joy_Button_L:
Joy_Button_R:
Joy_Button_Back:
Joy_Button_Start:
Joy_Button_A_Up:
Joy_Button_B_Up:
Joy_Button_X_Up:
Joy_Button_Y_Up:
Joy_Button_L_Up:
Joy_Button_R_Up:
Joy_Button_Back_Up:
Joy_Button_Start_Up: 

Joy_Cross_Left:
Joy_Cross_Right:
Joy_Cross_Up:
Joy_Cross_Down:
Joy_Cross_Left_Up:
Joy_Cross_Right_Up:
Joy_Cross_Up_Up:
Joy_Cross_Down_Up:
	ToolTip % A_ThisLabel
	Return
По вопросам возмездной помощи пишите на E-Mail: serzh82saratov@mail.ru Telegram: https://t.me/sergiol982
Win10x64 AhkSpy, Hotkey, ClockGui

16

Re: AHK: Работа с джойстиками через HID

serzh82saratov не работает.
Не реагирует на нажатие Left

17

Re: AHK: Работа с джойстиками через HID

Snegovik2 пишет:

И на нем как раз - есть какая-то реакция.

Не понимаю, какая реакция есть?

По вопросам возмездной помощи пишите на E-Mail: serzh82saratov@mail.ru Telegram: https://t.me/sergiol982
Win10x64 AhkSpy, Hotkey, ClockGui

18

Re: AHK: Работа с джойстиками через HID

serzh82saratov пишет:

Не понимаю, какая реакция есть?

Изначально появлялись две таблички - первая отображает численные значения нажатых кнопок (начимаешь на какую-то клавишу - а там какое-то число отображается).
А вторая табличка - отображала какие-то названия, например при нажатии Left на джойстике - она показывала Joy_L_Left (или что-то в этом духе).

Когда вы сказали -

serzh82saratov пишет:

Это надо предварительно удалить.

Я удалил эту часть скрипта - и вторая табличка перестала появляться.
А первая табличка все еще появляется и по-прежнему реагирует на нажатия джойстика - поэтому я и говорю, что вижу реакцию на нажатия (в этой табличке).
Но кнопка {Left} из куска кода

Joy_L_Left: 
	Send {Left}
	Return

что-то не нажимается.

19

Re: AHK: Работа с джойстиками через HID

Тогда приведите весь код который вы запускаете.

По вопросам возмездной помощи пишите на E-Mail: serzh82saratov@mail.ru Telegram: https://t.me/sergiol982
Win10x64 AhkSpy, Hotkey, ClockGui

20 (изменено: Snegovik2, 2020-08-12 22:29:26)

Re: AHK: Работа с джойстиками через HID

serzh82saratov
вот:


; http://forum.script-coding.com/viewtopic.php?pid=141541#p141541


#SingleInstance Force
#NoEnv
ListLines Off
SetBatchLines -1
 
Joy.Register("Joy_")
Return

Esc::ExitApp



Joy_L_Left: 
	Send {Left}
	Return
	
 
Class Joy { 
/* 
		v1.02		21:06 08.08.2020
		Автор - serzh82saratov
		E-Mail: serzh82saratov@mail.ru
		Обсуждение - http://forum.script-coding.com/viewtopic.php?pid=141541#p141541
		Связывает метки или функции с нажатиями джойстика. 
		Аналоговые стики работают как крестовины, аналоговые кнопки, как обычные кнопки но без возможности одновременного удержания. 
		Можно связать отпускания кнопок.

		Register - запускает отслеживание нажатий, в первом параметре задаётся префикс меток или функций.
		UnRegister - завершает отслеживание нажатий.
		Свойство "Correction" задаёт для аналоговых кнопок отступ от начального положения, 
		после которого статус считается нажато или отпущено. По умолчанию = 70 из 128.
		Joy.State.namekey - даёт статус нажато или отпущено.
		
		Все имена для меток, по аналогии с джойстиком X-Box. Постфикс "_Up" реакция на отпускание.

		L_Left
		L_Right
		L_Up
		L_Down
		L_Button

		R_Left
		R_Right
		R_Up
		R_Down
		R_Button

		L_Trigger
		R_Trigger

		Cross_Left
		Cross_Right
		Cross_Up
		Cross_Down 

		Button_A
		Button_B
		Button_X
		Button_Y
		Button_L
		Button_R
		Button_Back
		Button_Start 
*/
	Static WM_INPUT := 0x00FF, RIDEV_INPUTSINK := 0x00000100, Correction := 70, State := {}
	
	Register(FuncName = "")  {
		this.HIDRegister(1, 5, A_ScriptHWND, this.RIDEV_INPUTSINK)
		this.FuncName := FuncName
		If !this.Message
			this.Message := ObjBindMethod(this, "Input")
		OnMessage(this.WM_INPUT, this.Message, 8)
	}
	UnRegister()  {
		OnMessage(this.WM_INPUT, this.Message, 0)
		this.HIDRegister()
	}
	Input(wParam, lParam) {
	    Critical  
		Static oDir := {"LX":["L_Left", "L_Right"], "LY":["L_Up", "L_Down"]
					 , "RX":["R_Left", "R_Right"], "RY":["R_Up", "R_Down"]
					 , "Triggers":["R_Trigger", "L_Trigger"]} 
		Static oButtons := [["Buttons", "Button_A", 1]
						  , ["Buttons", "Button_B", 2]
						  , ["Buttons", "Button_X", 4]
						  , ["Buttons", "Button_Y", 8]
						  , ["Buttons", "Button_L", 16]
						  , ["Buttons", "Button_R", 32]
						  , ["Buttons", "Button_Back", 64]
						  , ["Buttons", "Button_Start", 128]
						  ,   ["Cross", "L_Button", 1]
						  ,   ["Cross", "R_Button", 2]] 
		Static oCross := [["Cross_Left", 28]
						, ["Cross_Right", 12]
						, ["Cross_Up", 4]
						, ["Cross_Down", 20]]
 
		this.HIDGetInputData(lParam, uData)
		
		LX := NumGet(&uData, 2, "UChar")
		LY := NumGet(&uData, 4, "UChar")
		RX := NumGet(&uData, 6, "UChar")
		RY := NumGet(&uData, 8, "UChar")
		Triggers := NumGet(&uData, 10, "UChar")
		Buttons := NumGet(&uData, 11, "UChar")
		Cross := NumGet(&uData, 12, "UChar")
		Min := 128 - this.Correction
		Max := 128 + this.Correction
		Launch := {}
		ToolTip % LX "`n" LY "`n" RX "`n" RY "`n" Triggers "`n" Buttons "`n" Cross, 222, 222, 2
		for key, name in oDir
		{ 
			If (!this.State[name[1]] && %key% < Min)
				this.State[name[1]] := 1, Launch.Push(name[1])
			Else If (this.State[name[1]] && %key% > Min)
				this.State[name[1]] := 0, Launch.Push(name[1] "_Up")
			
			If (!this.State[name[2]] && %key% > Max)
				this.State[name[2]] := 1, Launch.Push(name[2])
			Else If (this.State[name[2]] && %key% < Max)
				this.State[name[2]] := 0, Launch.Push(name[2] "_Up")
		} 
		for key, v in oButtons
		{ 
			n := v[1] 
			If (!this.State[v[2]] && %n% & v[3])
				this.State[v[2]] := 1, Launch.Push(v[2])
			Else If (this.State[v[2]]) && !(%n% & v[3])
				this.State[v[2]] := 0, Launch.Push(v[2] "_Up") 
		} 
		for key, v in oCross
		{  
			If (!this.State[v[1]] && Cross = v[2])
				this.State[v[1]] := 1, Launch.Push(v[1])
			Else If (this.State[v[1]] && Cross != v[2])
				this.State[v[1]] := 0, Launch.Push(v[1] "_Up") 
		}  
		for k, v in Launch
			Try SetTimer, % this.FuncName v, -10
	}  
	 
	; Следующие функции взяты из библиотеки AHKHID https://autohotkey.com/board/topic/38015-ahkhid-an-ahk-implementation-of-the-hid-functions/
	
	HIDGetInputData(InputHandle, ByRef uData) { 
	    ;Get raw data size                                           RID_INPUT
	    r := DllCall("GetRawInputData", "UInt", InputHandle, "UInt", 0x10000003, "Ptr", 0, "UInt*", iSize, "UInt", 8 + A_PtrSize * 2)
	    If (r = -1) Or ErrorLevel {
	        ErrorLevel = GetRawInputData call failed.`nReturn value: %r%`nErrorLevel: %ErrorLevel%`nLine: %A_LineNumber%`nLast Error: %A_LastError%
	        Return -1
	    }
	    
	    ;Prep var
	    VarSetCapacity(uRawInput, iSize)
	    
	    ;Get raw data                                                RID_INPUT
	    r := DllCall("GetRawInputData", "UInt", InputHandle, "UInt", 0x10000003, "Ptr", &uRawInput, "UInt*", iSize, "UInt", 8 + A_PtrSize * 2)
	    If (r = -1) Or ErrorLevel {
	        ErrorLevel = GetRawInputData call failed.`nReturn value: %r%`nErrorLevel: %ErrorLevel%`nLine: %A_LineNumber%`nLast Error: %A_LastError%
	        Return -1
	    } Else If (r <> iSize) {
	        ErrorLevel = GetRawInputData did not return the correct size.`nSize returned: %r%`nSize allocated: %iSize%
	        Return -1
	    }
	    
	    ;Get the size of each HID input and the number of them
	    iSize   := NumGet(uRawInput, 8 + A_PtrSize * 2 + 0, "UInt") ;ID_HID_SIZE
	    iCount  := NumGet(uRawInput, 8 + A_PtrSize * 2 + 4, "UInt") ;ID_HID_COUNT
	    
	    ;Allocate memory
	    VarSetCapacity(uData, iSize * iCount)
	    
	    ;Copy bytes
	    DllCall("RtlMoveMemory", UInt, &uData, UInt, &uRawInput + 8 + A_PtrSize * 2 + 8, UInt, iSize * iCount)
	    
	    Return (iSize * iCount)
	} 
	HIDAddRegister(UsagePage = False, Usage = False, Handle = False, Flags = 0) {
	    Static uDev, iIndex := 0, iCount := 0
	    
	    ;Check if we just want the address
	    If Not (UsagePage Or Usage Or Handle Or Flags)
	        Return &uDev
	    ;Check if we just want the count
	    Else If (UsagePage = "Count")
	        Return iCount
	    ;Check if we're dimensioning the struct
	    Else If UsagePage And Not (Usage Or Handle Or Flags) {
	        iCount := UsagePage
	        iIndex := 0
	        VarSetCapacity(uDev, iCount * (8 + A_PtrSize))
	        Return &uDev
	    }
	    
	    ;Check if there's space before adding data to struct
	    If (iIndex = iCount)
	        Return -1    ;Full capacity
	    
	    ;Check if hwnd needs to be null. RIDEV_REMOVE, RIDEV_EXCLUDE
	    Handle := ((Flags & 0x00000001) Or (Flags & 0x00000010)) ? 0 : Handle
	
	    ;Put in struct
	    NumPut(UsagePage, uDev, (iIndex * (8 + A_PtrSize)) + 0, "UShort")
	    NumPut(Usage,     uDev, (iIndex * (8 + A_PtrSize)) + 2, "UShort")
	    NumPut(Flags,     uDev, (iIndex * (8 + A_PtrSize)) + 4, "UInt")
	    NumPut(Handle,    uDev, (iIndex * (8 + A_PtrSize)) + 8, "Ptr")
	    
	    ;Move to next slot
	    iIndex += 1
	    
	    Return &uDev
	} 
	HIDRegister(UsagePage = False, Usage = False, Handle = False, Flags = 0) { 
	    ;Check if we're using the AddRegister array or only a single struct
	    If Not (UsagePage Or Usage Or Handle Or Flags) {
	        
	        ;Call
	        r := DllCall("RegisterRawInputDevices", "Ptr", this.HIDAddRegister(), "UInt", this.HIDAddRegister("Count"), "UInt", 8 + A_PtrSize)
	        
	        ;Check for error
	        If Not r {
	            ErrorLevel = RegisterRawInputDevices call failed.`nReturn value: %r%`nErrorLevel: %ErrorLevel%`nLine: %A_LineNumber%`nLast Error: %A_LastError%
	            Return -1
	        }
	        
	    ;Build struct and call
	    } Else {
	        
	        ;Prep var
	        VarSetCapacity(uDev, (8 + A_PtrSize), 0)
	        
	        ;Check if hwnd needs to be null. RIDEV_REMOVE, RIDEV_EXCLUDE
	        Handle := ((Flags & 0x00000001) Or (Flags & 0x00000010)) ? 0 : Handle
	        
	        NumPut(UsagePage, uDev, 0, "UShort")
	        NumPut(Usage,     uDev, 2, "UShort")
	        NumPut(Flags,     uDev, 4, "UInt")
	        NumPut(Handle,    uDev, 8, "Ptr")
	        
	        ;Call
	        r := DllCall("RegisterRawInputDevices", "Ptr", &uDev, "UInt", 1, "UInt", 8 + A_PtrSize)
	        
	        ;Check for error
	        If Not r Or ErrorLevel {
	            ErrorLevel = RegisterRawInputDevices call failed.`nReturn value: %r%`nErrorLevel: %ErrorLevel%`nLine: %A_LineNumber%`nLast Error: %A_LastError%
	            Return -1
	        }
	    } 
	    Return 0
	}
} 

21

Re: AHK: Работа с джойстиками через HID

Может вы просто не видите нажатие в каком то приложении, в обычном блокноте попробуйте.
Замените send на msgbox для проверки вызова метки.

По вопросам возмездной помощи пишите на E-Mail: serzh82saratov@mail.ru Telegram: https://t.me/sergiol982
Win10x64 AhkSpy, Hotkey, ClockGui

22

Re: AHK: Работа с джойстиками через HID

Лучше так, будет отправлено нажатие Left и будет видно что метка была запущена.

#SingleInstance Force
#NoEnv
ListLines Off
SetBatchLines -1
 
Joy.Register("Joy_")
Return

Esc::ExitApp

Joy_L_Left: 
	Send {Left}
	ToolTip % A_ThisLabel "`n" A_TickCount
	Return 
 
 
Class Joy { 
/* 
		v1.02		21:06 08.08.2020
		Автор - serzh82saratov
		E-Mail: serzh82saratov@mail.ru
		Обсуждение - http://forum.script-coding.com/viewtopic.php?pid=141541#p141541
		Связывает метки или функции с нажатиями джойстика. 
		Аналоговые стики работают как крестовины, аналоговые кнопки, как обычные кнопки но без возможности одновременного удержания. 
		Можно связать отпускания кнопок.

		Register - запускает отслеживание нажатий, в первом параметре задаётся префикс меток или функций.
		UnRegister - завершает отслеживание нажатий.
		Свойство "Correction" задаёт для аналоговых кнопок отступ от начального положения, 
		после которого статус считается нажато или отпущено. По умолчанию = 70 из 128.
		Joy.State.namekey - даёт статус нажато или отпущено.
		
		Все имена для меток, по аналогии с джойстиком X-Box. Постфикс "_Up" реакция на отпускание.

		L_Left
		L_Right
		L_Up
		L_Down
		L_Button

		R_Left
		R_Right
		R_Up
		R_Down
		R_Button

		L_Trigger
		R_Trigger

		Cross_Left
		Cross_Right
		Cross_Up
		Cross_Down 

		Button_A
		Button_B
		Button_X
		Button_Y
		Button_L
		Button_R
		Button_Back
		Button_Start 
*/
	Static WM_INPUT := 0x00FF, RIDEV_INPUTSINK := 0x00000100, Correction := 70, State := {}
	
	Register(FuncName = "")  {
		this.HIDRegister(1, 5, A_ScriptHWND, this.RIDEV_INPUTSINK)
		this.FuncName := FuncName
		If !this.Message
			this.Message := ObjBindMethod(this, "Input")
		OnMessage(this.WM_INPUT, this.Message, 8)
	}
	UnRegister()  {
		OnMessage(this.WM_INPUT, this.Message, 0)
		this.HIDRegister()
	}
	Input(wParam, lParam) {
	    Critical  
		Static oDir := {"LX":["L_Left", "L_Right"], "LY":["L_Up", "L_Down"]
					 , "RX":["R_Left", "R_Right"], "RY":["R_Up", "R_Down"]
					 , "Triggers":["R_Trigger", "L_Trigger"]} 
		Static oButtons := [["Buttons", "Button_A", 1]
						  , ["Buttons", "Button_B", 2]
						  , ["Buttons", "Button_X", 4]
						  , ["Buttons", "Button_Y", 8]
						  , ["Buttons", "Button_L", 16]
						  , ["Buttons", "Button_R", 32]
						  , ["Buttons", "Button_Back", 64]
						  , ["Buttons", "Button_Start", 128]
						  ,   ["Cross", "L_Button", 1]
						  ,   ["Cross", "R_Button", 2]] 
		Static oCross := [["Cross_Left", 28]
						, ["Cross_Right", 12]
						, ["Cross_Up", 4]
						, ["Cross_Down", 20]]
 
		this.HIDGetInputData(lParam, uData)
		
		LX := NumGet(&uData, 2, "UChar")
		LY := NumGet(&uData, 4, "UChar")
		RX := NumGet(&uData, 6, "UChar")
		RY := NumGet(&uData, 8, "UChar")
		Triggers := NumGet(&uData, 10, "UChar")
		Buttons := NumGet(&uData, 11, "UChar")
		Cross := NumGet(&uData, 12, "UChar")
		Min := 128 - this.Correction
		Max := 128 + this.Correction
		Launch := {}
		; ToolTip % LX "`n" LY "`n" RX "`n" RY "`n" Triggers "`n" Buttons "`n" Cross, 222, 222, 2
		for key, name in oDir
		{ 
			If (!this.State[name[1]] && %key% < Min)
				this.State[name[1]] := 1, Launch.Push(name[1])
			Else If (this.State[name[1]] && %key% > Min)
				this.State[name[1]] := 0, Launch.Push(name[1] "_Up")
			
			If (!this.State[name[2]] && %key% > Max)
				this.State[name[2]] := 1, Launch.Push(name[2])
			Else If (this.State[name[2]] && %key% < Max)
				this.State[name[2]] := 0, Launch.Push(name[2] "_Up")
		} 
		for key, v in oButtons
		{ 
			n := v[1] 
			If (!this.State[v[2]] && %n% & v[3])
				this.State[v[2]] := 1, Launch.Push(v[2])
			Else If (this.State[v[2]]) && !(%n% & v[3])
				this.State[v[2]] := 0, Launch.Push(v[2] "_Up") 
		} 
		for key, v in oCross
		{  
			If (!this.State[v[1]] && Cross = v[2])
				this.State[v[1]] := 1, Launch.Push(v[1])
			Else If (this.State[v[1]] && Cross != v[2])
				this.State[v[1]] := 0, Launch.Push(v[1] "_Up") 
		}  
		for k, v in Launch
			Try SetTimer, % this.FuncName v, -10
	}  
	 
	; Следующие функции взяты из библиотеки AHKHID https://autohotkey.com/board/topic/38015-ahkhid-an-ahk-implementation-of-the-hid-functions/
	
	HIDGetInputData(InputHandle, ByRef uData) { 
	    ;Get raw data size                                           RID_INPUT
	    r := DllCall("GetRawInputData", "UInt", InputHandle, "UInt", 0x10000003, "Ptr", 0, "UInt*", iSize, "UInt", 8 + A_PtrSize * 2)
	    If (r = -1) Or ErrorLevel {
	        ErrorLevel = GetRawInputData call failed.`nReturn value: %r%`nErrorLevel: %ErrorLevel%`nLine: %A_LineNumber%`nLast Error: %A_LastError%
	        Return -1
	    }
	    
	    ;Prep var
	    VarSetCapacity(uRawInput, iSize)
	    
	    ;Get raw data                                                RID_INPUT
	    r := DllCall("GetRawInputData", "UInt", InputHandle, "UInt", 0x10000003, "Ptr", &uRawInput, "UInt*", iSize, "UInt", 8 + A_PtrSize * 2)
	    If (r = -1) Or ErrorLevel {
	        ErrorLevel = GetRawInputData call failed.`nReturn value: %r%`nErrorLevel: %ErrorLevel%`nLine: %A_LineNumber%`nLast Error: %A_LastError%
	        Return -1
	    } Else If (r <> iSize) {
	        ErrorLevel = GetRawInputData did not return the correct size.`nSize returned: %r%`nSize allocated: %iSize%
	        Return -1
	    }
	    
	    ;Get the size of each HID input and the number of them
	    iSize   := NumGet(uRawInput, 8 + A_PtrSize * 2 + 0, "UInt") ;ID_HID_SIZE
	    iCount  := NumGet(uRawInput, 8 + A_PtrSize * 2 + 4, "UInt") ;ID_HID_COUNT
	    
	    ;Allocate memory
	    VarSetCapacity(uData, iSize * iCount)
	    
	    ;Copy bytes
	    DllCall("RtlMoveMemory", UInt, &uData, UInt, &uRawInput + 8 + A_PtrSize * 2 + 8, UInt, iSize * iCount)
	    
	    Return (iSize * iCount)
	} 
	HIDAddRegister(UsagePage = False, Usage = False, Handle = False, Flags = 0) {
	    Static uDev, iIndex := 0, iCount := 0
	    
	    ;Check if we just want the address
	    If Not (UsagePage Or Usage Or Handle Or Flags)
	        Return &uDev
	    ;Check if we just want the count
	    Else If (UsagePage = "Count")
	        Return iCount
	    ;Check if we're dimensioning the struct
	    Else If UsagePage And Not (Usage Or Handle Or Flags) {
	        iCount := UsagePage
	        iIndex := 0
	        VarSetCapacity(uDev, iCount * (8 + A_PtrSize))
	        Return &uDev
	    }
	    
	    ;Check if there's space before adding data to struct
	    If (iIndex = iCount)
	        Return -1    ;Full capacity
	    
	    ;Check if hwnd needs to be null. RIDEV_REMOVE, RIDEV_EXCLUDE
	    Handle := ((Flags & 0x00000001) Or (Flags & 0x00000010)) ? 0 : Handle
	
	    ;Put in struct
	    NumPut(UsagePage, uDev, (iIndex * (8 + A_PtrSize)) + 0, "UShort")
	    NumPut(Usage,     uDev, (iIndex * (8 + A_PtrSize)) + 2, "UShort")
	    NumPut(Flags,     uDev, (iIndex * (8 + A_PtrSize)) + 4, "UInt")
	    NumPut(Handle,    uDev, (iIndex * (8 + A_PtrSize)) + 8, "Ptr")
	    
	    ;Move to next slot
	    iIndex += 1
	    
	    Return &uDev
	} 
	HIDRegister(UsagePage = False, Usage = False, Handle = False, Flags = 0) { 
	    ;Check if we're using the AddRegister array or only a single struct
	    If Not (UsagePage Or Usage Or Handle Or Flags) {
	        
	        ;Call
	        r := DllCall("RegisterRawInputDevices", "Ptr", this.HIDAddRegister(), "UInt", this.HIDAddRegister("Count"), "UInt", 8 + A_PtrSize)
	        
	        ;Check for error
	        If Not r {
	            ErrorLevel = RegisterRawInputDevices call failed.`nReturn value: %r%`nErrorLevel: %ErrorLevel%`nLine: %A_LineNumber%`nLast Error: %A_LastError%
	            Return -1
	        }
	        
	    ;Build struct and call
	    } Else {
	        
	        ;Prep var
	        VarSetCapacity(uDev, (8 + A_PtrSize), 0)
	        
	        ;Check if hwnd needs to be null. RIDEV_REMOVE, RIDEV_EXCLUDE
	        Handle := ((Flags & 0x00000001) Or (Flags & 0x00000010)) ? 0 : Handle
	        
	        NumPut(UsagePage, uDev, 0, "UShort")
	        NumPut(Usage,     uDev, 2, "UShort")
	        NumPut(Flags,     uDev, 4, "UInt")
	        NumPut(Handle,    uDev, 8, "Ptr")
	        
	        ;Call
	        r := DllCall("RegisterRawInputDevices", "Ptr", &uDev, "UInt", 1, "UInt", 8 + A_PtrSize)
	        
	        ;Check for error
	        If Not r Or ErrorLevel {
	            ErrorLevel = RegisterRawInputDevices call failed.`nReturn value: %r%`nErrorLevel: %ErrorLevel%`nLine: %A_LineNumber%`nLast Error: %A_LastError%
	            Return -1
	        }
	    } 
	    Return 0
	}
} 
По вопросам возмездной помощи пишите на E-Mail: serzh82saratov@mail.ru Telegram: https://t.me/sergiol982
Win10x64 AhkSpy, Hotkey, ClockGui

23

Re: AHK: Работа с джойстиками через HID

Не работает.
На нажатия не реагирует вообще никак.

24

Re: AHK: Работа с джойстиками через HID

Snegovik2 пишет:

Изначально появлялись две таблички - первая отображает численные значения нажатых кнопок (начимаешь на какую-то клавишу - а там какое-то число отображается).
А вторая табличка - отображала какие-то названия, например при нажатии Left на джойстике - она показывала Joy_L_Left (или что-то в этом духе).

У вас все признаки того, что всё работает как надо.
Этот код создан как раз для самых начинающих, проще не куда. Пример из справки сложнее, и в общем хуже.
Думаю вам надо подождать несколько лет, и после снова попробовать, некоторым это помогает. Я не знаю что вам ещё тут посоветовать.

По вопросам возмездной помощи пишите на E-Mail: serzh82saratov@mail.ru Telegram: https://t.me/sergiol982
Win10x64 AhkSpy, Hotkey, ClockGui

25

Re: AHK: Работа с джойстиками через HID

serzh82saratov, я говорю - что запускаю последний скрипт (из сообщения 22) - и он не работает.
Никаких табличек в нем не появляется.

26

Re: AHK: Работа с джойстиками через HID

А я вам говорю что исходя из той цитаты что я привёл, это весьма сомнительно.

По вопросам возмездной помощи пишите на E-Mail: serzh82saratov@mail.ru Telegram: https://t.me/sergiol982
Win10x64 AhkSpy, Hotkey, ClockGui

27

Re: AHK: Работа с джойстиками через HID

Так это я про предыдущий код говорил - из сообщения 11
Там-то как раз реакция в табличках была.

28 (изменено: serzh82saratov, 2020-08-13 23:06:45)

Re: AHK: Работа с джойстиками через HID

Всё верно, я тот пост и цитировал (то есть тот в котором говорится о предыдущем коде), исходя из этого код из 22 поста у вас тоже работает, но вы зачем то пишите, что он не работает.

По вопросам возмездной помощи пишите на E-Mail: serzh82saratov@mail.ru Telegram: https://t.me/sergiol982
Win10x64 AhkSpy, Hotkey, ClockGui

29

Re: AHK: Работа с джойстиками через HID

Еще раз создаю ahk-скрипт , добавляю туда код из 22 сообщения.

Джойстик подключен.
Нажимаю на джойстике клавишу "Left" - не происходит ничего.
Скрипт по-прежнему висит в трее, никаких ошибок не выдает.
Также не появляется никаких желтых табличек, которые появлялись ранее - в скрипте из 11 сообщения.

30

Re: AHK: Работа с джойстиками через HID

Snegovik2 пишет:

Нажимаю на джойстике клавишу "Left" - не происходит ничего.

Что это за клавиша?

По вопросам возмездной помощи пишите на E-Mail: serzh82saratov@mail.ru Telegram: https://t.me/sergiol982
Win10x64 AhkSpy, Hotkey, ClockGui

31

Re: AHK: Работа с джойстиками через HID

Клавиша - на крестовине джойстика (обозначает направление "влево")

32

Re: AHK: Работа с джойстиками через HID

Почему на крестовине? Речи про неё не было.

По вопросам возмездной помощи пишите на E-Mail: serzh82saratov@mail.ru Telegram: https://t.me/sergiol982
Win10x64 AhkSpy, Hotkey, ClockGui

33

Re: AHK: Работа с джойстиками через HID

У вас же написано "Отслеживание нажатий джойстиков" - разве нажатие на крестовину - не является нажатием джойстика.
Да и из моего вопрос это вроде понятно - Left - это направление, а направление в джойстике - задается на крестовине.

34

Re: AHK: Работа с джойстиками через HID

serzh82saratov пишет:

например при нажатии Left на джойстике

- вот же я писал - нажатии Left на джойстике. На джойстике - Left - это крестовина. Там нет других Left.

35

Re: AHK: Работа с джойстиками через HID

Joy_L_Left - это левый стик. Это должно быть понятно из кода 11 поста.

По вопросам возмездной помощи пишите на E-Mail: serzh82saratov@mail.ru Telegram: https://t.me/sergiol982
Win10x64 AhkSpy, Hotkey, ClockGui

36

Re: AHK: Работа с джойстиками через HID

Да. Вот сейчас протестировал - левый стик действительно работает.

А как заменить Joy_L_Left, на такую команду, чтобы код реагировал на кнопку крестовины ?

37

Re: AHK: Работа с джойстиками через HID

Joy_C_Left:

То есть всё это время вы даже не удосужили себя тем, чтобы другие кнопки "потыкать".

По вопросам возмездной помощи пишите на E-Mail: serzh82saratov@mail.ru Telegram: https://t.me/sergiol982
Win10x64 AhkSpy, Hotkey, ClockGui

38

Re: AHK: Работа с джойстиками через HID

serzh82saratov пишет:

Joy_C_Left

Это имя для новой версии, он во втором посте.

По вопросам возмездной помощи пишите на E-Mail: serzh82saratov@mail.ru Telegram: https://t.me/sergiol982
Win10x64 AhkSpy, Hotkey, ClockGui

39

Re: AHK: Работа с джойстиками через HID

Вроде теперь все ясно.
Спасибо за ответ.