Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Applying the hs.mouse.trackingSpeed() setting #3744

Open
guillaume-azarias opened this issue Jan 29, 2025 · 8 comments · May be fixed by #3758
Open

Applying the hs.mouse.trackingSpeed() setting #3744

guillaume-azarias opened this issue Jan 29, 2025 · 8 comments · May be fixed by #3758

Comments

@guillaume-azarias
Copy link

Hi,

I want to set the mouse tracking speed according to my location. I added this script to my init.lua file loaded at start by Hammerspoon:

home_wifi = "home_wifi_name"
work_wifi = "work_wifi_name"

function updateMouseSpeed()
    local currentWiFi = hs.wifi.currentNetwork()
    
    if hs.fnutils.contains(home_wifi, currentWiFi) then
        hs.mouse.trackingSpeed(3.0)  -- Maximum speed
        hs.alert.show("Mouse tracking speed set for home location")
    elseif currentWiFi == work_wifi then
        hs.mouse.trackingSpeed(0.0)  -- Minimum speed
        hs.alert.show("Mouse tracking speed set for work location")
    else
        hs.mouse.trackingSpeed(1.5)  -- Default speed
        hs.alert.show("Default mouse speed")
    end
end

-- Run the script at start
local tracking_speed = hs.mouse.trackingSpeed()
hs.alert.show(tracking_speed)
updateMouseSpeed()
local tracking_speed = hs.mouse.trackingSpeed()
hs.alert.show(tracking_speed)

The value shown of hs.mouse.trackingSpeed() is changed by the location, but it does not affect the real value set in the system preferences. Is there a way to apply the setting ?
Thank you for your support,
Guillaume

MacBook Pro, M1 Pro, 2021, macOS Sequoia 15.2

@asmagill
Copy link
Member

FWIW, when I use hs.mouse.trackingSpeed (and I have an external USB mouse attached), it updates the Mouse Tracking Speed, not the Trackpad Tracking Speed...

Are you using an external mouse or the trackpad? If the trackpad, I'm not sure that the current code can do that -- it doesn't seem to on my M2 MacBook Pro running Sonoma 14.7.1 (suppose I need to upgrade at some point)... I can try and dig a little, but it will probably be this weekend or next week before I can.

@asmagill
Copy link
Member

I should add, even with the mouse not attached, the trackpad setting doesn't change, so... assuming it's a limitation of the current code until I can look closer at how it's doing the work...

@asmagill
Copy link
Member

asmagill commented Feb 1, 2025

Ok, I have identified that the trackpad uses a different property name (HIDTrackpadAcceleration instead of HIDMouseAcceleration), and have added code to allow getting and setting it, similar to the existing mouse code; however, while it does change the value shown in the System Settings, it does not seem to actually change the active tracking speed; I suspect a logout/login might apply the new value, since it does seem to be adjusting whatever it is System Settings queries, but a way to do so at the time of the change should also be possible... I'm going to have to dig more and play with it a bit before uploading a pull request.

@guillaume-azarias
Copy link
Author

Hi @asmagill ,
Thank you very much for your investigation. I use a usb mouse connected to my computer through a docking station. My issue is the same as what you describe for the trackpad:

while it does change the value shown in the System Settings, it does not seem to actually change the active tracking speed

Easier than logging out and in, I can adjust the mouse tracking speed by moving the slider in the system preferences, but of course, the goal of the function is to immediately set the speed.

Looking forward to seeing if you find a solution :-)

@asmagill
Copy link
Member

asmagill commented Mar 1, 2025

Not having much luck so far, but I haven't given up yet... I'll try and give a more thorough status update in the next couple of days, if only to list things that don't work

@asmagill
Copy link
Member

asmagill commented Mar 9, 2025

Ok, I've pushed a PR to address this that seems to be working for now -- it uses deprecated functions, so in theory Apple could bork it at any time, but its the only way I've found so far that actually works with Sonoma at least.

If you know how to build your own custom version of Hammerspoon, please do so with these changes and let me know if it works with Sequoia. I don't want to push it into master until its been reviewed and tested with the latest OS, so if you aren't sure how (it's in the wiki or issues, I just don't have a link handy atm), then I can try and make a precompiled version available in a day or so.

@guillaume-azarias
Copy link
Author

Hi @asmagill ,

I tested your branch and the speed of the mouse is indeed set by the function now. So it works on 15.3.1... but I noticed that now the value of the slider remains static although the speed is changed. So on my side, it is the expected function but the merge to Master might be controversial.
Since I am not an expert, it might be good if someone else can confirm or not my observation.

In any case, many thanks for your support !

@asmagill
Copy link
Member

I don't have a mouse with me right now (I'm traveling atm), so I can't test that, but I've noticed that if I have the System Panel open to the Trackpad settings when I make the change via Hammerspoon, it doesn't update in the System Panel until I visit another section (say General) and then go back to the Trackpad section -- then it will reflect the new value.

Have you tried that? If so, and it's still not updating in the settings, then I'm not sure... I could change the code so that it does both changes (i.e. the previous version of editing the IOKit properties directly and the updated approach of using IOHIDSetAccelerationWithKey).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants