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

Inject UTF-8 text #1426

Closed
wants to merge 3 commits into from
Closed

Inject UTF-8 text #1426

wants to merge 3 commits into from

Conversation

rom1v
Copy link
Collaborator

@rom1v rom1v commented May 25, 2020

Since Android 7, we could use the PASTE keycode to inject UTF-8 text, instead of injecting every ASCII char sequentially. (ref: #786 (comment))

So I just implemented it.

If the device runs Android >= 7:

  • backup the clipboard
  • set the text to inject into the clipboard
  • inject the PASTE key code
  • restore the clipboard

It is very fast and allows to inject UTF-8 text, which is awesome for non-ASCII languages like Chinese.

Please test and report any problem.

Thank you for your feedbacks.


Here are binaries so that you can test even if you don't know how to build:

  • scrcpy.exe
    SHA256: 4481d854630aaba9f31e4f3a19b46c4843c1918da03e1a486089bae3a3ebb09a
  • scrcpy-server
    SHA256: 64cd4d4277040430c48d35677ad8472a6315337651fb9cfc5f62cf5abf2021b7

(on Windows, replace both files in the v1.13 release; on other platforms, use scrcpy-server and follow §Prebuilt server)

rom1v added 3 commits May 25, 2020 03:28
Only the main injection method was exposed on Device, the convenience
methods were implemented in Controller.

For consistency, move them all to the Device class.
Expose methods to inject key events and key codes with an additional
parameter to specify the "mode" (async, wait for finish, wait for
result).
On Android >= 7, inject text using the clipboard.

This is faster and allows to inject UTF-8.
@AndroidDeveloperLB
Copy link

Where can I download it?

@rom1v
Copy link
Collaborator Author

rom1v commented May 25, 2020

I updated the post to include links to binaries.

@AndroidDeveloperLB
Copy link

Doesn't work well.
Each time I write a Hebrew letter, it also writes the English one .

Example:
I write: "ש"
And I get: "שa"

Not only that, but for some reason it doesn't work on all places. In the create-contact screen, it didn't work for some of the EditTexts...

And for some reason I can't run it without the console using "scrcpy-noconsole.exe" .

See here:
2020-05-25_10-52-45.zip

@rom1v
Copy link
Collaborator Author

rom1v commented May 25, 2020

Each time I write a Hebrew letter, it also writes the English one .

Example:
I write: "ש"
And I get: "שa"

What behavior do you get on v1.13?

What if you launch it with scrcpy --prefer-text?

Not only that, but for some reason it doesn't work on all places. In the create-contact screen, it didn't work for some of the EditTexts...

Oh, that's bad :(

And for some reason I can't run it without the console using "scrcpy-noconsole.exe" .

This is not a release, just a quick build so that you can test, I didn't post a new build of scrcpy-noconsole.exe.

@AndroidDeveloperLB
Copy link

By "v1.13", you mean the official one here:
https://github.com/Genymobile/scrcpy/releases
?
If so, instead of Hebrew it writes in English. So for the above example, when I type "ש" it puts "a" .

Using scrcpy --prefer-text seems to handle the English writing, but I've noticed an issue that is caused by the method itself of copying-to-clipboard:
It puts the texts that are copied into the clipboard history of Gboard ...

I still don't get how come the EditText issue exists. I can paste there...

@rom1v
Copy link
Collaborator Author

rom1v commented May 25, 2020

Not only that, but for some reason it doesn't work on all places. In the create-contact screen, it didn't work for some of the EditTexts...

It puts the texts that are copied into the clipboard history of Gboard ...

OK, thank you for your feedbacks. This method could not be used when typing text then.

However, I could probably use it to paste the text immediately on Ctrl+Shift+v (if Android >= 7), in addition to clipboard synchronization.

@AndroidDeveloperLB
Copy link

Can you please explain how could the EditText issue exist ? Pasting is allowed there...

Is it possible perhaps to create a (secured) app that uses root, to type characters via ScrCpy?
One that will allow only ScrCpy to type characters?
Or is even if it's possible, it could be very dangerous, as other apps might exploit it ?

@rom1v
Copy link
Collaborator Author

rom1v commented May 25, 2020

Can you please explain how could the EditText issue exist ? Pasting is allowed there...

I don't know.

Is it possible perhaps to create a (secured) app that uses root, to type characters via ScrCpy?
One that will allow only ScrCpy to type characters?
Or is even if it's possible, it could be very dangerous, as other apps might exploit it ?

Scrcpy should work without root. And indeed it looks dangerous.

@AndroidDeveloperLB
Copy link

I'm talking only about the non-English typing. I don't think there is any other solution, sadly.

Someone proposed a keyboard, but that's changing the way we use the device and it doesn't seem like a stable solution.

I already requested Google to add unicode support:
https://issuetracker.google.com/issues/143375921

It doesn't even work for emulator:
https://issuetracker.google.com/issues/153619173

But I don't think it's going to be added anytime soon.

@quyleanh
Copy link
Contributor

quyleanh commented Sep 3, 2020

@rom1v so could you investigate the work around based on their implementation?
As your comment, I think people cannot trust mirroid.io at all. Thank you for comment anyway.

@AndroidDeveloperLB
Copy link

@quyleanh Do you know perhaps of a different alternative to it, that does offer unicode?

@quyleanh
Copy link
Contributor

quyleanh commented Sep 3, 2020

@AndroidDeveloperLB no, I don't. I have tested QtScrcpy but it doesn't support unicode. (of course, it's based on scrcpy). Hope @rom1v can find out the way.

@AndroidDeveloperLB
Copy link

@rom1v I hope you sue them for violating.
But I also want to know how to avoid such an issue myself. I thought apache is quite free-to-use.

@vip-liutao
Copy link

vip-liutao commented Oct 21, 2020

接时替换键盘应用程

mirroid真强大,是我需要的
一样不延迟并且解决了输入法问题,这个在scrcpy困扰了我很久
这两款软件的音频都可以采用
蓝牙连接电脑
win10应用商店下载
Bluetooth audio receiver
使用

@gaussandhisgun
Copy link

can you add built linux binaries of this in here? my distro seems to have problems with meson as it provides only 0.45 and scrcpy needs 0.48 to be built

@thedmd
Copy link

thedmd commented Feb 18, 2021

Version from top post works for me like a charm. Polish characters are handled well. Thank you!

@heweisheng
Copy link

if check the message is utf8 char use paste?or not use ascii inject。maybe is a idea but i need check all char

@AlexBurdu
Copy link
Contributor

AlexBurdu commented Nov 10, 2021

I adapted this a bit in my fork and works great for my needs.
Thanks!

@quyleanh
Copy link
Contributor

@AlexBurdu could you add some brief info?

@AlexBurdu
Copy link
Contributor

AlexBurdu commented Nov 10, 2021

So the implementation that you can see in my fork, which is a slightly modified version of the one in this PR works fine on my Pixel phone. Unfortunately it does not work well on my Samsung Galaxy S20 FE.
I believe Samsung may have customized the way clipboard works on their rom, as when I'm sending characters through clipboard I see them in Gboard keyboard clipboard history, but I don't see them in OS clipboard or Samsung Keyboard's clipboard.

I initially started poking at this to add Romanian diacritics support, but it seems that support for them is missing in the Android OS KeyCharacterMap class, and no events are returned for KeyCharacterMap.getEvents(chars)
For example, ACCENT_COMMA_BELOW is commented out in the source file. While ACCENT_BREVE is there, KeyCharacterMap.getEvents() still returns null for breve('A') / breve('a'). I didn't dig further to see why that happens, I'm assuming that some mapping for it is also missing in this function that is meant to be used for tests.

@AlexBurdu
Copy link
Contributor

AlexBurdu commented Nov 20, 2021

@rom1v would it be possible for scrcpy server to register an IME that would take care of text input, or is not possible?
Is scrcpy-server seen by the OS as a regular application or like "something else" (what else)?

Note: I have read DEVELOP.md, but it's still not clear to me and I'd like to contribute to this.

@rom1v
Copy link
Collaborator Author

rom1v commented Nov 20, 2021

Is scrcpy-server seen by the OS as a regular application or like "something else" (what else)?

No, it's not an Android app, just a Java executable accessing the Android APIs.

More details: https://blog.rom1v.com/2018/03/introducing-scrcpy/

About IME, also see #1751.

@AlexBurdu
Copy link
Contributor

AlexBurdu commented Nov 21, 2021

Thanks, the hack in #1751 is what I needed as a fallback to key inject!
It's a bit laggy sometimes, but not that annoying.

@quyleanh
Copy link
Contributor

@AlexBurdu could you please give me the Windows build for your latest implementation? I would like to test with my inject text scenario.
Thank you.

@domnpp
Copy link

domnpp commented Nov 22, 2021

@quyleanh Here.
@AlexBurdu You did the hard work, I managed to build it for Windows and therefore I may as well share it with anyone else that's interested.
https://github.com/domenn/scrcpy/releases/tag/v1.19

@AlexBurdu
Copy link
Contributor

AlexBurdu commented Nov 22, 2021

Thanks, I just adapted the work others have done in the linked PRs.
Please note that you need to install ADBKeyBoard and select it as your keyboard while you use scrcpy for it to work:

Also, i have a few changes done to the mouse buttons bindings on my fork.

@AlexBurdu
Copy link
Contributor

AlexBurdu commented Nov 22, 2021

For Linux/Mac users, I use this trick to get the ADBKeyBoard set when I start scrcpy and then switch back to my previous one (GBoard) when I exit it. Should be easy to adapt it for Windows also.

export personal_phone=192.168.0.94:8888 && \
adb connect $personal_phone && \
adb -s $personal_phone shell ime set com.android.adbkeyboard/.AdbIME && \
scrcpy \
--turn-screen-off \
--stay-awake \
--window-x 2500 --window-y 0 \
--max-size 1024 \
-s $personal_phone; \
adb -s $personal_phone shell ime set com.google.android.inputmethod.latin/com.android.inputmethod.latin.LatinIME

@gaussandhisgun
Copy link

gaussandhisgun commented Nov 23, 2021

Thanks, I just adapted the work others have done in the linked PRs. Please note that you need to install ADBKeyBoard and select it as your keyboard while you use scrcpy for it to work:

Also, i have a few changes done to the mouse buttons bindings on my fork.

since i for can not submit issues on your fork built by @domenn, i'll put it right here. for whatever reason, on non-english layout, when pressing a russian key, both english and russian chars appear at the same time. this might be my clipboard manager or kde connect freaking out but they seem to copy only russian chars.
image

my devices are: redmi note 9s running pixelplusui on android 11 and a laptop running windows 10 21H1 x64, connection was done with usb, commandline is scrcpy.

upd. seems fine with scrcpy --prefer-text tho.

upd2. i also get a system accent colored screen if i start your build with -f to make it fullscreen by default

@AlexBurdu
Copy link
Contributor

that sounds like a bug for https://github.com/senzhk/ADBKeyBoard, but I'm not sure.
I'm no longer using clipboard for this, but sending the chars through adb shell to ADBKeyBoard.

@quyleanh
Copy link
Contributor

quyleanh commented Nov 24, 2021

@domenn @AlexBurdu thank you very much. I have tried and it work well with my case.
Input Vietnamese text need to be careful to not mess with clipboard.

How can I disable inject with clipboard? I would like to use only with ADBKeyBoard.

@AlexBurdu
Copy link
Contributor

AlexBurdu commented Nov 24, 2021

We have diverged a lot from the initial PR discussion (sorry for that). in my fork, I don't have any code left for injecting through clipboard.

@yilksd
Copy link

yilksd commented Jan 5, 2022

that sounds like a bug for https://github.com/senzhk/ADBKeyBoard, but I'm not sure. I'm no longer using clipboard for this, but sending the chars through adb shell to ADBKeyBoard.

How to use your fork? I have just compiled your fork and launched it but I still cannot type Chinese character. I have the ADB keyboard app installed.

@quyleanh
Copy link
Contributor

quyleanh commented Jul 5, 2022

I have compiled the Windows builds with @AlexBurdu 's ADBKeyBoard merged.
Feel free to use in this release.
https://github.com/quyleanh/scrcpy/releases/tag/v1.24

@electriquo
Copy link

see #37 (comment) for probable solution

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.