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

Forward Alt and Super #5322

Merged
merged 5 commits into from
Oct 6, 2024
Merged

Forward Alt and Super #5322

merged 5 commits into from
Oct 6, 2024

Conversation

rom1v
Copy link
Collaborator

@rom1v rom1v commented Sep 27, 2024

Alt and Super (also named Meta) modifier keys are captured for shortcuts by default (cf --shortcut-mod).

However, when shortcut modifiers are changed, Alt and Super should be forwarded to the device. This is the case for AOA and UHID keyboards, but it was not the case for SDK keyboard.

This PR fixes this.


In addition, mouse capture key for relative mouse mode (when --mouse=aoa or --mouse=uhid) used hardcoded keys (Alt or Super), regardless of the configured shortcut modifier.

Replace these hardcoded keys by the modifiers keys (configured by --shortcut-mod).


As a result, if you use for example scrcpy --shortcut-mod=rctrl, then Alt and Super are forwarded to the device (in all modes).

Fixes #5318

Here is a binary to test:

@jackf723
Copy link

I tried scrcpy.exe -shortcut-mod=rctrl
Alt+left and right arrow do nothing while in the emulator. I am not sure if the alt key itself isn't being forwarded, but TalkBack isn't registering alt+arrow key movements. Using Android 15 beta. If you aren't yet testing with talkback, do the following:

  1. Hold down both the volume keys for about 3 seconds until you hear a vibration followed by speech.
  2. Now launch scrcpy.exe shortcut-mod=rctrl
  3. Try pressing alt+right arrow, alt+left arrow, etc.
    Expected behavior: Talkback moves to next or previous item.
    Actual behavior: TalkBack seemingly does nothing. I'm not sure what the program is doing or not doing, however.

@rom1v
Copy link
Collaborator Author

rom1v commented Sep 28, 2024

Please run with -Vverbose to see in the console if Alt+arrow is correctly injected.

Your system (window manager) may capture the shortcut for itself (so scrcpy might not even receive it).

@jackf723
Copy link

Where are the logs stored?

@jackf723
Copy link

I'm getting logs in the console, but I won't be able to see immediately what happens when trying to press alt+right or alt+left without capturing to output. Is there a --verbosity flag that saves the log output so I can post it here?

@rom1v
Copy link
Collaborator Author

rom1v commented Sep 28, 2024

The logs are only output to stdout/stderr. Redirect them to a file using:

scrcpy --shortcut-mod=rctrl -Vverbose > file.txt

@jackf723
Copy link

scrcpy 2.7 https://github.com/Genymobile/scrcpy
INFO: ADB device found:
INFO: --> (usb) adb-3B290DLJG00026-wVp2Mu._adb-tls-connect._tcp. device Pixel_8_Pro
INFO: (tcpip) emulator-5562 offline
DEBUG: Device serial: adb-3B290DLJG00026-wVp2Mu._adb-tls-connect._tcp.
DEBUG: Using server (portable): C:\scrcpy-win64-v2.7-15-g4d9dba2cd\scrcpy-server
C:\scrcpy-win64-v2.7-15-g4d9dba2cd\scrcpy-server: 1 file pushed, 0 skipped. 7.8 MB/s (71432 bytes in 0.009s)
[server] INFO: Device: [Google] google Pixel 8 Pro (Android 15)
DEBUG: Server connected
DEBUG: Starting controller thread
DEBUG: Starting receiver thread
DEBUG: Using icon (portable): C:\scrcpy-win64-v2.7-15-g4d9dba2cd\icon.png
[server] DEBUG: Using audio encoder: 'c2.android.opus.encoder'
[server] DEBUG: Using video encoder: 'c2.exynos.h264.encoder'
INFO: Renderer: direct3d
DEBUG: Trilinear filtering disabled (not an OpenGL renderer)
DEBUG: Demuxer 'video': starting thread
DEBUG: Demuxer 'audio': starting thread
INFO: Texture: 1008x2240
[server] DEBUG: Display: using DisplayManager API
VERBOSE: input: touch [id=mouse] hover-move position=505,1120 pressure=1.000000 action_button=000000 buttons=000000
VERBOSE: [Audio] Inserting initial buffering silence: 240 samples
VERBOSE: [Audio] Inserting initial buffering silence: 240 samples
VERBOSE: [Audio] Inserting initial buffering silence: 240 samples
VERBOSE: [Audio] Inserting initial buffering silence: 240 samples
VERBOSE: [Audio] Inserting initial buffering silence: 240 samples
VERBOSE: [Audio] Inserting initial buffering silence: 240 samples
VERBOSE: [Audio] Inserting initial buffering silence: 240 samples
VERBOSE: [Audio] Inserting initial buffering silence: 240 samples
VERBOSE: [Audio] Inserting initial buffering silence: 240 samples
VERBOSE: [Audio] Inserting initial buffering silence: 240 samples
VERBOSE: [Audio] Buffering: target=2400 avg=2313.600586 cur=2400 compensation=0
DEBUG: [Audio] Buffer underflow, inserting silence: 240 samples
DEBUG: [Audio] Buffer underflow, inserting silence: 240 samples
VERBOSE: [Audio] Buffering: target=2400 avg=2712.000732 cur=2880 compensation=-312
DEBUG: [Audio] Buffer underflow, inserting silence: 21 samples
INFO: Display orientation set to 90
INFO: Display orientation set to 180
VERBOSE: input: touch [id=mouse] hover-move position=502,1119 pressure=1.000000 action_button=000000 buttons=000000
INFO: Display orientation set to 270
INFO: Display orientation set to 180
VERBOSE: [Audio] Buffering: target=2400 avg=2549.519775 cur=2325 compensation=0
VERBOSE: input: touch [id=mouse] hover-move position=501,1117 pressure=1.000000 action_button=000000 buttons=000000
VERBOSE: [Audio] Buffering: target=2400 avg=2566.885986 cur=2805 compensation=0
VERBOSE: [Audio] Buffering: target=2400 avg=2657.188232 cur=2805 compensation=-257
INFO: Display orientation set to 270
INFO: Display orientation set to 180
VERBOSE: input: touch [id=mouse] hover-move position=500,1114 pressure=1.000000 action_button=000000 buttons=000000
VERBOSE: [Audio] Buffering: target=2400 avg=2602.155762 cur=2260 compensation=0
VERBOSE: [Audio] Buffering: target=2400 avg=2590.564453 cur=2740 compensation=0
VERBOSE: [Audio] Buffering: target=2400 avg=2607.770508 cur=2740 compensation=-207
INFO: Display orientation set to 270
INFO: Display orientation set to 0
VERBOSE: input: touch [id=mouse] hover-move position=510,1129 pressure=1.000000 action_button=000000 buttons=000000
VERBOSE: [Audio] Buffering: target=2400 avg=2563.707275 cur=2207 compensation=0
INFO: Display orientation set to 90
INFO: Display orientation set to 180
VERBOSE: input: touch [id=mouse] hover-move position=497,1108 pressure=1.000000 action_button=000000 buttons=000000
VERBOSE: [Audio] Buffering: target=2400 avg=2534.379395 cur=2207 compensation=0
INFO: Display orientation set to 270
INFO: Display orientation set to 180
VERBOSE: input: touch [id=mouse] hover-move position=496,1106 pressure=1.000000 action_button=000000 buttons=000000
INFO: Display orientation set to 90
INFO: Display orientation set to 0
VERBOSE: input: touch [id=mouse] hover-move position=513,1137 pressure=1.000000 action_button=000000 buttons=000000
INFO: Display orientation set to 90
INFO: Display orientation set to 180
VERBOSE: input: touch [id=mouse] hover-move position=495,1101 pressure=1.000000 action_button=000000 buttons=000000
VERBOSE: [Audio] Buffering: target=2400 avg=2490.909668 cur=2207 compensation=0
INFO: Display orientation set to 270
INFO: Display orientation set to 0
VERBOSE: input: touch [id=mouse] hover-move position=513,1139 pressure=1.000000 action_button=000000 buttons=000000
VERBOSE: input: key down code=22 repeat=0 meta=000000
VERBOSE: input: key up code=22 repeat=0 meta=000000
VERBOSE: [Audio] Buffering: target=2400 avg=2474.430908 cur=2207 compensation=0
VERBOSE: input: key down code=21 repeat=0 meta=000000
VERBOSE: input: key up code=21 repeat=0 meta=000000
INFO: Display orientation set to 90
INFO: Display orientation set to 180
VERBOSE: input: touch [id=mouse] hover-move position=495,1101 pressure=1.000000 action_button=000000 buttons=000000
VERBOSE: [Audio] Buffering: target=2400 avg=2494.224365 cur=2207 compensation=0
DEBUG: [Audio] Buffer underflow, inserting silence: 193 samples
VERBOSE: input: key down code=21 repeat=0 meta=000000
VERBOSE: [Audio] Buffering: target=2400 avg=2595.147705 cur=2880 compensation=-195
VERBOSE: input: key up code=21 repeat=0 meta=000000
DEBUG: User requested to quit
DEBUG: quit...
DEBUG: Controller stopped
DEBUG: Demuxer 'video': end of frames
DEBUG: Demuxer 'audio': end of frames
DEBUG: Receiver stopped
[server] DEBUG: Controller stopped
[server] DEBUG: Device message sender stopped
[server] DEBUG: Audio encoder stopped
DEBUG: Server disconnected
DEBUG: Server terminated

@jackf723
Copy link

The screen only moves when pressing the right or left arrow key, however that isn't generally what Talkback expects from a keyboard user. It expects the actual alt+left, alt+right, alt+enter, etc. to perform the keyboard commands. For reference, the Talkback Keyboard Command reference will give you a full breakdown of the default keymap, and the keys TalkBack anticipates will be used.

@rom1v
Copy link
Collaborator Author

rom1v commented Sep 28, 2024

INFO: Display orientation set to 90
INFO: Display orientation set to 180

The screen only moves when pressing the right or left arrow key

The screen is rotated on Alt+left/Alt+right? It is not expected if the shortcut modifier is not Alt 🤔

I cannot reproduce if I pass --shortcut-mod=rctrl.

@jackf723
Copy link

I should clarify. The screen is not rotating left or right, but the focus on a particular screen element, rather. The way Talkback works is you press alt+left or right arrow to move from one item to the other, alt+enter to select that item, etc. If you need a live demonstration via Zoom screenshare, please let me know here.

@rom1v
Copy link
Collaborator Author

rom1v commented Sep 28, 2024

The screen is not rotating left or right

But the logs say that the screen is rotating:

INFO: Display orientation set to 90
INFO: Display orientation set to 180

If you need a live demonstration via Zoom screenshare, please let me know here.

If you can just capture a video on your computer, to see what happens when you press Alt+left and Alt+right (and what should happen, if you can, I have never used tallback to be honest).

@jackf723
Copy link

jackf723 commented Sep 28, 2024

Will keycode events be logged in hid mode? If so I will do a side-by-side comparison between mirror mode and hid mode.

@jackf723
Copy link

jackf723 commented Oct 5, 2024

I can confirm that --shortcut-mod=rctrl is properly making the shortcut right control. However, what is causing alt+any other key to not register?

@rom1v
Copy link
Collaborator Author

rom1v commented Oct 6, 2024

However, what is causing alt+any other key to not register?

On a Pixel 8, if I press Super from the home screen, it opens "Search your phone and more". I type some random text, then press Alt+backspace, it clears the text field. So Alt and Super are correctly forwarded.

@jackf723
Copy link

jackf723 commented Oct 6, 2024

Well, seeing as I'm on windows - if I press the windows key (super key) it opens the start menu. I'm typing the following command to start scrcpy:
.\scrcpy.exe -d --shortcut-mod=rctrl --mouse=disabled
alt+backspace however does work. Alt+any of the arrow keys, alt+enter, etc. do not work, however.

@rom1v
Copy link
Collaborator Author

rom1v commented Oct 6, 2024

alt+backspace however does work.

👍

Alt+any of the arrow keys, alt+enter, etc. do not work, however.

OK, they're probably captured by Windows without sending them to scrcpy. Check the events with -Vverbose.

@jackf723
Copy link

jackf723 commented Oct 6, 2024

logging.txt
In this, I press alt+arrow keys to no avail, but alt+backspace does work. The results of that are here in the log.

@rom1v
Copy link
Collaborator Author

rom1v commented Oct 6, 2024

Your logging.txt is in UTF-16 for some reason. To convert (and print to stdout):

iconv -f utf-16 logging.txt

Here:

VERBOSE: input: key down code=57 repeat=0 meta=000012
VERBOSE: input: key down code=22 repeat=0 meta=000012
VERBOSE: input: key up   code=22 repeat=0 meta=000012
VERBOSE: input: key down code=21 repeat=0 meta=000012
VERBOSE: input: key up   code=21 repeat=0 meta=000012
VERBOSE: input: key down code=20 repeat=0 meta=000012
VERBOSE: input: key up   code=20 repeat=0 meta=000012
VERBOSE: input: key down code=19 repeat=0 meta=000012
VERBOSE: input: key up   code=19 repeat=0 meta=000012
VERBOSE: input: key down code=22 repeat=0 meta=000012
VERBOSE: input: key up   code=22 repeat=0 meta=000012
VERBOSE: input: key down code=21 repeat=0 meta=000012
VERBOSE: input: key up   code=21 repeat=0 meta=000012
VERBOSE: input: key down code=22 repeat=0 meta=000012
VERBOSE: input: key up   code=22 repeat=0 meta=000012
VERBOSE: input: key down code=21 repeat=0 meta=000012
VERBOSE: input: key up   code=21 repeat=0 meta=000012
VERBOSE: input: key up   code=57 repeat=0 meta=000000
  • 57 is Alt
  • 19, 20, 21, 22 are , , ,

So it correctly injects Alt+→←↓↑→←→←.

Android might not react to such events, but they are correctly injected.

@jackf723
Copy link

jackf723 commented Oct 6, 2024

It looks like the screen rotating problem from last time is gone as well. So, is SCRCPY injecting alt+right arrow, alt+left arrow, etc. as a combined event, I.E. the equivalent of pressing and holding alt, pressing right arrow, and releasing them both? Or is it firing off alt and the arrow keys as separate events. If the ladder, why for the arrow keys and not for alt+backspace? Is there an sdk limitation that prevents these keys from firing the way they're suppose to?
In order for talkback to work, alt+arrows, alt+enter, etc. have to be fired as single events just as alt+backspace appears to be.

@rom1v
Copy link
Collaborator Author

rom1v commented Oct 6, 2024

It works exactly the same way for Alt+backspace or Alt+arrows: it inject Alt pressed, then arrow pressed, then arrow released, then Alt released (it injects exactly the key events when you press/release, check your logs).

@jackf723
Copy link

jackf723 commented Oct 6, 2024

I'm not seeing anything that could indicate there not functioning with TalkBack, though. For reference, alt+right arrow is supposed to move the the next element on the screen, alt+left arrow is previous, alt+enter clicks on the selected icon. When a regular keyboard is connected, or when SCRCPY is in hid mode, this works, however hid mode prevents the device's audio/video from being transmitted. Any thoughts? Is it possible that SCRCPY's shortcuts can/should instead be user-customizable to send the appropriate keyboard events to the phone? I.E. alt+enter in scrcpy sends an alt+enter keyboard event to the phone, similar to how TeamViewer and other programs have a send ctrl+alt+del menu item that sends that keyboard event to the remote machine, bypassing the controlling machine's interception of that command?

@rom1v
Copy link
Collaborator Author

rom1v commented Oct 6, 2024

I'm not seeing anything that could indicate there not functioning with TalkBack, though.

Probably because talkback is listening at a lower level (uinput?), so it cannot capture events injected via the SDK). However, a normal Android app should receive these events.

@rom1v
Copy link
Collaborator Author

rom1v commented Oct 6, 2024

however hid mode prevents the device's audio/video from being transmitted.

Not in UHID mode. Doesn't it work in UHID mode?

@jackf723
Copy link

jackf723 commented Oct 6, 2024

Oh, is audio passthrough supposed to work in uhid mode?

@jackf723
Copy link

jackf723 commented Oct 6, 2024

Ah, that does work. Had to manually assign --keyboard-mode=uhid, and also disable the lalt shortcut. Let's see if it works with -K normally now.

@jackf723
Copy link

jackf723 commented Oct 6, 2024

Ok, looks like this is all fixed. Thanks!
It may be good to add a command-line switch that automatically sets the options most preferred for TalkBack, -a11y or -talkback would be good. The accessibility switch should set the following:
-d because over-the-wire is the way to go, later versions of Android make ADB over network not worth the headache
-K for setting the keyboard to uhid, rather than sdk injection
--mouse=disabled to allow for exclusive keyboard control
--shortcut-mod=rctrl
Thanks again.

Alt and Super (also named Meta) modifier keys are captured for shortcuts
by default (cf --shortcut-mod).

However, when shortcut modifiers are changed, Alt and Super should be
forwarded to the device. This is the case for AOA and UHID keyboards,
but it was not the case for SDK keyboard.

Fixes #5318 <#5318>
PR #5322 <#5322>
Masking was unnecessary (im->sdl_shortcut_mods is implicitly masked).

PR #5322 <#5322>
Factorize mouse capture for relative mouse mode to reduce code
duplication between normal and OTG modes.

PR #5322 <#5322>
This will allow to reuse it for mouse capture keys, which are handled by
a component separate from the input manager.

PR #5322 <#5322>
Instead of using separate hardcoded keys for mouse capture/uncapture,
use the shortcut mods.

By changing the shortcut mods (for example --shortcut-mod=rctrl), it
allows to forward Alt and Super to the device.

Fixes #5318 <#5318>
PR #5322 <#5322>
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 this pull request may close these issues.

2 participants