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

Not respecting key repeat OS setting #2026

Closed
probablykasper opened this issue Oct 19, 2022 · 34 comments
Closed

Not respecting key repeat OS setting #2026

probablykasper opened this issue Oct 19, 2022 · 34 comments
Labels
bug Something isn't working unreproducible Need help from the author to reproduce the issue

Comments

@probablykasper
Copy link

Describe the bug

AltTab key repeat is a lot slower than what I've set my OS key repeat setting to (I have key repeat on the fastest setting)

Screenshots / video

AltTab vs macOS cmd+tab:

Screen.Recording.2022-10-19.at.7.35.37.mp4

Steps to reproduce the bug

  1. Set a high key repeat macOS setting

Versions
AltTab: 6.47.0
OS: macOS Catalina 10.15.7

@probablykasper probablykasper added the bug Something isn't working label Oct 19, 2022
@probablykasper
Copy link
Author

Measured the speed difference:
AltTab takes 1.6s to go through 16 items
macOS takes 0.27s to go through 9 items

@lwouis
Copy link
Owner

lwouis commented Oct 19, 2022

I just tried on my machine (Monterey, M2, latest AltTab), and when I change the key repeat, or key repeat delay, AltTab correctly follows. When I slow it down, or speed it up, AltTab slows down or speeds it, and at the same speed as the system.

I'm not sure how to debug what's unique to your setup. Maybe you use an external keyboard, or software that interfere with key presses (e.g. BTT, Karabineer, etc)?

Maybe you could try resetting AltTab's preferences (at the bottom of the first preferences tab) and see if it fixes it for you?

@lwouis lwouis added the unreproducible Need help from the author to reproduce the issue label Oct 19, 2022
@probablykasper
Copy link
Author

I use an Apple Magic Keyboard on an x86 iMac. I do have Karabiner but disabled it when testing this.

Will try resetting the preferences

@probablykasper
Copy link
Author

Resetting the preferences fixed it, thanks! Only used AltTab for a few weeks, so not sure what caused it, maybe an update or something

@lwouis
Copy link
Owner

lwouis commented Oct 20, 2022

I think you may have set it manually. The Key Repeat value can be overriden for specific app. I'm guessing that you ran something like defaults write com.lwouis.alt-tab-macos KeyRepeat 1000 a while ago and forgot. And then AltTab was forced to that speed. Resetting the preferences clearer every preferences you set through the UI and also this one you manually set from the terminal.

Or it could be something else like migrating your mac, or who knows. Anyway, glad it's fixed 👍

@probablykasper
Copy link
Author

@lwouis Just restarted my pc, and the issue came back. I have AltTab set to start at login. Relaunching AltTab fixes it. Maybe it tries to read the key repeat setting too early?

@lwouis
Copy link
Owner

lwouis commented Oct 25, 2022

This is very unexpected. What does this command return?

defaults read com.lwouis.alt-tab-macos KeyRepeat

@probablykasper
Copy link
Author

Right now it says this:

2022-10-25 22:20:23.304 defaults[73936:880657]
The domain/default pair of (com.lwouis.alt-tab-macos, KeyRepeat) does not exist

@lwouis
Copy link
Owner

lwouis commented Oct 25, 2022

The KeyRepeat preference is read every time you press and hold the tab key. So it's basically "live", as opposed to being read once at launch for instance.

The code grabs that value from the OS, which reads from the System Preferences, or from the app's preference (see the command I gave you above) if defined (manually since AltTab has no UI to override it at app-level). Finally, if somehow the OS returns no value, it defaults to 6. I just tested and 6 is normal speed, nothing like in your video.

I'm out of idea on what could possibly be happening on your system. I can't reproduce on my machine. I strongly suspect you have some hardware and/or software intervening here.

An idea maybe: are you sure you only have 1 instance of AltTab running? Can you check your top menubar, and/or Activity Monitor? I've seen issues in the past with people having multiple instances at once, which could then have the tab key triggers multiple times or something. That could explain why you face the issue again after a restart.

@probablykasper
Copy link
Author

probablykasper commented Oct 26, 2022

Tried restarting, and there's only one AltTab instance.

Tried manually setting com.lwouis.alt-tab-macos KeyRepeat, and it does seem like 6 is the speed it goes to when I restart. That also made me realize the Key repeat delay also has the same issue

Edit: I checked defaults read -g KeyRepeat and it says my key repeat is 2. I then ran defaults write com.lwouis.alt-tab-macos KeyRepeat -int 2 , and that resulted in a slower speed than expected. Once I relaunched, the speed was as expected. So it seems like the KeyRepeat detection isn't the issue

@lwouis
Copy link
Owner

lwouis commented Oct 27, 2022

@probablykasper I'm not sure to follow:

  • You system-level value is 2
  • When you set AltTab value to 6 -> too fast (like in the video)?
  • When you set AltTab value to 2 -> what's the speed exactly? -> Then you restart AltTab -> what's the speed exactly?

Then your conclusion is that AltTab respects the value, at system-level and app-level?

@probablykasper
Copy link
Author

Sorry, let me clarify. I tried manually setting the KeyRepeat to what my OS has, and it didn't change the key repeat. If I then change it to 6, it gets updated, but it's still slower than what a key repeat of 6 should be.

So it does read the key repeat value, but whatever the value is, it seems to be slowed down

@lwouis
Copy link
Owner

lwouis commented Oct 27, 2022

I tried manually setting the KeyRepeat to what my OS has, and it didn't change the key repeat

At this point, is AltTab repeat rate matching the OS repeat rate?

@probablykasper
Copy link
Author

At this point, is AltTab repeat rate matching the OS repeat rate?

When the bug happens, it's not matching the OS repeat rate or the manual repeat rate, but it does read it. If the setting is changed, AltTab changes too, but it's slower than what it should be

@lwouis
Copy link
Owner

lwouis commented Oct 27, 2022

I'm still not clear on what the various settings do 😅 Could you please fill this table? Feel free to add more values you experimented with.

KeyRepeat value set for AltTab Observed repeat-rate in AltTab
Not set Same as the OS?
2 Slower than the OS?
6 Faster than OS?

@probablykasper
Copy link
Author

probablykasper commented Oct 28, 2022

All good, sorry for not explaining it well enough.

The observed repeat-rate is x% slower than the KeyRepeat value.

  • If the KeyRepeat value comes from the OS, it's x% slower than that.
  • If the KeyRepeat value is set for AltTab, it's x% slower than that.
KeyRepeat value set for AltTab OS KeyRepeat value Observed repeat-rate in AltTab
Not set 6 Too slow, normal after relaunch
6 6 Same as above
2 6 Not as slow as the others, too fast after relaunch

@lwouis
Copy link
Owner

lwouis commented Oct 28, 2022

  • What's the refresh rate of your monitor?
  • Could you please test if this build fixes your issue?
  • I'm a bit confused by the first line in the table you shared. If the repeat-rate is normal after a restart, it sounds like you don't have an issue in the first place if you don't set a value in the Terminal manually. It sounds like now the repeat-rate is normal and your situation is fixed, no?

@probablykasper
Copy link
Author

probablykasper commented Oct 28, 2022

  • What's the refresh rate of your monitor?

60 Hz

  • Could you please test if this build fixes your issue?

Unfortunately not. Might not have anything at all to do with AltTab, but for the first 15-30s the tab repeat was a bit laggy, as in some key repeats were delayed/inconsistent. Probably just because I just restarted my pc, but thought I'd mention it

  • I'm a bit confused by the first line in the table you shared. If the repeat-rate is normal after a restart, it sounds like you don't have an issue in the first place if you don't set a value in the Terminal manually. It sounds like now the repeat-rate is normal and your situation is fixed, no?

Sorry, I meant after relaunching AltTab, not restarting

@probablykasper
Copy link
Author

For some reason the key repeat speeds up at the end here:

AltTab.KeyRepeat.mp4

These are the frames where the selected item changes:
image

@lwouis
Copy link
Owner

lwouis commented Oct 28, 2022

For some reason the key repeat speeds up at the end here:

I don't see that speed-up on the video.

When you press the trigger key (e.g. tab), the OS sends one key event, then after an initial delay called InitialKeyRepeat, the OS starts repeating the event, at the speed called KeyRepeat.

Both KeyRepeat and InitialKeyRepeat can be set by the user in System Preferences, using these sliders:

image

To me, what is happening in your video is expected behavior.

Your main issue is that AltTab is not at the same speed as other apps in the OS, sometimes, right? You showed in the video in the OP that the native app-switcher is faster than AltTab. I don't know how to explain that. It's exactly at the same speed on my machine.

Is everything in your system at the same speed? Like if you select a file in a folder with many files, and you hold down arrow. Does it go at the same very fast speed as the native app-switcher? What about holding a letter-key in the Terminal, does it print the key repeatedly at that same speed?

But then in your later comments here, you say that if you don't set any variable for AltTab, it's first too slow, then after you restart AltTab, it's at the correct speed. This seems to contradict your original post at the top, where you had not set any variable, but you showed on the video that it's too fast, not too slow.

I'm getting lost with what behavior is the problem in this ticket 😅

@probablykasper
Copy link
Author

probablykasper commented Oct 29, 2022

I don't see that speed-up on the video.

I pointed out which frames it happens on in the image below the video I wasn't talking about the Delay until repeat though. Here's some more explanation:
Screenshot 2022-10-29 at 3 02 22

So every key repeat had 6 frames of delay, but the switch at frame 1:52 took only 5 frames, and the one at 1:55 took only 3 frames.

Also, that video is after relaunching AltTab, the slowness isn't in that one, just thought it would be worth sharing in case it could be helpful (sorry for not mentioning).

Your main issue is that AltTab is not at the same speed as other apps in the OS, sometimes, right?

Yes. AltTab is slower than everything else on the OS after restarting the computer. After relaunching AltTab, the speed is pretty much as expected.

Is everything in your system at the same speed?

Yes, this is only ever an issue with AltTab. Finder file selection, writing text in the terminal, tabbing in browsers etc, it's all the correct speed. The slowness in AltTab is very noticeable to me.

But then in your later comments here, you say that if you don't set any variable for AltTab, it's first too slow, then after you restart AltTab, it's at the correct speed. This seems to contradict your original post at the top, where you had not set any variable, but you showed on the video that it's too fast, not too slow.

I don't think I've ever said AltTab is too fast? The original post has a video where AltTab is slower than the native tab switcher. It's never been faster than expected.

If the table is what was confusing, hopefully this clears it up:

KeyRepeat for AltTab OS KeyRepeat Observed repeat-rate in AltTab
Not set 6 Slower than 6 after restarting PC, 6 after relaunching AltTab
6 6 Same as above
2 6 Slower than 2 after restarting PC, 2 after relaunching AltTab

@lwouis
Copy link
Owner

lwouis commented Oct 29, 2022

Thank you for all the clarifications!

I'm really not sure what could be at play here. The fact that restarting AltTab fixes the issue seems to imply that AltTab code is correct, but that there is something about its environment that can vary. For example, maybe at launch, the OS provides the wrong value somehow? Or maybe the AltTab process launched by launchd gets lower priority for its timers and they get slowed down. Not sure. The fact that i can't reproduce it makes it hard to investigate.

@probablykasper
Copy link
Author

probablykasper commented Oct 29, 2022

For example, maybe at launch, the OS provides the wrong value somehow?

I don't think that's it because AltTab reacts to when the KeyRepeat value is changed

Maybe this could somehow be fixed by using a different approach to handling key presses, where AltTab itself doesn't handle key repeat/delay itself? I know for example Contexts doesn't have this issue.

@lwouis
Copy link
Owner

lwouis commented Nov 2, 2022

Regarding the inconsistent pacing of the repeat keys, the only explanation I see is in Apple documentation for Timer.tolerance:

The system reserves the right to apply a small amount of tolerance to certain timers regardless of the value of this property.

So essentially the system doesn't honor user timer strictly. Thus we may drop frames here and there. In addition, we do some UI rendering stuff. Maybe the repeat events are perfectly fired (seems to be the case when I measure), but then the UI rendering of moving the selection around has some rendering costs and lags a few frames sometimes. It's my best guess. Unfortunately, I spend hours upon hours on this topic already (see the length of #563), and I think it's good enough at this point. I need an absolute expert in AppKit now to push the performance to be smoother.


Regarding the issue in the OP: after a fresh restart of the machine, AltTab is twice as slow to repeat. I'm not sure what to think here. The difference between this and after restart would be that at login it's launched by launchd, whereas a restart means the new start is under the user directly.

A way to test if the initiator/parent is the issue is to logout -> login fresh, then restart AltTab but not manually. Instead restart it using launchd itself by running launchctl kickstart -k gui/501/com.lwouis.alt-tab-macos.

If it's still too slow, then it means the app being launched by launchd is the issue. If it's at correct speed, then something else happens at login. @probablykasper could you please confirm what happens here?

@probablykasper
Copy link
Author

Regarding the inconsistent pacing of the repeat keys

All good, I only mentioned it in case it was related. Sounds like it isn't, so it's all good.


When I run launchctl kickstart -k gui/501/com.lwouis.alt-tab-macos , it doesn't seem to restart. I'm guessing it's because my user ID is 502?

If I quit AltTab and then run launchctl kickstart gui/502/com.lwouis.alt-tab-macos the issue does appear.

@lwouis
Copy link
Owner

lwouis commented Nov 2, 2022

Thank you. So, what's the repeat rate after your restart with the command?

@probablykasper
Copy link
Author

With the command I mentioned, it seems to be as slow as in the original video

@lwouis
Copy link
Owner

lwouis commented Nov 2, 2022

Oh, that's big news! It means it's launchd who creates the sluggishness. Can you run the command a few times and really confirm that it's slow? And then maybe quit and start AltTab manually and confirm that it doesn't have the issue that way?

@probablykasper
Copy link
Author

Yeah, can confirm that's the case

@lwouis
Copy link
Owner

lwouis commented Nov 2, 2022

I was able to reproduce the issue on my machine! That's a great step. Now I can investigate what is going on. Thanks @probablykasper for you support~

@probablykasper
Copy link
Author

Awesome !! :D

@lwouis
Copy link
Owner

lwouis commented Nov 2, 2022

Timer ticks, in milliseconds, when AltTab is launched by the user:

33
33
34
33
33
33
33
33
34

Timer ticks, in milliseconds, when AltTab is launched by launchd:

43
5
103
100
100
100
100
102
98

So, timer ticks, with launchd, are often at 100ms instead of the expected 33ms, and are sometime wild (e.g. 5ms).

@lwouis
Copy link
Owner

lwouis commented Nov 2, 2022

I found the issue! When launched through launchd, we need to declare some extra fields in the plist file, otherwise AltTab gets low prio/resources from the OS.

This is an amazing discovery that will improve the overall performance/snapiness of AltTab, when launched after a restart. I'm very grateful to you @probablykasper for having supported me in this investigation. The fruit of this work will benefit many users that may finally be rid of "absorbed" key presses and other visual lags.

Can you confirm that this build fixes the issue for you like it does for me?

@lwouis lwouis closed this as completed in 1821dea Nov 2, 2022
lwouis pushed a commit that referenced this issue Nov 2, 2022
# [6.49.0](v6.48.0...v6.49.0) (2022-11-02)

### Bug Fixes

* command+backtick not working if stage manager is on (closes [#2053](#2053)) ([848ae5f](848ae5f))
* crash when setting some shortcut combinations (closes [#2061](#2061)) ([8b2d659](8b2d659))
* hide minimize and fullscreen thumbnail buttons for tabs ([b62c422](b62c422))
* improve key repeat-rate when held (closes [#2026](#2026)) ([1821dea](1821dea))
* key-above-tab on international keyboards (closes [#1190](#1190)) ([4c31740](4c31740))
* thumbnails would sometimes be the wrong size ([1065c0d](1065c0d))
* wrong focus after active app becomes windowless (closes [#2065](#2065)) ([281b3ed](281b3ed))

### Features

* improve some localizations ([292e6b0](292e6b0))
* play alert sound for unavailable thumbnail shortcuts ([fd84a9a](fd84a9a))
* support adobe after effects non-standard windows (closes [#1982](#1982)) ([7b54873](7b54873))
@probablykasper
Copy link
Author

Yes, that build fixed it!! Amazing work, thank you so much

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working unreproducible Need help from the author to reproduce the issue
Projects
None yet
Development

No branches or pull requests

2 participants