Skip to content

Commit 2f24489

Browse files
committed
feat(ascii_composer): support alt, super key press
1 parent 37b3246 commit 2f24489

2 files changed

Lines changed: 31 additions & 13 deletions

File tree

src/rime/gear/ascii_composer.cc

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,11 @@ AsciiComposer::~AsciiComposer() {
5858
}
5959

6060
ProcessResult AsciiComposer::ProcessKeyEvent(const KeyEvent& key_event) {
61-
if ((key_event.shift() && key_event.ctrl()) || key_event.alt() ||
62-
key_event.super()) {
63-
shift_key_pressed_ = ctrl_key_pressed_ = false;
61+
if (key_event.shift() + key_event.ctrl() + key_event.alt() +
62+
key_event.super() >
63+
1) {
64+
shift_key_pressed_ = ctrl_key_pressed_ = alt_key_pressed_ =
65+
super_key_pressed_ = false;
6466
return kNoop;
6567
}
6668
if (caps_lock_switch_style_ != kAsciiModeSwitchNoop) {
@@ -80,23 +82,34 @@ ProcessResult AsciiComposer::ProcessKeyEvent(const KeyEvent& key_event) {
8082
}
8183
bool is_shift = (ch == XK_Shift_L || ch == XK_Shift_R);
8284
bool is_ctrl = (ch == XK_Control_L || ch == XK_Control_R);
83-
if (is_shift || is_ctrl) {
85+
bool is_alt = (ch == XK_Alt_L || ch == XK_Alt_R);
86+
bool is_super = (ch == XK_Super_L || ch == XK_Super_R);
87+
if (is_shift || is_ctrl || is_alt || is_super) {
8488
if (key_event.release()) {
85-
if (shift_key_pressed_ || ctrl_key_pressed_) {
89+
if (shift_key_pressed_ || ctrl_key_pressed_ || alt_key_pressed_ ||
90+
super_key_pressed_) {
8691
auto now = std::chrono::steady_clock::now();
8792
if (((is_shift && shift_key_pressed_) ||
88-
(is_ctrl && ctrl_key_pressed_)) &&
93+
(is_ctrl && ctrl_key_pressed_) || (is_alt && alt_key_pressed_) ||
94+
(is_super && super_key_pressed_)) &&
8995
now < toggle_expired_) {
9096
ToggleAsciiModeWithKey(ch);
9197
}
92-
shift_key_pressed_ = ctrl_key_pressed_ = false;
98+
shift_key_pressed_ = ctrl_key_pressed_ = alt_key_pressed_ =
99+
super_key_pressed_ = false;
93100
return kNoop;
94101
}
95-
} else if (!(shift_key_pressed_ || ctrl_key_pressed_)) { // first key down
102+
} else if (!(shift_key_pressed_ || ctrl_key_pressed_ || alt_key_pressed_ ||
103+
super_key_pressed_)) {
104+
// first key down
96105
if (is_shift)
97106
shift_key_pressed_ = true;
98-
else
107+
else if (is_ctrl)
99108
ctrl_key_pressed_ = true;
109+
else if (is_alt)
110+
alt_key_pressed_ = true;
111+
else if (is_super)
112+
super_key_pressed_ = true;
100113
// will not toggle unless the toggle key is released shortly
101114
const auto toggle_duration_limit = std::chrono::milliseconds(500);
102115
auto now = std::chrono::steady_clock::now();
@@ -105,9 +118,11 @@ ProcessResult AsciiComposer::ProcessKeyEvent(const KeyEvent& key_event) {
105118
return kNoop;
106119
}
107120
// other keys
108-
shift_key_pressed_ = ctrl_key_pressed_ = false;
121+
shift_key_pressed_ = ctrl_key_pressed_ = alt_key_pressed_ =
122+
super_key_pressed_ = false;
109123
// possible key binding: Control+x, Shift+space
110-
if (key_event.ctrl() || (key_event.shift() && ch == XK_space)) {
124+
if (key_event.ctrl() || key_event.alt() || key_event.super() ||
125+
(key_event.shift() && ch == XK_space)) {
111126
return kNoop;
112127
}
113128
Context* ctx = engine_->context();
@@ -129,7 +144,8 @@ ProcessResult AsciiComposer::ProcessCapsLock(const KeyEvent& key_event) {
129144
int ch = key_event.keycode();
130145
if (ch == XK_Caps_Lock) {
131146
if (!key_event.release()) {
132-
shift_key_pressed_ = ctrl_key_pressed_ = false;
147+
shift_key_pressed_ = ctrl_key_pressed_ = alt_key_pressed_ =
148+
super_key_pressed_ = false;
133149
// temporarily disable good-old (uppercase) Caps Lock as mode switch key
134150
// in case the user switched to ascii mode with other keys, eg. with Shift
135151
if (good_old_caps_lock_ && !toggle_with_caps_) {
@@ -152,7 +168,7 @@ ProcessResult AsciiComposer::ProcessCapsLock(const KeyEvent& key_event) {
152168
}
153169
if (key_event.caps()) {
154170
if (!good_old_caps_lock_ && !key_event.release() && !key_event.ctrl() &&
155-
isascii(ch) && isalpha(ch)) {
171+
!key_event.alt() && !key_event.super() && isascii(ch) && isalpha(ch)) {
156172
// output ascii characters ignoring Caps Lock
157173
if (islower(ch))
158174
ch = toupper(ch);

src/rime/gear/ascii_composer.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ class AsciiComposer : public Processor {
5151
bool toggle_with_caps_ = false;
5252
bool shift_key_pressed_ = false;
5353
bool ctrl_key_pressed_ = false;
54+
bool alt_key_pressed_ = false;
55+
bool super_key_pressed_ = false;
5456
using TimePoint = std::chrono::steady_clock::time_point;
5557
TimePoint toggle_expired_;
5658
connection connection_;

0 commit comments

Comments
 (0)