Skip to content

如何阻止 lua_processor 将触发按键发送出去? #20

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Ace-Who opened this issue Sep 3, 2019 · 2 comments
Closed

如何阻止 lua_processor 将触发按键发送出去? #20

Ace-Who opened this issue Sep 3, 2019 · 2 comments

Comments

@Ace-Who
Copy link

Ace-Who commented Sep 3, 2019

套用 rime/librime#248 的例子来说明:

-- Control+v 转大写,加空格

local function do_capital_space(s)
  return string.upper(s .. " ")
end

local function myproc(key, env)
  local engine = env.engine
  local context = engine.context
  local page_size = engine.schema.page_size

  --- accept: Control+v
  if (key:repr() == 'Control+v') then
    --- when: composing
    if (context:is_composing()) then
      local s_orig = context:get_commit_text()
      --- do_capital_space
      local s = do_capital_space(s_orig) .. ' <' .. key:repr() .. '>'
      engine:commit_text(s)
      context:clear()
      return 0 -- kAccepted
    end
  end
  return 2 -- kNoop
end

return myproc

方案配置:

...
engine:
  processors:
    - ascii_composer
    - recognizer
    - lua_processor@myproc
    - key_binder
    - speller
    - punctuator
    - selector
    - navigator
    - express_editor
...

使用时,在选字状态下按 Ctrl + v,成功加了空格,但是也发生了粘贴操作。又如,将按键判断改为 key:repr() == '8',那么上屏时除加了空格以外,还会加个「8」。

@hchunhui
Copy link
Owner

hchunhui commented Sep 3, 2019

-- Control+v 转大写,加空格

local function do_capital_space(s)
  return string.upper(s .. " ")
end

local function myproc(key, env)
  local engine = env.engine
  local context = engine.context
  local page_size = engine.schema.page_size

  --- accept: Control+v
  if (key:repr() == 'Control+v') then
    --- when: composing
    if (context:is_composing()) then
      local s_orig = context:get_commit_text()
      --- do_capital_space
      local s = do_capital_space(s_orig) .. ' <' .. key:repr() .. '>'
      engine:commit_text(s)
      context:clear()
      return 0 -- kAccepted

是我打错了,返回 0 是拒绝,1 才是接受。
返回拒绝时,虽然我们已经处理过按键了,但系统以为没有,于是会按默认值再处理一遍。

end

end
return 2 -- kNoop
end

return myproc

@Ace-Who
Copy link
Author

Ace-Who commented Sep 3, 2019

谢谢,解决了。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants