Commit 891674b5 authored by Erik Jensen's avatar Erik Jensen Committed by Commit Bot

remoting: Inject text events as individual graphemes on Mac

CGEventKeyboardSetUnicodeString seems only to handle up to 20 code
units. In addition, this allows us to inject newlines correctly.

Change-Id: If7d78f1a66416d5e3c435bd856f47171c0b20c66
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2264946Reviewed-by: default avatarJamie Walch <jamiewalch@chromium.org>
Commit-Queue: Erik Jensen <rkjnsn@chromium.org>
Cr-Commit-Position: refs/heads/master@{#782148}
parent 96e7ad75
...@@ -14,6 +14,7 @@ ...@@ -14,6 +14,7 @@
#include "base/bind.h" #include "base/bind.h"
#include "base/compiler_specific.h" #include "base/compiler_specific.h"
#include "base/i18n/break_iterator.h"
#include "base/location.h" #include "base/location.h"
#include "base/logging.h" #include "base/logging.h"
#include "base/mac/scoped_cftyperef.h" #include "base/mac/scoped_cftyperef.h"
...@@ -21,6 +22,7 @@ ...@@ -21,6 +22,7 @@
#include "base/memory/ptr_util.h" #include "base/memory/ptr_util.h"
#include "base/memory/ref_counted.h" #include "base/memory/ref_counted.h"
#include "base/single_thread_task_runner.h" #include "base/single_thread_task_runner.h"
#include "base/strings/string_piece.h"
#include "base/strings/utf_string_conversions.h" #include "base/strings/utf_string_conversions.h"
#include "base/time/time.h" #include "base/time/time.h"
#include "remoting/host/clipboard.h" #include "remoting/host/clipboard.h"
...@@ -294,14 +296,41 @@ void InputInjectorMac::Core::InjectTextEvent(const TextEvent& event) { ...@@ -294,14 +296,41 @@ void InputInjectorMac::Core::InjectTextEvent(const TextEvent& event) {
base::string16 text = base::UTF8ToUTF16(event.text()); base::string16 text = base::UTF8ToUTF16(event.text());
// Applications that ignore UnicodeString field will see the text event as // CGEventKeyboardSetUnicodeString appears to only process up to 20 code
// Space key. // units (and key presses are generally expected to generate a single
ui_thread_task_runner_->PostTask( // character), so split the input text into graphemes.
FROM_HERE, base::BindOnce(CreateAndPostKeyEvent, kVK_Space, base::i18n::BreakIterator grapheme_iterator(
/*pressed=*/true, 0, text)); text, base::i18n::BreakIterator::BREAK_CHARACTER);
ui_thread_task_runner_->PostTask(
FROM_HERE, base::BindOnce(CreateAndPostKeyEvent, kVK_Space, if (!grapheme_iterator.Init()) {
/*pressed=*/false, 0, text)); LOG(ERROR) << "Failed to init grapheme iterator.";
return;
}
while (grapheme_iterator.Advance()) {
base::StringPiece16 grapheme = grapheme_iterator.GetStringPiece();
if (grapheme.length() == 1 && grapheme[0] == '\n') {
// On Mac, the return key sends "\r" rather than "\n", so handle it
// specially.
ui_thread_task_runner_->PostTask(
FROM_HERE, base::BindOnce(CreateAndPostKeyEvent, kVK_Return,
/*pressed=*/true, 0, base::string16()));
ui_thread_task_runner_->PostTask(
FROM_HERE, base::BindOnce(CreateAndPostKeyEvent, kVK_Return,
/*pressed=*/false, 0, base::string16()));
} else {
// Applications that ignore UnicodeString field will see the text event as
// Space key.
ui_thread_task_runner_->PostTask(
FROM_HERE, base::BindOnce(CreateAndPostKeyEvent, kVK_Space,
/*pressed=*/true, 0, grapheme.as_string()));
ui_thread_task_runner_->PostTask(
FROM_HERE,
base::BindOnce(CreateAndPostKeyEvent, kVK_Space,
/*pressed=*/false, 0, grapheme.as_string()));
}
}
} }
void InputInjectorMac::Core::InjectMouseEvent(const MouseEvent& event) { void InputInjectorMac::Core::InjectMouseEvent(const MouseEvent& event) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment