WM_INPUT(0xFF) - Problem with delays!

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User
Posts: 407
Joined: 26 Jun 2017, 08:12

WM_INPUT(0xFF) - Problem with delays!

07 Dec 2017, 23:53

I use the function below to detect input devices! The problem is that, 1 or even 0 millisecond delay is enough to make "InputHandler()" function don't return the input device info! Is there any fix for this issue?
Detect Input Devices.gif
Detect Input Devices.gif (69.56 KiB) Viewed 995 times

Code: Select all

RegisterRawInputDevices(1, 2)	;Monitor Mouse devices
RegisterRawInputDevices(1, 6)	;Monitor Keyboard devices


SleepTime := ""     ;1 or even 0 millisecond delay is enough to make "InputHandler()" don't return the input device info!

OnMessage(0xFF, "InputHandler")

gui, add, edit, w600 h200 vEditControl,
gui, show
return


guiclose:	;____________ gui close ____________
ExitApp


InputHandler(wParam, lParam) {

    Global SleepTime

    static RID_INPUT        := 0x10000003
    static RIDI_DEVICENAME  := 0x20000007

   if (SleepTime != "")
   sleep, % SleepTime

    Critical

    DllCall("GetRawInputData"
        , "Ptr",    lParam
        , "UInt",   RID_INPUT
        , "Ptr",    0
        , "UIntP",  size
        , "UInt",   8 + A_PtrSize * 2)
    VarSetCapacity(buffer, size)
    DllCall("GetRawInputData"
        , "Ptr",    lParam
        , "UInt",   RID_INPUT
        , "Ptr",    &buffer
        , "UIntP",  size
        , "UInt",   8 + A_PtrSize * 2)

    devHandle := NumGet(buffer, 8)
    vk := NumGet(buffer, 8 + 2 * A_PtrSize + 6, "UShort")

    DllCall("GetRawInputDeviceInfo"
        , "Ptr",    devHandle
        , "UInt",   RIDI_DEVICENAME
        , "Ptr",    0
        , "UIntP",  size)
    VarSetCapacity(info, size)
    DllCall("GetRawInputDeviceInfo"
        , "Ptr",    devHandle
        , "UInt",   RIDI_DEVICENAME
        , "Ptr",    &info
        , "UIntP",  size)

    guicontrol, , EditControl, % "tick:`t" A_TickCount
        . "`nname:`t"   StrGet(&info)
        . "`nvk:`t"     vk
}

RegisterRawInputDevices(usagePage, usage) {
    static RIDEV_INPUTSINK := 0x00000100
    VarSetCapacity(rawDevice, 8 + A_PtrSize)
    NumPut(usagePage,       rawDevice, 0, "UShort")
    NumPut(usage,           rawDevice, 2, "UShort")
    NumPut(RIDEV_INPUTSINK, rawDevice, 4, "UInt")
    NumPut(A_ScriptHWND,    rawDevice, 8, "UPtr")

    if !DllCall("RegisterRawInputDevices"
        , "Ptr",  &rawDevice
        , "UInt", 1
        , "UInt", 8 + A_PtrSize)
    {
        throw "Fail"
    }
}
User
Posts: 407
Joined: 26 Jun 2017, 08:12

Re: WM_INPUT(0xFF) - Problem with delays!

08 Dec 2017, 11:28

I rewrote the above script in order to illustrate better the problem!

Code: Select all

RegisterRawInputDevices(1, 2)	;Monitor Mouse devices
RegisterRawInputDevices(1, 6)	;Monitor Keyboard devices


OnMessage(0xFF, "WM_Input")
WM_Input(WParam, LParam)
{
AnotherFunction()
;The above function has its own tasks to do, and it must be executed first!
;The function above contains "sleep, 1", which causes the problem bellow,
;1 or even 0 millisecond delay is enough to make "InputHandler()" below to don't return the input device info!

InputHandler(WParam, LParam)
}


gui, add, edit, w600 h200 vEditControl,
gui, show
return


guiclose:	;____________ gui close ____________
ExitApp


AnotherFunction()	;_______________ Another Function ________________
{
;This is another function that has its own tasks to do

sleep, 1	;1 or even 0 millisecond delay is enough to make "InputHandler()" don't return the input device info!
}

InputHandler(wParam, lParam)	;________________________ InputHandler(Function) ___________________
{
    static RID_INPUT        := 0x10000003
    static RIDI_DEVICENAME  := 0x20000007

    Critical

    DllCall("GetRawInputData"
        , "Ptr",    lParam
        , "UInt",   RID_INPUT
        , "Ptr",    0
        , "UIntP",  size
        , "UInt",   8 + A_PtrSize * 2)
    VarSetCapacity(buffer, size)
    DllCall("GetRawInputData"
        , "Ptr",    lParam
        , "UInt",   RID_INPUT
        , "Ptr",    &buffer
        , "UIntP",  size
        , "UInt",   8 + A_PtrSize * 2)

    devHandle := NumGet(buffer, 8)
    vk := NumGet(buffer, 8 + 2 * A_PtrSize + 6, "UShort")

    DllCall("GetRawInputDeviceInfo"
        , "Ptr",    devHandle
        , "UInt",   RIDI_DEVICENAME
        , "Ptr",    0
        , "UIntP",  size)
    VarSetCapacity(info, size)
    DllCall("GetRawInputDeviceInfo"
        , "Ptr",    devHandle
        , "UInt",   RIDI_DEVICENAME
        , "Ptr",    &info
        , "UIntP",  size)

    guicontrol, , EditControl, % "tick:`t" A_TickCount
        . "`nname:`t"   StrGet(&info)
        . "`nvk:`t"     vk
}

RegisterRawInputDevices(usagePage, usage) {
    static RIDEV_INPUTSINK := 0x00000100
    VarSetCapacity(rawDevice, 8 + A_PtrSize)
    NumPut(usagePage,       rawDevice, 0, "UShort")
    NumPut(usage,           rawDevice, 2, "UShort")
    NumPut(RIDEV_INPUTSINK, rawDevice, 4, "UInt")
    NumPut(A_ScriptHWND,    rawDevice, 8, "UPtr")

    if !DllCall("RegisterRawInputDevices"
        , "Ptr",  &rawDevice
        , "UInt", 1
        , "UInt", 8 + A_PtrSize)
    {
        throw "Fail"
    }
}
Noesis
Posts: 301
Joined: 26 Apr 2014, 07:57

Re: WM_INPUT(0xFF) - Problem with delays!

09 Dec 2017, 00:20

I'd suggest it's mainly to do with the granularity of ahk's Sleep command.
Due to the granularity of the OS's time-keeping system, Delay is typically rounded up to the nearest multiple of 10 or 15.6 milliseconds (depending on the type of hardware and drivers installed).
Check the link for some examples to get shorter delays, or you could perhaps use sleep -1 if it serves your purpose.

Another factor to consider is that GUI's (and tooltips for that matter) will also slow down the code in these instances. It takes time for them to be, updated as they need to be rendered & displayed each & every time they are updated, and when you're updating them rapidly by displaying inputs from things like mouse movements there will be a noticeable & significant lag in performance. Most the time simply not "displaying" anything to do with movements will significantly improve the code performance, i.e. CPU's, Memory, Input devices are updated faster than Monitors, the Monitor is the bottleneck if you're displaying stuff (especially when in a single thread). Bear in mind however, even without displaying things, the code handling these sorts of scenarios also needs to be as efficient as possible.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: RandomBoy, scriptor2016 and 370 guests