A4Tech x7 Mouse - remap DPI Button! (models A4Tech F4, A4Tech X-710BK)

Post your working scripts, libraries and tools
VcSaJen
Posts: 4
Joined: 14 Jun 2018, 05:44

A4Tech x7 Mouse - remap DPI Button! (models A4Tech F4, A4Tech X-710BK)

Post by VcSaJen » 14 Jun 2018, 07:12

You can't rebind DPI Button in Oscar Mouse Editor. I searched far and wide for complete AHK solution, but I got only topic where people ask the same question, without answers. Well, no more! Solution is here!

Tested and works with A4Tech F4, A4Tech X-710BK (old and new versions with rectangular sensor hole and circular hole). For any other A4Tech x7 mouse (or other version of the same model), you need to modify the code.

YOU STILL NEED OSCAR! You need to disable all DPI modes except the first one, and change first mode's DPI to match your preference. That way, clicking DPI button won't change your mouse's DPI.
You can also disable default triple click button action in Oscar, if that's what you want.

Code: Select all

; AHKv1 Script
#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
;Must be in auto-execute section if I want to use the constants
#Include %A_ScriptDir%\AHKHID.ahk

;Create GUI to receive messages
Gui, +LastFound
hGui := WinExist()

;Intercept WM_INPUT messages
WM_INPUT := 0xFF
OnMessage(WM_INPUT, "InputMsg")

;Register Remote Control with RIDEV_INPUTSINK (so that data is received even in the background)
r := AHKHID_Register(65440, 165, hGui, RIDEV_INPUTSINK)

btnDPIShiftState = false ; 6th button, below scroll wheel
btnTripleClickState = false ; 7th button, between LMB and scroll wheel
DPIMode = 0 ; 0-5: black, green, yellow, red, yellow-red, green-yellow respectively. Some models have only 0-4 modes.

/*
;Prefix loop
Loop {
    Sleep 1000
    If WinActive("ahk_class QWidget") Or WinActive("ahk_class VLC DirectX")
    sPrefix := "VLC"
    Else If WinActive("ahk_class Winamp v1.x") Or WinActive("ahk_class Winamp Video")
    sPrefix := "Winamp"
    Else If WinActive("ahk_class MediaPlayerClassicW")
    sPrefix := "MPC"
    Else sPrefix := "Default"
}
*/
Return

InputMsg(wParam, lParam) {
    Local devh, iKey, sLabel

    Critical

    ;Get handle of device
    devh := AHKHID_GetInputInfo(lParam, II_DEVHANDLE)

    ;Check for error
    If (devh <> -1) ;Check that it is compatible a4tech x7 mouse
    And (AHKHID_GetDevInfo(devh, DI_DEVTYPE, True) = RIM_TYPEHID) {
        VendorID := AHKHID_GetDevInfo(devh, DI_HID_VENDORID, True)
        ProductID := AHKHID_GetDevInfo(devh, DI_HID_PRODUCTID, True)
        VersionNumber := AHKHID_GetDevInfo(devh, DI_HID_VERSIONNUMBER, True)
        isCompatible := false
        
        ; Model a4tech X-710BK (5 DPI modes version, rectangular sensor hole (VersionNumber=258) and 6 DPI modes version, round sensor hole(VersionNumber=259))
        If (VendorID = 2522)
        And (ProductID = 37008)
        And (VersionNumber = 259 or VersionNumber = 258) {
            isCompatible := true
            btnDataByte := 3 ; 4th byte
            DPIModeDataByte := 7 ; 8th byte
            flagBtnDPIShift := 0x20
            flagBtnTripleClick := 0x40
        }
        
        ; Model a4tech F4
        If (VendorID = 2522)
        And (ProductID = 36966)
        And (VersionNumber = 259) {
            isCompatible := true
            btnDataByte := 3 ; 4th byte
            DPIModeDataByte := 7 ; 8th byte
            flagBtnDPIShift := 0x80
            flagBtnTripleClick := 0x40
        }
        
        ;insert here block for your own mouse model
        
        if (isCompatible) {
            ;Get data
            iKey := AHKHID_GetInputData(lParam, uData)

            ;Check for error
            If (iKey <> -1) {
                ;Get key map (located at the 4th byte)
                iKey := NumGet(uData, btnDataByte, "UChar")
                
                btnCurDPIShiftState := (iKey & flagBtnDPIShift) != 0
                btnCurTripleClickState := (iKey & flagBtnTripleClick) != 0
                
                ;Get DPI mode (located at the 8th byte)
                curDPIMode := NumGet(uData, DPIModeDataByte, "UChar")
                curDPIMode &= 0xF
                DPIMode := curDPIMode
                
                if (btnDPIShiftState <> btnCurDPIShiftState) {
                    btnDPIShiftState := btnCurDPIShiftState
                    Gosub, ON_BTN_DPISHIFT
                }
                if (btnTripleClickState <> btnCurTripleClickState) {
                    btnTripleClickState := btnCurTripleClickState
                    Gosub, ON_BTN_TRIPLECLICK
                }
            }
        }
    }
}

ON_BTN_DPISHIFT: ;up/down btnDPIShift event
    If (btnDPIShiftState) {
        Send {Home Down} ;replace with your own mapping
    } else {
        Send {Home Up} ;replace with your own mapping
    }
Return

ON_BTN_TRIPLECLICK: ;up/down btnTripleClick event
    If (btnTripleClickState) { ; you can also remap triple-click key, if you want
        
    } else {
        
    }
Return
How to install:
1) Go here, right click on "Download", and select "Save object as...". Save AHKHID.ahk to preferred folder.
2) Save code from above codebox in same folder, file extension must be ".ahk".
3) Double click on saved file (from step 2)

How to remap:
By default it remaps DPI button to Home key. You can change it in ON_BTN_DPISHIFT part of code.

How to add your own a4tech x7 mouse model:
1) Download Example 1 and Example 2 from this topic and save them to same folder you saved AHKHID.ahk
2) Open both files with your editor and add #include AHKHID.ahk as a first line. Save both files.
3) Execute Example1.ahk and Example2.ahk by doubleclicking them.
4) Refer to this screenshot to figure out needed variables.
5) Copypaste Model a4tech F4 block after ;insert here block for your own mouse model, then change variables to the ones you figured out at step 4). You need to change VendorID, ProductID, VersionNumber, btnDataByte, DPIModeDataByte, flagBtnDPIShift.
6) Done

Feel free to ask here if you have any difficulties.

Return to “Scripts and Functions”