Skip to content

Commit

Permalink
feat(key_binder): bind key to a key sequence
Browse files Browse the repository at this point in the history
Closes #301

To bind a key to a key sequence,
set 'send_sequence' attribute of a 'key_binder/bindings' entry.

Special named keys should be around with "{" and "}".
Notice that you have to add quotes when writing a filed with "{" or "}" in a
yaml file.

For example:
key_binder:
  bindings:
    - { when: composing, accept: a, send_sequence: aa }
    - { when: composing, accept: Right, send_sequence: "{Right}{Right}" }
    - { when: composing, accept: Left, send_sequence: "aa{Left}{Left}" }
  • Loading branch information
SleepyBag authored and lotem committed Sep 5, 2019
1 parent 75a60dc commit 3b5dbf6
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions src/rime/gear/key_binder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ static KeyBindingCondition translate_condition(const string& str) {

struct KeyBinding {
KeyBindingCondition whence;
KeyEvent target;
KeySequence target;
function<void (Engine* engine)> action;

bool operator< (const KeyBinding& o) const {
Expand Down Expand Up @@ -118,11 +118,20 @@ void KeyBindings::LoadBindings(const an<ConfigList>& bindings) {
continue;
}
if (auto target = map->GetValue("send")) {
if (!binding.target.Parse(target->str())) {
KeyEvent key;
if (key.Parse(target->str())) {
binding.target.push_back(std::move(key));
} else {
LOG(WARNING) << "invalid key binding #" << i << ".";
continue;
}
}
else if (auto target = map->GetValue("send_sequence")) {
if (!binding.target.Parse(target->str())) {
LOG(WARNING) << "invalid key sequence #" << i << ".";
continue;
}
}
else if (auto option = map->GetValue("toggle")) {
binding.action = std::bind(&toggle_option, _1, option->str());
}
Expand Down Expand Up @@ -203,7 +212,9 @@ void KeyBinder::PerformKeyBinding(const KeyBinding& binding) {
}
else {
redirecting_ = true;
engine_->ProcessKey(binding.target);
for (const KeyEvent& key_event : binding.target) {
engine_->ProcessKey(key_event);
}
redirecting_ = false;
}
}
Expand Down

3 comments on commit 3b5dbf6

@1yx
Copy link

@1yx 1yx commented on 3b5dbf6 Mar 13, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good Job! 我刚好试图在 rime 上找一种方案实现中英混排时加空格,正如 https://github.com/vinta/pangu.js 所做的。
{ when: composing, accept: Return, send_sequence: " {Escape} " }
似乎这样的配置就可以实现。

@david50407
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

有計劃什麼時候會 release 這個 feature 嗎?

@lotem
Copy link
Member

@lotem lotem commented on 3b5dbf6 May 19, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

有計劃什麼時候會 release 這個 feature 嗎?

還沒有。不過最近 https://github.com/BYVoid/OpenCC 又開始更新了,且發佈了API的變更。librime 也不得不準備跟進。

Please sign in to comment.