You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Horrendous keyboard lag in games that compounds with time pressed #1013
It's as if scrcpy places "key pressed" events in a queue with every other event, which quickly clogs up and doesn't let other events through. E.g.: open any game that supports keyboard and press W for a brief moment and at the same time, try to swipe a screen. It goes through. Then, press W for ten seconds and then try to swipe the screen. Touch input goes through after a few seconds(!). And so on: input lag is proportional to the time spent pressing a keyboard button.
EDIT: Only some games have this issue. From what I have on my phone, Minecraft and Survivalcraft exhibit this problem while Sonic 1 does not. Apologies if this issue is not scrcpy's fault
I just tested a random game (minetest) on a tablet, and indeed I can reproduce (a bit) the problem.
However, I added some logs, which show all key and touch events are transmitted and injected immediately by scrcpy.
After some delay (sometimes a few seconds), the game reacts to the events, but there are no new event injected at that time. So it seems the device/game takes time to consume the events.
Not sure if there are possible workarounds on our side.
Maybe related to the issue, but I get a similar problem randomly sometimes on normal use. scrcpy still inputs everything as expected in the end, backspaces and all, but stuff like parsing a long link can take almost a minute.
For the record, I'm using the latest release with the --prefer-text modifier, but I remember this happening before the text fix.
I just did another test: inject key events by pressing a key for several seconds in scrcpy, then touch with a finger on the physical device. The touch events consumption is also delayed.
games instantly start working correctly, however this has a side-effect where now text apps would not work as expected where if you hold a key down it would start repeating.
Does disabling key-repeating make the most sense?
When would key-repeating be the desired scenario other then testing if keys were being sent?
Perhaps a shortcut combo to toggle between key-repeating and not repeating?
By default, letters are sent as key events (instead of text events), unless --prefer-text is passed (see #87), so repeated events must be sent. Instead, maybe we should send letters as text events by default, and add an option --game-mode or similar, where letters are passed as key events and not repeated…
However, this will cause the same issue for other keys like Space and Enter (keeping space pressed should inject several spaces).
I had tried prefer-text before this and character would move but then stop, so i then started looking at what was going on.
Do you know what the actual behavior is on android when holding down a key via a physical keyboard? If my understanding of the current system is correct, isn't the key repeating a hack since we are sending a keydown event, shouldn't that mean it's the responsibility of whatever app to repeat the key?
On the server-side, can you check if the virtual keyboard is open, or even better if a text input is active? If you can then it would make sense to not process the duplicate keydown events when either of those cases was false.
Anyway, I think adding a command-line option --no-key-repeat would be acceptable. What do you think?
My thought was if key repeating did not occur with a physical keyboard, then that would be a big part of if repeating would be enabled by default or not.
I'm not a huge fan of it being only a flag since you may want to switch between repeating mode and non repeating mode and it would be silly to have to restart the program with different flags each time.
That is what I just tested, but it seems it does not (if you disable repeat on the client side, it does not repeat letters or space).
is scrcpy incrementing the repeat counter on events past the first one? It seems like for every event past the initial keydown, repeat should be incremented by one, where as the value for the keyup is always 0.
On the client-side, SDL only reports repeat as a boolean (either 0 or 1). It is only used for ignoring repeated events on shortcuts, and is not forwarded to the device.
It would be interesting to know if passing the actual repeat value would solve the keyboard lag.
That's what im wondering, can you store a counter, unordered_map<key,int>, increment every time sdl repeat is true, wipe on repeat is false, and send the counter with the key press event?
I guess testing with one number would be a start, but if that does resolve the issue then it would be best to adhere to the spec and have the repeat value track each individual key. I guess an array would suffice since were working with only c.
Just to clarify my position: in any case, repeat should be correctly initialized for injecting events on the device. So unless it causes problems, this change should be done anyway.
Ok, so i modified my branch to limit a maximum of 40 repeat events to be generated. After 40 events were reached a fake keyup event was sent and all further events were ignored until an actual keyup event occurred.
I also did some additional testing with stardew valley. Stardew valley is much less cpu, gpu intensive and displayed no noticeable difference between versions until I went and locked the cpu frequency to its smallest possible value. Once the cpu frequency was limited, there was no difference between the hardcoded 0 value and the incrementing version. Both versions exhibited a noticeable delay when stopping. The version where repeat keys were not sent at all exhibited no delay.
In conclusion:
Incrementing the repeat value is either beneficial in scenarios where the cpu is somewhat bound or when the app ignores incoming repeat values
Not sending repeat keys offers the best performance, but will inhibit scenarios where the user does want the keys to actually be repeated, ie (delete,backspace, arrow keys, etc)
I think a flag is good, but a shortcut is better so that you can disable repeats, play games, then when you swap to sending messages you don't need to restart the program.
OK, so first let's inject the correct repeat value. :)
I think a flag is good, but a shortcut is better so that you can disable repeats, play games, then when you swap to sending messages you don't need to restart the program.
The issue is that a stateful toggle flag without visual feedback is confusing. That's why I haven't added one for --prefer-text.
But even with that, I'm not sure I want to consume a shortcut for such an advanced feature (which is only necessary on some devices, as a workaround to avoid sending too many events).
just using a single unsigned int for tracking the count?
Yes, and resetting it if and only if the current event has repeat set to false (otherwise, increment it).
Please also rebase on current dev and open a PR :)
Activity
rom1v commentedon Dec 16, 2019
Hi,
Thank you for your report.
I just tested a random game (minetest) on a tablet, and indeed I can reproduce (a bit) the problem.
However, I added some logs, which show all key and touch events are transmitted and injected immediately by scrcpy.
After some delay (sometimes a few seconds), the game reacts to the events, but there are no new event injected at that time. So it seems the device/game takes time to consume the events.
Not sure if there are possible workarounds on our side.
fefo-dev commentedon Dec 16, 2019
Maybe related to the issue, but I get a similar problem randomly sometimes on normal use. scrcpy still inputs everything as expected in the end, backspaces and all, but stuff like parsing a long link can take almost a minute.
For the record, I'm using the latest release with the --prefer-text modifier, but I remember this happening before the text fix.
rom1v commentedon Dec 16, 2019
I just did another test: inject key events by pressing a key for several seconds in scrcpy, then touch with a finger on the physical device. The touch events consumption is also delayed.
xeropresence commentedon Jun 10, 2020
After looking into #109 i think the issues are related.
With this small change https://github.com/xeropresence/scrcpy/commit/3d52cd5c91d070dc15ead33e1932e973d0e04c6a
games instantly start working correctly, however this has a side-effect where now text apps would not work as expected where if you hold a key down it would start repeating.
Does disabling key-repeating make the most sense?
When would key-repeating be the desired scenario other then testing if keys were being sent?
Perhaps a shortcut combo to toggle between key-repeating and not repeating?
rom1v commentedon Jun 10, 2020
@xeropresence thank you 👍
By default, letters are sent as key events (instead of text events), unless
--prefer-textis passed (see #87), so repeated events must be sent. Instead, maybe we should send letters as text events by default, and add an option--game-modeor similar, where letters are passed as key events and not repeated…However, this will cause the same issue for other keys like Space and Enter (keeping space pressed should inject several spaces).
I will think about it.
xeropresence commentedon Jun 10, 2020
I had tried prefer-text before this and character would move but then stop, so i then started looking at what was going on.
Do you know what the actual behavior is on android when holding down a key via a physical keyboard? If my understanding of the current system is correct, isn't the key repeating a hack since we are sending a keydown event, shouldn't that mean it's the responsibility of whatever app to repeat the key?
On the server-side, can you check if the virtual keyboard is open, or even better if a text input is active? If you can then it would make sense to not process the duplicate keydown events when either of those cases was false.
rom1v commentedon Jun 10, 2020
That is what I just tested, but it seems it does not (if you disable repeat on the client side, it does not repeat letters or space).
No (and it would be too hacky).
Anyway, I think adding a command-line option
--no-key-repeatwould be acceptable. What do you think?xeropresence commentedon Jun 11, 2020
My thought was if key repeating did not occur with a physical keyboard, then that would be a big part of if repeating would be enabled by default or not.
I'm not a huge fan of it being only a flag since you may want to switch between repeating mode and non repeating mode and it would be silly to have to restart the program with different flags each time.
So, I was reading the android documentation,
https://developer.android.com/reference/android/view/KeyEvent
is scrcpy incrementing the repeat counter on events past the first one? It seems like for every event past the initial keydown, repeat should be incremented by one, where as the value for the keyup is always 0.
rom1v commentedon Jun 11, 2020
On the device side, scrcpy always injects the events with
repeat= 0:scrcpy/server/src/main/java/com/genymobile/scrcpy/Controller.java
Line 134 in 3c0fc8f
On the client-side, SDL only reports
repeatas a boolean (either 0 or 1). It is only used for ignoring repeated events on shortcuts, and is not forwarded to the device.It would be interesting to know if passing the actual repeat value would solve the keyboard lag.
xeropresence commentedon Jun 11, 2020
That's what im wondering, can you store a counter, unordered_map<key,int>, increment every time sdl repeat is true, wipe on repeat is false, and send the counter with the key press event?
rom1v commentedon Jun 11, 2020
(This is C++) 😄
A simple counter should be sufficient (not one per key, in practice only one key may be repeated at the same time).
Do you want to work on that to see if it improves things?
Here is a list of things to do:
int repeatfieldcontrol_msg.h(incontrol_msg.inject_key_codesstruct) andControlMessage.javacontrol_msg_serialize()andparseInjectKeycode()repeatvalue inconvert_input_key()xeropresence commentedon Jun 11, 2020
I guess testing with one number would be a start, but if that does resolve the issue then it would be best to adhere to the spec and have the repeat value track each individual key. I guess an array would suffice since were working with only c.
I'll give it a shot, doesn't sound too bad.
rom1v commentedon Jun 11, 2020
A single
unsigned intis sufficient: if the current event hasrepeatenabled, increment the counter, otherwise, reset it to 0.14 remaining items
rom1v commentedon Jun 11, 2020
Just to clarify my position: in any case,
repeatshould be correctly initialized for injecting events on the device. So unless it causes problems, this change should be done anyway.xeropresence commentedon Jun 11, 2020
Sounds good. I'm currently trying to do some testing to quantify the results of the change using recording.
xeropresence commentedon Jun 11, 2020
Ok, so i modified my branch to limit a maximum of 40 repeat events to be generated. After 40 events were reached a fake keyup event was sent and all further events were ignored until an actual keyup event occurred.
Repeat with a hardcoded 0 value, v1.14
https://streamable.com/jlhi14
Repeat incrementing
https://streamable.com/d7l6bv
Not sending repeat at all
https://streamable.com/f2f5or
I also did some additional testing with stardew valley. Stardew valley is much less cpu, gpu intensive and displayed no noticeable difference between versions until I went and locked the cpu frequency to its smallest possible value. Once the cpu frequency was limited, there was no difference between the hardcoded 0 value and the incrementing version. Both versions exhibited a noticeable delay when stopping. The version where repeat keys were not sent at all exhibited no delay.
In conclusion:
Incrementing the repeat value is either beneficial in scenarios where the cpu is somewhat bound or when the app ignores incoming repeat values
Not sending repeat keys offers the best performance, but will inhibit scenarios where the user does want the keys to actually be repeated, ie (delete,backspace, arrow keys, etc)
I think a flag is good, but a shortcut is better so that you can disable repeats, play games, then when you swap to sending messages you don't need to restart the program.
rom1v commentedon Jun 11, 2020
OK, so first let's inject the correct
repeatvalue. :)The issue is that a stateful toggle flag without visual feedback is confusing. That's why I haven't added one for
--prefer-text.Oh, also, please implement on
dev(the development branch), notmaster: https://github.com/Genymobile/scrcpy/blob/master/BUILD.md#branchesxeropresence commentedon Jun 12, 2020
I've rebased my branch onto dev.
Sounds like a OSD could be useful for displaying information about state changes.
rom1v commentedon Jun 12, 2020
(see #1484)
But even with that, I'm not sure I want to consume a shortcut for such an advanced feature (which is only necessary on some devices, as a workaround to avoid sending too many events).
xeropresence commentedon Jun 17, 2020
What other changes would you like to see, just using a single unsigned int for tracking the count?
rom1v commentedon Jun 17, 2020
Yes, and resetting it if and only if the current event has repeat set to
false(otherwise, increment it).Please also rebase on current
devand open a PR :)Thank you 😉
Handle repeating keycodes
Handle repeating keycodes
Add --no-key-repeat cli option
Add --no-key-repeat cli option
rom1v commentedon Aug 6, 2020
So using v1.15 with
--no-key-repeatshould fix this issue. I'm closing.Handle repeating keycodes