Last active
September 9, 2020 10:13
-
-
Save honjow/1bc1a1603bcb6278501a20d479d3446e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| local module = {} | |
| -- 默认为将ctrl映射为f19 | |
| KeyMap = { | |
| key = "ctrl", | |
| timeout = 0.15, | |
| action = function() | |
| hs.eventtap.keyStroke({}, "f19", 0) | |
| end | |
| } | |
| local events = hs.eventtap.event.types | |
| local log = hs.logger.new("key_remap", "info") | |
| -- 构造 | |
| function KeyMap:new(o) | |
| o = o or {} | |
| setmetatable(o, self) | |
| self.__index = self | |
| self.log = hs.logger.new("key_remap", "info") | |
| self.last_flags_1 = {} | |
| self.last_flags_0 = {} | |
| self.last_time_1 = 0 | |
| self.last_time_0 = 0 | |
| return o | |
| end | |
| function KeyMap:init() | |
| self.watcher = | |
| hs.eventtap.new( | |
| {events.flagsChanged}, | |
| function(e) | |
| local typ = e:getType() | |
| local code = e:getKeyCode() | |
| local flags = e:getFlags() | |
| local now = hs.timer.secondsSinceEpoch() | |
| if | |
| _dict_has_no_other_key(flags, self.key) and not flags[self.key] and | |
| _dict_has_no_other_key(self.last_flags_0, self.key) and | |
| self.last_flags_0[self.key] and | |
| _dict_has_no_other_key(self.last_flags_1, self.key) and | |
| not self.last_flags_1[self.key] and | |
| now - self.last_time_0 < self.timeout | |
| then | |
| log.i(self.key .. "映射事件") | |
| if self.action then | |
| self.action() | |
| end | |
| end | |
| self.last_flags_1 = self.last_flags_0 | |
| self.last_flags_0 = flags | |
| self.last_time_1 = self.last_time_0 | |
| self.last_time_0 = now | |
| return false | |
| end | |
| ) | |
| self.watcher:start() | |
| end | |
| -- 检查是否按下其它按键 | |
| function _dict_has_no_other_key(dic, key) | |
| for k, v in pairs(dic) do | |
| if k ~= key then | |
| return false | |
| end | |
| end | |
| return true | |
| end | |
| return module |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| local module = {} | |
| -- 默认为将ctrl映射为f19 | |
| KeyMap = { | |
| key = "ctrl", | |
| timeout = 0.15, | |
| action = function() | |
| hs.eventtap.keyStroke({}, "f19", 0) | |
| end | |
| } | |
| local events = hs.eventtap.event.types | |
| local log = hs.logger.new("key_remap", "info") | |
| -- 构造 | |
| function KeyMap:new(o) | |
| o = o or {} | |
| setmetatable(o, self) | |
| self.__index = self | |
| self.log = hs.logger.new("key_remap", "info") | |
| self.last_flags_1 = {} | |
| self.last_flags_0 = {} | |
| self.last_time_1 = 0 | |
| self.last_time_0 = 0 | |
| return o | |
| end | |
| function KeyMap:init() | |
| self.watcher = | |
| hs.eventtap.new( | |
| {events.flagsChanged}, | |
| function(e) | |
| local typ = e:getType() | |
| local code = e:getKeyCode() | |
| local flags = e:getFlags() | |
| local now = hs.timer.secondsSinceEpoch() | |
| if | |
| _dict_has_no_other_key(flags, self.key) and not flags[self.key] and | |
| _dict_has_no_other_key(self.last_flags_0, self.key) and | |
| self.last_flags_0[self.key] and | |
| _dict_has_no_other_key(self.last_flags_1, self.key) and | |
| not self.last_flags_1[self.key] and | |
| now - self.last_time_0 < self.timeout | |
| then | |
| log.i(self.key .. "映射事件") | |
| if self.action then | |
| self.action() | |
| end | |
| end | |
| self.last_flags_1 = self.last_flags_0 | |
| self.last_flags_0 = flags | |
| self.last_time_1 = self.last_time_0 | |
| self.last_time_0 = now | |
| return false | |
| end | |
| ) | |
| self.watcher:start() | |
| end | |
| -- 检查是否按下其它按键 | |
| function _dict_has_no_other_key(dic, key) | |
| for k, v in pairs(dic) do | |
| if k ~= key then | |
| return false | |
| end | |
| end | |
| return true | |
| end | |
| return module |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require("keyMap") | |
| function toChinese() | |
| require("keyMap") | |
| function toChinese() | |
| -- 其它输入法id可以使用 hs.keycodes.currentSourceID() 方法查看 | |
| -- 切换为 RIME | |
| hs.keycodes.currentSourceID("im.rime.inputmethod.Squirrel.Rime") | |
| end | |
| function toEnglish() | |
| hs.keycodes.currentSourceID("com.apple.keylayout.ABC") | |
| end | |
| -- cmd键的配置 | |
| local mapA = KeyMap:new() | |
| mapA.key = 'cmd' | |
| mapA.action = toChinese | |
| mapA:init() | |
| -- shift键的配置 | |
| local mapB = KeyMap:new() | |
| mapB.key = 'shift' | |
| mapB.action = toEnglish | |
| mapB:init() | |
| -- 切换为 RIME | |
| hs.keycodes.currentSourceID("im.rime.inputmethod.Squirrel.Rime") | |
| end | |
| function toEnglish() | |
| hs.keycodes.currentSourceID("com.apple.keylayout.ABC") | |
| end | |
| -- cmd键的配置 | |
| local mapA = KeyMap:new() | |
| mapA.key = 'cmd' | |
| mapA.action = toChinese | |
| mapA:init() | |
| -- shift键的配置 | |
| local mapB = KeyMap:new() | |
| mapB.key = 'shift' | |
| mapB.action = toEnglish | |
| mapB:init() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment