aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorfredizzimo <fsundvik@gmail.com>2020-03-13 20:09:38 +0200
committerGitHub <noreply@github.com>2020-03-13 14:09:38 -0400
commit9e8767917d628afd3dc43759d1d50151c61944a1 (patch)
tree0060b8968d5e9df38c37b375e4eb2d6a0d65ac0c
parentf89439ae09c06d0e85f59af2bc5e020d141f23d6 (diff)
downloadqmk_firmware-9e8767917d628afd3dc43759d1d50151c61944a1.tar.gz
qmk_firmware-9e8767917d628afd3dc43759d1d50151c61944a1.zip
Fix pressing two keys with the same keycode but different modifiers (#2710)
* Fix extra keyboard report during test_fixture teardown * Add tests for pressing two keys with only different modifers * Fix #1708 When two keys that use the same keycode, but different modifiers were pressed at the same time, the second keypress wasn't registered. This is fixed by forcing a key release when we detect a new press for the same keycode. * Fix the NKRO version of is_key_pressed * Fix uninitalized loop variable Co-authored-by: Jack Humbert <jack.humb@gmail.com>
-rw-r--r--tests/basic/keymap.c3
-rw-r--r--tests/basic/test_keypress.cpp118
-rw-r--r--tests/test_common/test_fixture.cpp7
-rw-r--r--tmk_core/common/action.c7
-rw-r--r--tmk_core/common/report.c26
-rw-r--r--tmk_core/common/report.h2
6 files changed, 158 insertions, 5 deletions
diff --git a/tests/basic/keymap.c b/tests/basic/keymap.c
index 02b3cc961..951ce8e0c 100644
--- a/tests/basic/keymap.c
+++ b/tests/basic/keymap.c
@@ -28,7 +28,7 @@ const uint16_t PROGMEM
28 { 28 {
29 // 0 1 2 3 4 5 6 7 8 9 29 // 0 1 2 3 4 5 6 7 8 9
30 {KC_A, KC_B, KC_NO, KC_LSFT, KC_RSFT, KC_LCTL, COMBO1, SFT_T(KC_P), M(0), KC_NO}, 30 {KC_A, KC_B, KC_NO, KC_LSFT, KC_RSFT, KC_LCTL, COMBO1, SFT_T(KC_P), M(0), KC_NO},
31 {KC_NO, KC_NO, KC_NO, KC_NO, KC_NO, KC_NO, KC_NO, KC_NO, KC_NO, KC_NO}, 31 {KC_EQL, KC_PLUS, KC_NO, KC_NO, KC_NO, KC_NO, KC_NO, KC_NO, KC_NO, KC_NO},
32 {KC_NO, KC_NO, KC_NO, KC_NO, KC_NO, KC_NO, KC_NO, KC_NO, KC_NO, KC_NO}, 32 {KC_NO, KC_NO, KC_NO, KC_NO, KC_NO, KC_NO, KC_NO, KC_NO, KC_NO, KC_NO},
33 {KC_C, KC_D, KC_NO, KC_NO, KC_NO, KC_NO, KC_NO, KC_NO, KC_NO, KC_NO}, 33 {KC_C, KC_D, KC_NO, KC_NO, KC_NO, KC_NO, KC_NO, KC_NO, KC_NO, KC_NO},
34 }, 34 },
@@ -43,3 +43,4 @@ const macro_t *action_get_macro(keyrecord_t *record, uint8_t id, uint8_t opt) {
43 } 43 }
44 return MACRO_NONE; 44 return MACRO_NONE;
45}; 45};
46
diff --git a/tests/basic/test_keypress.cpp b/tests/basic/test_keypress.cpp
index e5247911c..551458a0d 100644
--- a/tests/basic/test_keypress.cpp
+++ b/tests/basic/test_keypress.cpp
@@ -18,6 +18,7 @@
18 18
19using testing::_; 19using testing::_;
20using testing::Return; 20using testing::Return;
21using testing::InSequence;
21 22
22class KeyPress : public TestFixture {}; 23class KeyPress : public TestFixture {};
23 24
@@ -121,4 +122,119 @@ TEST_F(KeyPress, RightShiftLeftControlAndCharWithTheSameKey) {
121 EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_RSFT, KC_RCTRL))); 122 EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_RSFT, KC_RCTRL)));
122 EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport())); 123 EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport()));
123 keyboard_task(); 124 keyboard_task();
124} \ No newline at end of file 125}
126
127TEST_F(KeyPress, PressPlusEqualReleaseBeforePress) {
128 TestDriver driver;
129 InSequence s;
130
131 press_key(1, 1); // KC_PLUS
132 EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_LSFT)));
133 EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_LSFT, KC_EQL)));
134 run_one_scan_loop();
135 testing::Mock::VerifyAndClearExpectations(&driver);
136
137 release_key(1, 1); // KC_PLUS
138 EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_LSFT)));
139 EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport()));
140 run_one_scan_loop();
141 testing::Mock::VerifyAndClearExpectations(&driver);
142
143 press_key(0, 1); // KC_EQL
144 EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_EQL)));
145 run_one_scan_loop();
146 testing::Mock::VerifyAndClearExpectations(&driver);
147
148 release_key(0, 1); // KC_EQL
149 EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport()));
150 run_one_scan_loop();
151 testing::Mock::VerifyAndClearExpectations(&driver);
152}
153
154TEST_F(KeyPress, PressPlusEqualDontReleaseBeforePress) {
155 TestDriver driver;
156 InSequence s;
157
158 press_key(1, 1); // KC_PLUS
159 EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_LSFT)));
160 EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_LSFT, KC_EQL)));
161 run_one_scan_loop();
162 testing::Mock::VerifyAndClearExpectations(&driver);
163
164 press_key(0, 1); // KC_EQL
165 EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport()));
166 EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_EQL)));
167 run_one_scan_loop();
168 testing::Mock::VerifyAndClearExpectations(&driver);
169
170 release_key(1, 1); //KC_PLS
171 // BUG: Should really still return KC_EQL, but this is fine too
172 // It's also called twice for some reason
173 EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport())).Times(2);
174 run_one_scan_loop();
175 testing::Mock::VerifyAndClearExpectations(&driver);
176
177 release_key(0, 1); // KC_EQL
178 EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport()));
179 run_one_scan_loop();
180 testing::Mock::VerifyAndClearExpectations(&driver);
181}
182
183TEST_F(KeyPress, PressEqualPlusReleaseBeforePress) {
184 TestDriver driver;
185 InSequence s;
186
187 press_key(0, 1); // KC_EQL
188 EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_EQL)));
189 run_one_scan_loop();
190 testing::Mock::VerifyAndClearExpectations(&driver);
191
192 release_key(0, 1); // KQ_EQL
193 EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport()));
194 run_one_scan_loop();
195 testing::Mock::VerifyAndClearExpectations(&driver);
196
197 press_key(1, 1); // KC_PLUS
198 EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_LSFT)));
199 EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_LSFT, KC_EQL)));
200 run_one_scan_loop();
201 testing::Mock::VerifyAndClearExpectations(&driver);
202
203 release_key(1, 1); // KC_PLUS
204 EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_LSFT)));
205 EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport()));
206 run_one_scan_loop();
207 testing::Mock::VerifyAndClearExpectations(&driver);
208}
209
210TEST_F(KeyPress, PressEqualPlusDontReleaseBeforePress) {
211 TestDriver driver;
212 InSequence s;
213
214 press_key(0, 1); // KC_EQL
215 EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_EQL)));
216 run_one_scan_loop();
217 testing::Mock::VerifyAndClearExpectations(&driver);
218
219 press_key(1, 1); // KC_PLUS
220 // BUG: The sequence is a bit strange, but it works, the end result is that
221 // KC_PLUS is sent
222 EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_LSFT, KC_EQL)));
223 EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_LSFT)));
224 EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_LSFT, KC_EQL)));
225 run_one_scan_loop();
226 testing::Mock::VerifyAndClearExpectations(&driver);
227
228 release_key(0, 1); //KC_EQL
229 // I guess it's fine to still report shift here
230 EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_LSFT)));
231 run_one_scan_loop();
232 testing::Mock::VerifyAndClearExpectations(&driver);
233
234 release_key(1, 1); // KC_PLUS
235 // This report is not needed
236 EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_LSFT)));
237 EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport()));
238 run_one_scan_loop();
239 testing::Mock::VerifyAndClearExpectations(&driver);
240}
diff --git a/tests/test_common/test_fixture.cpp b/tests/test_common/test_fixture.cpp
index 8caf1fca4..20ed838eb 100644
--- a/tests/test_common/test_fixture.cpp
+++ b/tests/test_common/test_fixture.cpp
@@ -32,14 +32,15 @@ TestFixture::TestFixture() {}
32 32
33TestFixture::~TestFixture() { 33TestFixture::~TestFixture() {
34 TestDriver driver; 34 TestDriver driver;
35 layer_clear();
36 clear_all_keys();
37 // Run for a while to make sure all keys are completely released 35 // Run for a while to make sure all keys are completely released
38 EXPECT_CALL(driver, send_keyboard_mock(_)).Times(AnyNumber()); 36 EXPECT_CALL(driver, send_keyboard_mock(_)).Times(AnyNumber());
37 layer_clear();
38 clear_all_keys();
39 idle_for(TAPPING_TERM + 10); 39 idle_for(TAPPING_TERM + 10);
40 testing::Mock::VerifyAndClearExpectations(&driver); 40 testing::Mock::VerifyAndClearExpectations(&driver);
41 // Verify that the matrix really is cleared 41 // Verify that the matrix really is cleared
42 EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport())).Times(Between(0, 1)); 42 EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport())).Times(0);
43 idle_for(TAPPING_TERM + 10);
43} 44}
44 45
45void TestFixture::run_one_scan_loop() { 46void TestFixture::run_one_scan_loop() {
diff --git a/tmk_core/common/action.c b/tmk_core/common/action.c
index 0ec4a43ee..2deafd465 100644
--- a/tmk_core/common/action.c
+++ b/tmk_core/common/action.c
@@ -754,6 +754,13 @@ void register_code(uint8_t code) {
754*/ 754*/
755#endif 755#endif
756 { 756 {
757 // Force a new key press if the key is already pressed
758 // without this, keys with the same keycode, but different
759 // modifiers will be reported incorrectly, see issue #1708
760 if (is_key_pressed(keyboard_report, code)) {
761 del_key(code);
762 send_keyboard_report();
763 }
757 add_key(code); 764 add_key(code);
758 send_keyboard_report(); 765 send_keyboard_report();
759 } 766 }
diff --git a/tmk_core/common/report.c b/tmk_core/common/report.c
index f4758b48e..f4cdca728 100644
--- a/tmk_core/common/report.c
+++ b/tmk_core/common/report.c
@@ -68,6 +68,32 @@ uint8_t get_first_key(report_keyboard_t* keyboard_report) {
68#endif 68#endif
69} 69}
70 70
71/** \brief Checks if a key is pressed in the report
72 *
73 * Returns true if the keyboard_report reports that the key is pressed, otherwise false
74 * Note: The function doesn't support modifers currently, and it returns false for KC_NO
75 */
76bool is_key_pressed(report_keyboard_t* keyboard_report, uint8_t key) {
77 if (key == KC_NO) {
78 return false;
79 }
80#ifdef NKRO_ENABLE
81 if (keyboard_protocol && keymap_config.nkro) {
82 if ((key>>3) < KEYBOARD_REPORT_BITS) {
83 return keyboard_report->nkro.bits[key>>3] & 1<<(key&7);
84 } else {
85 return false;
86 }
87 }
88#endif
89 for (int i=0; i < KEYBOARD_REPORT_KEYS; i++) {
90 if (keyboard_report->keys[i] == key) {
91 return true;
92 }
93 }
94 return false;
95}
96
71/** \brief add key byte 97/** \brief add key byte
72 * 98 *
73 * FIXME: Needs doc 99 * FIXME: Needs doc
diff --git a/tmk_core/common/report.h b/tmk_core/common/report.h
index ecd5da89a..645e01612 100644
--- a/tmk_core/common/report.h
+++ b/tmk_core/common/report.h
@@ -19,6 +19,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
19#define REPORT_H 19#define REPORT_H
20 20
21#include <stdint.h> 21#include <stdint.h>
22#include <stdbool.h>
22#include "keycode.h" 23#include "keycode.h"
23 24
24/* report id */ 25/* report id */
@@ -236,6 +237,7 @@ static inline uint16_t KEYCODE2CONSUMER(uint8_t key) {
236 237
237uint8_t has_anykey(report_keyboard_t* keyboard_report); 238uint8_t has_anykey(report_keyboard_t* keyboard_report);
238uint8_t get_first_key(report_keyboard_t* keyboard_report); 239uint8_t get_first_key(report_keyboard_t* keyboard_report);
240bool is_key_pressed(report_keyboard_t* keyboard_report, uint8_t key);
239 241
240void add_key_byte(report_keyboard_t* keyboard_report, uint8_t code); 242void add_key_byte(report_keyboard_t* keyboard_report, uint8_t code);
241void del_key_byte(report_keyboard_t* keyboard_report, uint8_t code); 243void del_key_byte(report_keyboard_t* keyboard_report, uint8_t code);