diff --git a/java/res/xml-sw600dp/row_symbols4.xml b/java/res/xml-sw600dp/row_symbols4.xml
index 73a5b170364058ec0a0557d36cc974b628869106..f138d8ef49c05caa3127cff967f085b252539225 100644
--- a/java/res/xml-sw600dp/row_symbols4.xml
+++ b/java/res/xml-sw600dp/row_symbols4.xml
@@ -41,6 +41,8 @@
             latin:moreKeys="!text/more_keys_for_tablet_double_quote" />
         <Key
             latin:keyLabel="_" />
-        <!-- Here is empty space. -->
+        <!-- Note: This Spacer prevents the above key from being marked as a right edge key. -->
+        <Spacer
+            latin:keyWidth="fillRight" />
     </Row>
 </merge>
diff --git a/java/res/xml-sw600dp/row_symbols_shift4.xml b/java/res/xml-sw600dp/row_symbols_shift4.xml
index 6f3aac7c6a1f42779e07ca21b8e692fb239b8ab9..29befa92a5f152f339f7428bd90f78ebe03bcba9 100644
--- a/java/res/xml-sw600dp/row_symbols_shift4.xml
+++ b/java/res/xml-sw600dp/row_symbols_shift4.xml
@@ -33,6 +33,8 @@
             latin:keyXPos="28.0%p"
             latin:keyboardLayout="@xml/key_space"
             latin:backgroundType="normal" />
-        <!-- Here is empty space. -->
+        <!-- Note: This Spacer prevents the above key from being marked as a right edge key. -->
+        <Spacer
+            latin:keyWidth="fillRight" />
     </Row>
 </merge>
diff --git a/java/res/xml-sw600dp/rows_number_normal.xml b/java/res/xml-sw600dp/rows_number_normal.xml
index 48b3040894808e6215cf4d16e9241983808ef570..f692394566f19a563e95f075e31a566401ba74d3 100644
--- a/java/res/xml-sw600dp/rows_number_normal.xml
+++ b/java/res/xml-sw600dp/rows_number_normal.xml
@@ -153,5 +153,8 @@
         <Key
             latin:keyLabel="#"
             latin:keyStyle="numKeyStyle" />
+        <!-- Note: This Spacer prevents the above key from being marked as a right edge key. -->
+        <Spacer
+            latin:keyWidth="fillRight" />
     </Row>
 </merge>
diff --git a/java/res/xml-sw768dp/row_dvorak4.xml b/java/res/xml-sw768dp/row_dvorak4.xml
index 0827815c9dc1fa326161aba7f4611271c9179fcf..8f9230d4a9e9c6067ff00e8eb235867b40925ed0 100644
--- a/java/res/xml-sw768dp/row_dvorak4.xml
+++ b/java/res/xml-sw768dp/row_dvorak4.xml
@@ -25,8 +25,10 @@
         latin:keyWidth="8.047%p"
         latin:backgroundType="functional"
     >
+        <!-- Note: This Spacer prevents the below key from being marked as a left edge key. -->
+        <Spacer
+            latin:keyWidth="5.782%p" />
         <include
-            latin:keyXPos="5.782%p"
             latin:keyboardLayout="@xml/key_settings" />
         <include
             latin:keyboardLayout="@xml/key_shortcut" />
@@ -42,5 +44,8 @@
             latin:keyboardLayout="@xml/key_dash" />
         <include
             latin:keyboardLayout="@xml/key_f2" />
+        <!-- Note: This Spacer prevents the above key from being marked as a right edge key. -->
+        <Spacer
+            latin:keyWidth="fillRight" />
     </Row>
 </merge>
diff --git a/java/res/xml-sw768dp/row_hebrew4.xml b/java/res/xml-sw768dp/row_hebrew4.xml
index 180c5641d11ed5a3c1b72f1f0f93d00b6e59c16a..ae14f029662b1b21126bf4dd5b84e19f459bd5a9 100644
--- a/java/res/xml-sw768dp/row_hebrew4.xml
+++ b/java/res/xml-sw768dp/row_hebrew4.xml
@@ -25,8 +25,10 @@
         latin:keyWidth="8.047%p"
         latin:backgroundType="functional"
     >
+        <!-- Note: This Spacer prevents the below key from being marked as a left edge key. -->
+        <Spacer
+            latin:keyWidth="5.782%p" />
         <include
-            latin:keyXPos="5.782%p"
             latin:keyboardLayout="@xml/key_settings" />
         <include
             latin:keyboardLayout="@xml/key_shortcut" />
@@ -40,5 +42,8 @@
             latin:keyboardLayout="@xml/keys_comma_period" />
         <include
             latin:keyboardLayout="@xml/key_f2" />
+        <!-- Note: This Spacer prevents the above key from being marked as a right edge key. -->
+        <Spacer
+            latin:keyWidth="fillRight" />
     </Row>
 </merge>
diff --git a/java/res/xml-sw768dp/row_qwerty4.xml b/java/res/xml-sw768dp/row_qwerty4.xml
index 92411f54e40b768743247c73f08ea600d9c373c1..f1f4214ca17d8e4159a872e9ab883b57bdb80be1 100644
--- a/java/res/xml-sw768dp/row_qwerty4.xml
+++ b/java/res/xml-sw768dp/row_qwerty4.xml
@@ -25,8 +25,10 @@
         latin:keyWidth="8.047%p"
         latin:backgroundType="functional"
     >
+        <!-- Note: This Spacer prevents the below key from being marked as a left edge key. -->
+        <Spacer
+            latin:keyWidth="5.782%p" />
         <include
-            latin:keyXPos="5.782%p"
             latin:keyboardLayout="@xml/key_settings" />
         <include
             latin:keyboardLayout="@xml/key_shortcut" />
@@ -42,5 +44,8 @@
             latin:keyboardLayout="@xml/key_dash" />
         <include
             latin:keyboardLayout="@xml/key_f2" />
+        <!-- Note: This Spacer prevents the above key from being marked as a right edge key. -->
+        <Spacer
+            latin:keyWidth="fillRight" />
     </Row>
 </merge>
diff --git a/java/res/xml-sw768dp/row_symbols4.xml b/java/res/xml-sw768dp/row_symbols4.xml
index 4e1c1199457215dd61c227e6bd44976326dddad1..b801a12a7ba50fff7f7ad41c8e6f3a04755438ab 100644
--- a/java/res/xml-sw768dp/row_symbols4.xml
+++ b/java/res/xml-sw768dp/row_symbols4.xml
@@ -25,8 +25,10 @@
         latin:keyWidth="8.047%p"
         latin:backgroundType="functional"
     >
+        <!-- Note: This Spacer prevents the below key from being marked as a left edge key. -->
+        <Spacer
+            latin:keyWidth="13.829%p" />
         <Key
-            latin:keyXPos="13.829%p"
             latin:keyLabel="/" />
         <include
             latin:keyboardLayout="@xml/key_f1" />
@@ -39,6 +41,8 @@
             latin:moreKeys="!text/more_keys_for_tablet_double_quote" />
         <Key
             latin:keyLabel="_" />
-        <!-- Here is empty space. -->
+        <!-- Note: This Spacer prevents the above key from being marked as a right edge key. -->
+        <Spacer
+            latin:keyWidth="fillRight" />
     </Row>
 </merge>
diff --git a/java/res/xml-sw768dp/row_symbols_shift4.xml b/java/res/xml-sw768dp/row_symbols_shift4.xml
index 561351cabecf4e3ddf4fdc1304965718c76acc7f..f71864bc301f60b20b7dba461b2284a0ee2a57f3 100644
--- a/java/res/xml-sw768dp/row_symbols_shift4.xml
+++ b/java/res/xml-sw768dp/row_symbols_shift4.xml
@@ -25,11 +25,14 @@
         latin:keyWidth="8.047%p"
         latin:backgroundType="functional"
     >
-        <!-- Here is empty space. -->
+        <!-- Note: This Spacer prevents the below key from being marked as a left edge key. -->
+        <Spacer
+            latin:keyWidth="29.923%p" />
         <include
-            latin:keyXPos="29.923%p"
             latin:keyboardLayout="@xml/key_space"
             latin:backgroundType="normal" />
-        <!-- Here is empty space. -->
+        <!-- Note: This Spacer prevents the above key from being marked as a right edge key. -->
+        <Spacer
+            latin:keyWidth="fillRight" />
     </Row>
 </merge>
diff --git a/java/res/xml-sw768dp/rows_number_normal.xml b/java/res/xml-sw768dp/rows_number_normal.xml
index 84910a88fdbdf3b393af5b5e641bb6218b49b0f4..d4d7c722af2060ec26025dfa4cc61c552c949e04 100644
--- a/java/res/xml-sw768dp/rows_number_normal.xml
+++ b/java/res/xml-sw768dp/rows_number_normal.xml
@@ -168,5 +168,8 @@
         <Key
             latin:keyLabel="#"
             latin:keyStyle="numKeyStyle" />
+        <!-- Note: This Spacer prevents the above key from being marked as a right edge key. -->
+        <Spacer
+            latin:keyWidth="fillRight" />
     </Row>
 </merge>
diff --git a/java/src/com/android/inputmethod/keyboard/KeyDetector.java b/java/src/com/android/inputmethod/keyboard/KeyDetector.java
index 868c8cab56843f17c53a49c9d4a25522bdf86f11..f5686dcdaba949aa7091f7e377e509b78e5c5674 100644
--- a/java/src/com/android/inputmethod/keyboard/KeyDetector.java
+++ b/java/src/com/android/inputmethod/keyboard/KeyDetector.java
@@ -83,11 +83,17 @@ public class KeyDetector {
         int minDistance = Integer.MAX_VALUE;
         Key primaryKey = null;
         for (final Key key: mKeyboard.getNearestKeys(touchX, touchY)) {
-            final boolean isOnKey = key.isOnKey(touchX, touchY);
+            // An edge key always has its enlarged hitbox to respond to an event that occurred in
+            // the empty area around the key. (@see Key#markAsLeftEdge(KeyboardParams)} etc.)
+            if (!key.isOnKey(touchX, touchY)) {
+                continue;
+            }
             final int distance = key.squaredDistanceToEdge(touchX, touchY);
+            if (distance > minDistance) {
+                continue;
+            }
             // To take care of hitbox overlaps, we compare mCode here too.
-            if (primaryKey == null || distance < minDistance
-                    || (distance == minDistance && isOnKey && key.mCode > primaryKey.mCode)) {
+            if (primaryKey == null || distance < minDistance || key.mCode > primaryKey.mCode) {
                 minDistance = distance;
                 primaryKey = key;
             }