Jump to content


Photo

Mouse Wheel Emulator


  • Please log in to reply
21 replies to this topic

#1 Blahman

Blahman
  • Members
  • 25 posts

Posted 18 October 2009 - 08:12 AM

Download
How many people out there are stuck with a laptop with no scroll wheel, or an old but comfortable two-button mouse they refuse to let go? Here's a treat for you.

This script combines the functionality of TheGood's AHKHID and ManaUser's MakeChord libraries to provide emulated middle-click and scroll wheel abilities to users of mice, trackpads and trackballs without a scroll wheel.

I've included this script, along with the necessary AHK libraries in the ZIP file linked above. Extract all 3 files to the same folder and run MouseWheelEmulator.ahk. You can also use AHK's script compiler to make an EXE out of it that you can run from any folder without the library dependencies.

For more information and instructions see the comments in the script. Enjoy!

;; MouseWheelEmulator.ahk

/*

MouseWheelEmulator
Created by Blahman (blah238 at gmail dot com)
v1.0
10/18/2009

Summary: This script combines the functionality of TheGood's AHKHID and ManaUser's MakeChord libraries to provide emulated middle-click and scroll wheel abilities to users of mice, trackpads and trackballs without a scroll wheel.

Features:
-Allows middle clicks and mouse wheel scrolling to be performed on hardware without a physical scroll wheel
-Freezes the mouse cursor in place during virtual mouse wheel scrolling
-Sends scroll wheel messages to window or control under cursor, not just the active window (in supported applications; see note about different scroll modes below)

Installation:
1) Download the AHKHID and MakeChord libraries and place the AHKHID.ahk and MakeChord.ahk files in the same folder as this script.
AHKHID: http://www.autohotkey.com/forum/topic41397.html
MakeChord (just the second half that starts with the MakeChord() function): http://www.autohotkey.com/forum/topic44399.html
2) Run this script.

Usage:
By default there are two ways to activate a middle click or scroll the virtual mouse wheel:
To perform middle-click:
Click the left and right mouse buttons simultaneously (often referred to as "chording").
-or-
Hold Alt and right click.

To scroll the virtual mouse wheel:
Click and hold the left and right mouse buttons simultaneously and move the mouse in the direction you wish to scroll.
-or-
Hold Alt, click and hold the right mouse button, and move the mouse in the direction you wish to scroll.

Note: If you need to terminate the script you can use Ctrl-Alt-Break.

About the different scroll modes:
There are several different ways that any given program may implement mouse scrolling. AHK has built-in WheelUp and WheelDown functions, but not all applications respond to them.
Some applications respond to WM_VSCROLL/WM_HSCROLL messages, while others respond to WM_MOUSEWHEEL/WM_HSCROLL messages.
If you find an application you use doesn't work with this script out of the box, you can probably fix it yourself by adding that application's process name to the conditional statements in the GetScrollMode() function.
The default scroll mode, 0, is AHK's built-in WheelUp and WheelDown commands. This does not support horizontal scrolling or scrolling the window or control under the cursor, while the other two modes do.
Some applications respond to more than one scroll mode, so you can try them all and decide which works best for you.
Finally, to further muddy the waters, some applications have frames within them that respond to scroll messages differently to the rest of the application. An example of this is the AHK help file, which uses the Internet Explorer_Server1 control to display HTML pages in one frame, and standard Windows controls to display the table of contents, index, etc. in another frame, and each responds to different scroll modes. I have tried to account for this in the GetScrollMode() function as well, but there may be other implementations I have not covered. You can use AHK's Window Spy to determine the name of the non-conforming control and write an exception for it, similar to the examples I have provided.

*/

;; Configuration

mouse_Threshold = 3 ; the number of pixels the mouse must move for a scroll tick to occur
MakeChord("LButton", "RButton", "scrollChord", 20) ; Chord to activate middle click or scrolling. See MakeChord.ahk for instructions
scroll_Hotkey = !RButton ; Hotkey to activate middle click or scrolling

;; End Configuration

#SingleInstance Force
#NoEnv
#Persistent
SendMode Input
Process, Priority, , Realtime
#Include %A_ScriptDir%\AHKHID.ahk

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

;Intercept WM_INPUT messages
OnMessage(0x00FF, "InputMsg")

SetDefaultMouseSpeed, 0
scrollMode = 0 ; 0 = MouseClick, WheelUp/WheelDown, 1 = WM_VSCROLL/WM_HSCROLL, 2 = WM_MOUSEWHEEL/WM_HSCROLL
CoordMode, Mouse, Screen


HotKey, %scroll_Hotkey%, scrollChord
HotKey, %scroll_Hotkey% Up, scrollChord_Up
return

scrollChord:
mouse_Moved = n
BlockInput, MouseMove
MouseGetPos, m_x, m_y, winID, control
WinGet, procName, ProcessName, ahk_id %winID%
hw_m_target := DllCall( "WindowFromPoint", "int", m_x, "int", m_y )
GetScrollMode()
HID_Register(1, 2, hGui, RIDEV_INPUTSINK)
return

scrollChord_Up:
ToolTip
BlockInput, MouseMoveOff
HID_Register(1,2,0,RIDEV_REMOVE)
if mouse_Moved = n
    MouseClick, Middle
return

InputMsg(wParam, lParam) {
    local x, y
    Critical
    
    x := HID_GetInputInfo(lParam, II_MSE_LASTX)
    y := HID_GetInputInfo(lParam, II_MSE_LASTY)
    If ((Abs(x) > 0.0) or (Abs(y) > 0.0))
       mouse_Moved = y
    if y > %mouse_Threshold%
    {
        ScrollDown()
    }
    else if y < -%mouse_Threshold%
        ScrollUp()
    if x > %mouse_Threshold%
    {
        ScrollRight()
    }
    else if x < -%mouse_Threshold%
        ScrollLeft()
    ;ToolTip, % "dX = " . x . "  " . "dY = " . y . a_tab . winID . a_tab . control . a_tab . procName . a_tab . hw_m_target . a_tab . scrollMode
    ;; Uncomment the above line for handy debug info shown while scrolling
}

GetScrollMode()
{
    global
    local ctl_x, ctl_y, ctl_w, ctl_h, ctl_hwnd, win_x, win_y
    if (procName = "hh.exe" or procName = "iexplore.exe" or procName = "dexplore.exe" or procName = "OUTLOOK.EXE")
    {
        scrollMode = 1
        WinGetPos, win_x, win_y, , , ahk_id %WinID%
        ControlGetPos, ctl_x, ctl_y, ctl_w, ctl_h, Internet Explorer_Server1, ahk_id %WinID%
        if (((m_x >= win_x + ctl_x) and (m_x <= win_x + ctl_x + ctl_w)) and ((m_y >= win_y + ctl_y) and (m_y <= win_y + ctl_y + ctl_h)))
            control = Internet Explorer_Server1
        else
            {
                ControlGetPos, ctl_x, ctl_y, ctl_w, ctl_h, NETUIHWND1, ahk_id %WinID%
                if (((m_x >= win_x + ctl_x) and (m_x <= win_x + ctl_x + ctl_w)) and ((m_y >= win_y + ctl_y) and (m_y <= win_y + ctl_y + ctl_h)))
                    scrollMode = 2
            }
    }
    else if (procName = "firefox.exe" or procName = "notepad.exe" or procName = "explorer.exe" or procName = "EXCEL.exe")
        scrollMode = 2
    else
        scrollMode = 0
    return
}

ScrollDown()
{
    global
    if (scrollMode = 0)
        MouseClick, WheelDown
    else if (scrollMode = 1)
        PostMessage, 0x115, 1, 0, %control%, ahk_id %winID%
    else
    {
       ; WM_MOUSEWHEEL
       ;   WHEEL_DELTA = 120
       PostMessage, 0x20A, -120 << 16, ( m_y << 16 )|m_x,, ahk_id %hw_m_target%
    }
}

ScrollUp()
{
    global
    if (scrollMode = 0)
        MouseClick, WheelUp
    else if (scrollMode = 1)
    {
       PostMessage, 0x115, 0, 0, %control%, ahk_id %winID%
    }
    else if (scrollMode = 2)
    {
       ; WM_MOUSEWHEEL
       ;   WHEEL_DELTA = 120
       PostMessage, 0x20A, 120 << 16, ( m_y << 16 )|m_x,, ahk_id %hw_m_target%
    }
}

ScrollRight()
{
    global
    if (scrollMode <> 0)
        loop, 2
            SendMessage, 0x114, 1, 0, %control%, ahk_id %winID%
}

ScrollLeft()
{
    global
    if (scrollMode <> 0)
        loop, 2
            SendMessage, 0x114, 0, 0, %control%, ahk_id %winID%
}

^!CtrlBreak::ExitApp

#Include %A_ScriptDir%\MakeChord.ahk


#2 Scrolless

Scrolless
  • Guests

Posted 21 October 2009 - 07:39 PM

Don't think I'm doing anything wrong, but maybe I'm missing something obvious. I can reload this script to get the chording or alt scrolling working... until the very first time I press the left or right mouse button by itself. After that the script appears to do nothing even though it's still ostensibly running. Any clues would be great.

#3 Blahman

Blahman
  • Members
  • 25 posts

Posted 22 October 2009 - 12:39 AM

Hmm that is odd.

I have tested the script in the ZIP file with the libraries I used to create it. I have also tested under Windows 7 RC1 64-bit and Windows XP 32-bit and have never seen that problem. I have also tested with the latest version of AHK as well as an older version.

I would say if you are familiar with AHK code to try debugging it yourself. First thing to try is to uncomment the ToolTip line that shows some useful info, and see if any of the values are different between when it works and when it doesn't.

#4 kecinzer

kecinzer
  • Members
  • 6 posts

Posted 22 November 2009 - 11:56 AM

This script looks very good.
But I don't see download link :(

#5 Blahman

Blahman
  • Members
  • 25 posts

Posted 22 November 2009 - 05:03 PM

This script looks very good.
But I don't see download link :(


It's at the very top in big bold blue letters :p

But here it is again: <!-- m -->http://www.autohotke... ... r-v1-0.zip<!-- m -->

#6 lanserffnt

lanserffnt
  • Members
  • 5 posts

Posted 31 December 2009 - 05:19 PM

please!

how change LButton & RButon to MButton.

I want to use this script on LENOVO NB .

NB have Middle .

#7 Blahman

Blahman
  • Members
  • 25 posts

Posted 31 December 2009 - 08:25 PM

please!

how change LButton & RButon to MButton.

I want to use this script on LENOVO NB .

NB have Middle .


Try changing the line:
scroll_Hotkey = !RButton ; Hotkey to activate middle click or scrolling
To this:
scroll_Hotkey = MButton ; Hotkey to activate middle click or scrolling


#8 Guests

  • Guests

Posted 01 January 2010 - 04:01 AM

Thanks! Blahman


It's work fine.

#9 bert

bert
  • Guests

Posted 18 March 2010 - 09:55 PM

Same problem as Scrolless above.
Works perfectly, but either a left or right click will stop it responding, and it requires a restart before it responds again.
Your tooltip suggestion doesn't really help.
This would be a great tool if it worked consistently. As it is, unfortunately it's kind of useless. If I can help, I'd be delighted to do what I can, but the code means very little to me - if I could manage more complex debugging, I would!
I'm left thanking you for your efforts and hoping you can fix this nagging problem.

#10 Stefan_

Stefan_
  • Guests

Posted 09 May 2010 - 11:48 AM

Is it possible to scroll when I click the right mouse button and move the mouse in the direction i wish to scroll ? But when I click the right mouse button and don't move the mouse, the popup menu should be visible ;)

#11 JohnDuncow

JohnDuncow
  • Members
  • 2 posts

Posted 02 January 2011 - 03:24 PM

Hi, I've just started using Railworks2 with a trackerball. Unfortunately a mouse wheel is mandatory for zoom in the map screen. I've tried your script, in all 3 scrollmodes, and there's no effect - the screen simply scrolls, as would be expected if I just held down the right button and moved the ball.

Tried it in Firefox and it functions ok, so the script is working.

I'm using Win 7 (x64). Any ideas ?

#12 Blahman

Blahman
  • Members
  • 25 posts

Posted 02 January 2011 - 03:29 PM

Will not work in games as they typically use low level mouse hook APIs that AHK can't really work with.

#13 JohnDuncow

JohnDuncow
  • Members
  • 2 posts

Posted 02 January 2011 - 03:38 PM

I suspected it might something (nasty) like that. Thanks for your quick response.

#14 Guests

  • Guests

Posted 08 January 2011 - 02:16 AM

How can I deactivate the ALT Hotkey ?

Using ALT -> Right Click for another Action.

Thx

Greeting

#15 Guests

  • Guests

Posted 08 January 2011 - 02:18 AM

Also sometimes it stuck a little bit with down scrolling.

Up scrolling works without problems.

Thx again