Skip to content

Commit d004121

Browse files
dpadlipskyAndroid (Google) Code Review
authored andcommitted
Merge "Skip processing repeat EV_KEY events for keyboards" into main
2 parents 1c375bb + 48fd884 commit d004121

3 files changed

Lines changed: 59 additions & 3 deletions

File tree

services/inputflinger/reader/mapper/KeyboardInputMapper.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -243,11 +243,16 @@ std::list<NotifyArgs> KeyboardInputMapper::process(const RawEvent& rawEvent) {
243243
mHidUsageAccumulator.process(rawEvent);
244244
switch (rawEvent.type) {
245245
case EV_KEY: {
246-
int32_t scanCode = rawEvent.code;
246+
// Skip processing repeated keys (value == 2) since auto repeat is handled by Android
247+
// internally.
248+
if (rawEvent.value == 2) {
249+
break;
250+
}
247251

252+
const int32_t scanCode = rawEvent.code;
248253
if (isSupportedScanCode(scanCode)) {
249-
out += processKey(rawEvent.when, rawEvent.readTime, rawEvent.value != 0,
250-
scanCode, mHidUsageAccumulator.consumeCurrentHidUsage());
254+
out += processKey(rawEvent.when, rawEvent.readTime, rawEvent.value != 0, scanCode,
255+
mHidUsageAccumulator.consumeCurrentHidUsage());
251256
}
252257
break;
253258
}

services/inputflinger/tests/KeyboardInputMapper_test.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,19 @@
2020

2121
#include "InputMapperTest.h"
2222
#include "InterfaceMocks.h"
23+
#include "TestEventMatchers.h"
2324

2425
#define TAG "KeyboardInputMapper_test"
2526

2627
namespace android {
2728

2829
using testing::_;
30+
using testing::AllOf;
2931
using testing::Args;
3032
using testing::DoAll;
3133
using testing::Return;
3234
using testing::SetArgPointee;
35+
using testing::VariantWith;
3336

3437
/**
3538
* Unit tests for KeyboardInputMapper.
@@ -86,4 +89,24 @@ TEST_F(KeyboardInputMapperUnitTest, KeyPressTimestampRecorded) {
8689
}
8790
}
8891

92+
TEST_F(KeyboardInputMapperUnitTest, RepeatEventsDiscarded) {
93+
std::list<NotifyArgs> args;
94+
args += process(ARBITRARY_TIME, EV_KEY, KEY_0, 1);
95+
args += process(ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
96+
97+
args += process(ARBITRARY_TIME, EV_KEY, KEY_0, 2);
98+
args += process(ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
99+
100+
args += process(ARBITRARY_TIME, EV_KEY, KEY_0, 0);
101+
args += process(ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
102+
103+
EXPECT_THAT(args,
104+
ElementsAre(VariantWith<NotifyKeyArgs>(AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN),
105+
WithKeyCode(AKEYCODE_0),
106+
WithScanCode(KEY_0))),
107+
VariantWith<NotifyKeyArgs>(AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP),
108+
WithKeyCode(AKEYCODE_0),
109+
WithScanCode(KEY_0)))));
110+
}
111+
89112
} // namespace android

services/inputflinger/tests/TestEventMatchers.h

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -540,6 +540,34 @@ inline WithKeyCodeMatcher WithKeyCode(int32_t keyCode) {
540540
return WithKeyCodeMatcher(keyCode);
541541
}
542542

543+
/// Scan code
544+
class WithScanCodeMatcher {
545+
public:
546+
using is_gtest_matcher = void;
547+
explicit WithScanCodeMatcher(int32_t scanCode) : mScanCode(scanCode) {}
548+
549+
bool MatchAndExplain(const NotifyKeyArgs& args, std::ostream*) const {
550+
return mScanCode == args.scanCode;
551+
}
552+
553+
bool MatchAndExplain(const KeyEvent& event, std::ostream*) const {
554+
return mScanCode == event.getKeyCode();
555+
}
556+
557+
void DescribeTo(std::ostream* os) const {
558+
*os << "with scan code " << KeyEvent::getLabel(mScanCode);
559+
}
560+
561+
void DescribeNegationTo(std::ostream* os) const { *os << "wrong scan code"; }
562+
563+
private:
564+
const int32_t mScanCode;
565+
};
566+
567+
inline WithScanCodeMatcher WithScanCode(int32_t scanCode) {
568+
return WithScanCodeMatcher(scanCode);
569+
}
570+
543571
/// EventId
544572
class WithEventIdMatcher {
545573
public:

0 commit comments

Comments
 (0)