Advertisement
TheNoobPolice

MouseDelta to Vertical Scroll

Jun 6th, 2023 (edited)
1,409
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #SingleInstance Force
  2. #NoEnv
  3. SetBatchlines -1
  4. Sendmode Input
  5. key := "RAlt"
  6.  
  7. running := minSpeed := startValue := hideState := 0
  8.  
  9. ; register for rawinput
  10. _size := 8 + A_PtrSize
  11. RIDEV_INPUTSINK := 0x00000100
  12. VarSetCapacity(RAWINPUTDEVICE, _size)
  13. NumPut(1, RAWINPUTDEVICE, 0, "UShort")
  14. NumPut(2, RAWINPUTDEVICE, 2, "UShort")
  15. NumPut(RIDEV_INPUTSINK, RAWINPUTDEVICE, 4, "Uint")
  16. Gui +hwndhwnd
  17. NumPut(hwnd, RAWINPUTDEVICE, 8, "Uint")
  18. DllCall("RegisterRawInputDevices", "Ptr", &RAWINPUTDEVICE, "UInt", 1, "UInt", _size )
  19. OnMessage(0x00FF, "RawInputCallback")
  20.  
  21. ; store the existing scrollline setting so it can be restored on exit
  22. SetScrollLines()
  23. OnExit("RestoreScrollLines")
  24.  
  25. HotKey, % key " Up", revealCursor_stopScroll
  26. Return
  27.  
  28. SetScrollLines(value := 3) {
  29.     global startValue
  30.    
  31.     DllCall("SystemParametersInfo", UInt, 0x0068, UInt, 0, UIntP, CurrentScrolls, UInt, 0)
  32.    
  33.     if !startValue
  34.         startValue := CurrentScrolls
  35.  
  36.     if CurrentScrolls != value
  37.         DllCall("SystemParametersInfo", UInt, 0x0069, UInt, value, UIntP, 0, UInt, 0)
  38. }  
  39.  
  40. RestoreScrollLines() {
  41.     global startValue
  42.     DllCall("SystemParametersInfo", UInt, 0x0069, UInt, startValue, UIntP, 0, UInt, 0)
  43. }
  44.  
  45. ; Get mouse movement rawinput and fire a scroll function with x and y deltas
  46. RawInputCallback(wParam, lParam) {
  47.     Critical
  48.     static DeviceSize := 2 * A_PtrSize
  49.     , Size := 0
  50.     , pSize:= 8 + 2 * A_PtrSize
  51.     , delta:= {x: 20 + (A_PtrSize * 2), y: 24 + (A_PtrSize * 2)}
  52.     , RawInput
  53.    
  54.     VarSetCapacity(header, pcbSize, 0)
  55.     DllCall("GetRawInputData", "UPtr", lParam, "uint", 0x10000005, "UPtr", &header, "Uint*", pcbSize, "Uint", pSize)
  56.    
  57.     VarSetCapacity(RawInput, Size)
  58.     DllCall("GetRawInputData", "UInt", lParam, "UInt", 0x10000003, "Ptr", &RawInput, "UInt*", Size, "UInt", pSize)
  59.  
  60.     x := NumGet(&RawInput, delta.x, "Int"), y := NumGet(&RawInput, delta.y, "Int")
  61.  
  62.     if (y != 0)
  63.         Scroll(x, y)
  64. }
  65.  
  66. ; get the velocity of input and start a timer to repeat scrolling up or down by mouse speed & direction (move at least 45 degrees vertically and at least 1 pixel / ms)
  67. Scroll(x, y) {
  68.     static minThreshold := 1, angleThreshold := atan(1), velocity := 0.01, last_direction := 0
  69.     global hideState, key, minSpeed, running
  70.    
  71.     if (y > 0 && last_direction != -1){
  72.         last_direction := -1
  73.         minSpeed := 0
  74.         velocity := 0.01
  75.     }
  76.     else if (y < 0 && last_direction != 1) {
  77.         last_direction := 1
  78.         minSpeed := 0
  79.         velocity := 0.01
  80.     }
  81.    
  82.     velocity := 0.5 * ((sqrt(x*x + y*y) / GetTime()) - velocity) + velocity
  83.     angle := x ? atan(abs(y / x)) : angleThreshold * 2
  84.    
  85.     if (angle < angleThreshold || velocity < minThreshold)
  86.         return
  87.    
  88.     if GetKeyState(key, "P") {
  89.         if !hideState {
  90.             showMouse(0)
  91.             hideState := 1
  92.         }
  93.         num_lines := (velocity > 12) ? "6" : (velocity > 10) ? "3" : (velocity > 6) ? "2" : "1"
  94.         if (num_lines > minSpeed)
  95.             minSpeed := num_lines
  96.         centreCursor(), SetScrollLines(minSpeed)
  97.         if (y > 0 && running != -1) {
  98.             running := -1
  99.             SetTimer RepeatDown, Off
  100.             SetTimer RepeatUp, 0
  101.         }
  102.         else if (y < 0 && running != 1) {
  103.             running := 1
  104.             SetTimer RepeatUp, Off
  105.             SetTimer RepeatDown, 0
  106.         }
  107.     }
  108. }
  109.     GetTime() {
  110.         static last := 0, freq := 0, scaler_ms := 0
  111.  
  112.         if (!freq) {
  113.             DllCall("QueryPerformanceFrequency", "Int64P", freq)
  114.             scaler_ms := 1000 / freq
  115.         }
  116.          
  117.         DllCall("QueryPerformanceCounter","Int64P", current)
  118.         elapsed := (current - last) * scaler_ms
  119.         last := current
  120.         return elapsed
  121.     }
  122.     RepeatUp() {
  123.         Send {WheelUp}
  124.     }
  125.     RepeatDown(){
  126.         Send {WheelDown}
  127.     }
  128.  
  129.  
  130. ; hide and centre the mouse cursor while scrolling
  131. showMouse(state) {
  132.     static BlankCursor := "", CleanUp := {base: {__Delete: "showMouse"}}
  133.     static CursorList := "32512, 32513, 32514, 32515, 32516, 32640, 32641"
  134.         . ",32642, 32643, 32644, 32645, 32646, 32648, 32649, 32650, 32651"
  135.     local ANDmask, XORmask, CursorHandle
  136.     if state
  137.         return, DllCall("SystemParametersInfo", "UInt", 0x57, "UInt", 0, "Ptr",  0, "UInt", 0)
  138.     if !BlankCursor {
  139.         VarSetCapacity(ANDmask, 32 * 4, 0xFF)
  140.         VarSetCapacity(XORmask, 32 * 4, 0x00)
  141.         BlankCursor := DllCall("CreateCursor", "Ptr", 0 , "Int", 0, "Int", 0, "Int", 32, "Int", 32, "Ptr", &ANDmask, "Ptr", &XORmask, "Ptr")
  142.     }
  143.     loop, Parse, CursorList, `,, % A_Space
  144.     {
  145.         CursorHandle := DllCall("CopyImage", "Ptr",  BlankCursor, "UInt", 2, "Int",  0, "Int",  0, "UInt", 0, "Ptr")
  146.         DllCall("SetSystemCursor", "Ptr",  CursorHandle, "UInt", A_Loopfield)
  147.     }
  148. }
  149.     revealCursor_stopScroll() {
  150.         global hideState, minSpeed, running
  151.        
  152.         running := minSpeed := 0
  153.  
  154.         if hideState {
  155.             showMouse(1)
  156.             hideState := 0
  157.         }
  158.         SetTimer RepeatUp, Off
  159.         SetTimer RepeatDown, Off
  160.     }
  161.     centreCursor() {
  162.         static centerX := A_ScreenWidth / 2, centerY := A_ScreenHeight / 2
  163.         DllCall("SetCursorPos", "Int", centerX, "Int", centerY)
  164.     }
  165.    
  166. ^Esc::ExitApp
  167.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement