Skip to content

Commit 48424d3

Browse files
committed
feat(chord_composer): support chording with function keys
1 parent 79a32b2 commit 48424d3

File tree

2 files changed

+42
-27
lines changed

2 files changed

+42
-27
lines changed

include/rime/gear/chord_composer.h

+2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ class ChordComposer : public Processor {
2323
virtual ProcessResult ProcessKeyEvent(const KeyEvent& key_event);
2424

2525
protected:
26+
ProcessResult ProcessChordingKey(const KeyEvent& key_event);
27+
ProcessResult ProcessFunctionKey(const KeyEvent& key_event);
2628
string SerializeChord();
2729
void UpdateChord();
2830
void FinishChord();

src/gear/chord_composer.cc

+40-27
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ static const char* kZeroWidthSpace = "\xef\xbb\xbf"; //"\xe2\x80\x8b";
1818

1919
namespace rime {
2020

21-
2221
ChordComposer::ChordComposer(const Ticket& ticket) : Processor(ticket) {
2322
if (!engine_)
2423
return;
@@ -44,55 +43,49 @@ ChordComposer::~ChordComposer() {
4443
unhandled_key_connection_.disconnect();
4544
}
4645

47-
ProcessResult ChordComposer::ProcessKeyEvent(const KeyEvent& key_event) {
48-
if (pass_thru_)
46+
ProcessResult ChordComposer::ProcessFunctionKey(const KeyEvent& key_event) {
47+
if (key_event.release()) {
4948
return kNoop;
50-
bool chording = !chord_.empty();
51-
if (key_event.shift() || key_event.ctrl() || key_event.alt()) {
52-
ClearChord();
53-
return chording ? kAccepted : kNoop;
5449
}
55-
bool is_key_up = key_event.release();
5650
int ch = key_event.keycode();
57-
Context* ctx = engine_->context();
58-
if (!is_key_up && ch == XK_Return) {
51+
if (ch == XK_Return) {
5952
if (!raw_sequence_.empty()) {
6053
// commit raw input
61-
ctx->set_input(raw_sequence_);
54+
engine_->context()->set_input(raw_sequence_);
6255
// then the sequence should not be used again
6356
raw_sequence_.clear();
6457
}
6558
ClearChord();
66-
return kNoop;
67-
}
68-
if (!is_key_up && ch == XK_BackSpace) {
59+
} else if (ch == XK_BackSpace) {
6960
// invalidate raw sequence
7061
raw_sequence_.clear();
7162
ClearChord();
7263
if (DeleteLastSyllable()) {
7364
return kAccepted;
7465
}
75-
return kNoop;
76-
}
77-
if (!is_key_up && ch == XK_Escape) {
66+
} else if (ch == XK_Escape) {
7867
// clear the raw sequence
7968
raw_sequence_.clear();
8069
ClearChord();
81-
return kNoop;
8270
}
83-
if (!is_key_up && ch >= 0x20 && ch <= 0x7e) {
84-
// save raw input
85-
if (!ctx->IsComposing() || !raw_sequence_.empty()) {
86-
raw_sequence_.push_back(ch);
87-
DLOG(INFO) << "update raw sequence: " << raw_sequence_;
88-
}
71+
return kNoop;
72+
}
73+
74+
ProcessResult ChordComposer::ProcessChordingKey(const KeyEvent& key_event) {
75+
bool chording = !chord_.empty();
76+
if (key_event.shift() || key_event.ctrl() || key_event.alt()) {
77+
ClearChord();
78+
return chording ? kAccepted : kNoop;
8979
}
80+
bool is_key_up = key_event.release();
81+
int ch = key_event.keycode();
82+
// non chording key
9083
if (std::find(chording_keys_.begin(),
9184
chording_keys_.end(),
9285
KeyEvent{ch, 0}) == chording_keys_.end()) {
9386
return chording ? kAccepted : kNoop;
9487
}
95-
// in alphabet
88+
// chording key
9689
if (is_key_up) {
9790
if (pressed_.erase(ch) != 0 && pressed_.empty()) {
9891
FinishChord();
@@ -107,6 +100,26 @@ ProcessResult ChordComposer::ProcessKeyEvent(const KeyEvent& key_event) {
107100
return kAccepted;
108101
}
109102

103+
ProcessResult ChordComposer::ProcessKeyEvent(const KeyEvent& key_event) {
104+
if (pass_thru_) {
105+
return ProcessFunctionKey(key_event);
106+
}
107+
bool is_key_up = key_event.release();
108+
int ch = key_event.keycode();
109+
if (!is_key_up && ch >= 0x20 && ch <= 0x7e) {
110+
// save raw input
111+
if (!engine_->context()->IsComposing() || !raw_sequence_.empty()) {
112+
raw_sequence_.push_back(ch);
113+
DLOG(INFO) << "update raw sequence: " << raw_sequence_;
114+
}
115+
}
116+
auto result = ProcessChordingKey(key_event);
117+
if (result != kNoop) {
118+
return result;
119+
}
120+
return ProcessFunctionKey(key_event);
121+
}
122+
110123
string ChordComposer::SerializeChord() {
111124
KeySequence key_sequence;
112125
for (KeyEvent key : chording_keys_) {
@@ -208,7 +221,7 @@ void ChordComposer::OnContextUpdate(Context* ctx) {
208221
else if (composing_) {
209222
composing_ = false;
210223
raw_sequence_.clear();
211-
DLOG(INFO) << "clear sequence.";
224+
DLOG(INFO) << "clear raw sequence.";
212225
}
213226
}
214227

@@ -219,7 +232,7 @@ void ChordComposer::OnUnhandledKey(Context* ctx, const KeyEvent& key) {
219232
if ((key.modifier() & ~kShiftMask) == 0 &&
220233
key.keycode() >= 0x20 && key.keycode() <= 0x7e) {
221234
raw_sequence_.clear();
222-
DLOG(INFO) << "clear sequence.";
235+
DLOG(INFO) << "clear raw sequence.";
223236
}
224237
}
225238

0 commit comments

Comments
 (0)