From 3b5dbf61527cc128d482c4726017d1516d59c3d4 Mon Sep 17 00:00:00 2001 From: Xue Qianming Date: Mon, 2 Sep 2019 19:55:24 +0800 Subject: [PATCH] feat(key_binder): bind key to a key sequence 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}" } --- src/rime/gear/key_binder.cc | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/src/rime/gear/key_binder.cc b/src/rime/gear/key_binder.cc index a99e802a24..b7c37d109c 100644 --- a/src/rime/gear/key_binder.cc +++ b/src/rime/gear/key_binder.cc @@ -48,7 +48,7 @@ static KeyBindingCondition translate_condition(const string& str) { struct KeyBinding { KeyBindingCondition whence; - KeyEvent target; + KeySequence target; function action; bool operator< (const KeyBinding& o) const { @@ -118,11 +118,20 @@ void KeyBindings::LoadBindings(const an& 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()); } @@ -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; } }