开始

通常,我们使用热字串是这样的:

::;btw::by the way

需要使用结束符(; ' ' .等)来触发。

但在官方文档中,找到一种方法可以不使用结束符,基于InputHook的方式。

原始的例子就不展示了,我们直接入正题--封装后的代码。

代码



/**
 * @example
 * ; register actions by constructor
 * _ih := HotStringEx('~;', 1, ['foo', (*) => SendText('bar')])
 * ; or via [_ih.register()] method
 * actions := [
 *   ['btw', (*) => SendText('by the way')],
 *   ['jq', (*) => SendText('jquery')]
 * ]
 * _ih.Register(actions*)
 * ; cancel actions
 * _ih.Cancel('btw') ; O(n) operation
 * ; set timeout function or max-limit function
 * _ih.TimeOutFunc := (*) => MsgBox('timeout')
 * 
 */
class HotStringEx {

  actions := Map()
  matchList := ''
  TimeOutFunc := Noop
  MaxFunc := Noop

  ; ['jq', (*)=>'jquery'], ...
  __New(hk, hkLen, listAndActions*) {
    this.hk := hk
    this.hkLen := hkLen
    if (listAndActions.Length) {
      this._Update(listAndActions)
      Hotkey this.hk, (*) => this._Start(this.matchList), 'On'
    }
  }

  _Update(_param) {
    for v in _param
      this.actions.Set(v[1], v[2])
    for k in this.actions
      _matchList .= k ','
    this.matchList := RTrim(_matchList, ',')
  }

  _Start(matchList) {
    ih := InputHook('V T5 L8 C', '{space};', matchList)
    ih.Start()
    ih.Wait()
    switch ih.EndReason {
      case "Max": this.MaxFunc()
      case "Timeout": this.TimeOutFunc()
      case "EndKey":
      default:
        Send JoinStr('', '{BS ', this.hkLen + ih.input.Length, '}')
        this.actions.Get(ih.Input)()
    }
  }

  _Reload() => Hotkey(this.hk, (*) => this._Start(this.matchList), 'On')

  Register(listAndActions*) {
    this._Update(listAndActions)
    this._Reload()
  }

  Cancel(list*) {
    for key in list
      this.actions.Delete(key)
    this._Update('')
  }
}

示例

使用代码中使用的例子:

; register actions by constructor
_ih := HotStringEx('~;', 1, ['foo', (*) => SendText('bar')])
; or via [_ih.register()] method
actions := [
  ['btw', (*) => SendText('by the way')],
  ['jq', (*) => SendText('jquery')]
]
_ih.Register(actions*)
; cancel actions
_ih.Cancel('btw') ; O(n) operation
; set timeout function or max-limit function
_ih.TimeOutFunc := (*) => MsgBox('timeout')

可以在构造函数中提供actions,也可以使用register方法。

注意

需要注意的是,如果有注册了两个:Date Datetime

第二个将永远不会触发,因为第一个是它的前缀。

以及如果有普通的热字串:Date,优先触发的也是注册的Date。

一种推荐的方式是对于常规的首字母不大写,而注册的大写。

下面是我使用中的例子:

hse := HotStringEx('~;', 1)

main_actions := [
  ['Date', (*) => SendInput(FormatTime(, "yyyy/MM/dd"))],
  ['Time', (*) => SendInput(FormatTime(, "HH:mm:ss"))]
]
hse.Register(main_actions*)
::;datetime:: {
  SendInput(FormatTime(, "yyyy/MM/dd_HH:mm:ss/tt"))
}
posted on 2024-03-25 12:45  落寞的雪  阅读(39)  评论(0编辑  收藏  举报