*
sublime 下 解决Vintage模式下,normal 模式下中文输入的问题。*
大家好,相信有不少sublime 铁粉在vintage 模式下,使用vim 快捷键时,在转换normal 与 插入模式时,还需要转换一下中文输入法的问题所困扰。我也是找遍互联网,也没有找到好的实现方法。不得不自已研究一下。闲话少说,上代码
1:大家找到这个文件,C:\Program Files\Sublime Text\Packages\Vintage.sublime-package , 把此文件修改成zip 结尾,
2:解压上面的zip文件,找到里面的 vintage.py 文件。
3:在class InputState:里加入新的属性如下
3:再看下面的方法新增部份
user32.keybd_event(0x5B, 0, 0, 0) // 模拟windows 键按下
user32.keybd_event(0x20, 0, 0, 0) // 模拟空格键按下
user32.keybd_event(0x5B, 0, 0x0002, 0) // 模拟windows 键弹起
user32.keybd_event(0x20, 0, 0x0002, 0) // 模拟空格 键弹起
4:在下面方法里加入进入插入模式时的动作,如发现已操作过退出中文输入法,则进入时再转换输入法回去
5:在最上面引入ctype 库,此库是python 操作window相关API 用
最后,希望大家使用愉快。
后面是相应的代码快,可拷贝
#引入相应的库
import ctypes
#加入存贮输入法的全局变量
class InputState:
regChina = None
#退出插入模式时,如发现是中文,则进行输入法转换,有上面图里红色部份是新加的部份
class ExitInsertMode(sublime_plugin.TextCommand):
def run_(self, edit_token, args):
edit = self.view.begin_edit(edit_token, self.name(), args)
try:
self.run(edit)
finally:
self.view.end_edit(edit)
# Call after end_edit(), to ensure the final entry in the glued undo
# group is 'exit_insert_mode'.
user32 = ctypes.WinDLL('user32', use_last_error=True)
curr_window = user32.GetForegroundWindow()
thread_id = user32.GetWindowThreadProcessId(curr_window, 0)
klid = user32.GetKeyboardLayout(thread_id)
lid = klid & (2**16 - 1)
lid_hex = hex(lid)
if lid_hex == '0x804':
user32.keybd_event(0x5B, 0, 0, 0)
user32.keybd_event(0x20, 0, 0, 0)
user32.keybd_event(0x5B, 0, 0x0002, 0)
user32.keybd_event(0x20, 0, 0x0002, 0)
print("change to english")
g_input_state.regChina = 1
else:
g_input_state.regChina = None
print("lid_hex = ", lid_hex)
self.view.run_command('glue_marked_undo_groups')
def run(self, edit):
self.view.settings().set('command_mode', True)
self.view.settings().set('inverse_caret_state', True)
if not self.view.has_non_empty_selection_region():
self.view.run_command('vi_move_by_characters_in_line', {'forward': False})
update_status_line(self.view)
#在进入插入模式时,再次据上次的输入法模式,再进行回到相应的模式里
class EnterInsertMode(sublime_plugin.TextCommand):
# Ensure no undo group is created: the only entry on the undo stack should
# be the insert_command, if any
def run_(self, edit_token, args):
if args:
return self.run(**args)
else:
return self.run()
def run(self, insert_command = None, insert_args = {}, register = '"'):
# mark_undo_groups_for_gluing allows all commands run while in insert
# mode to comprise a single undo group, which is important for '.' to
# work as desired.
if g_input_state.regChina is not None:
user32 = ctypes.WinDLL('user32', use_last_error=True)
user32.keybd_event(0x5B, 0, 0, 0)
user32.keybd_event(0x20, 0, 0, 0)
user32.keybd_event(0x5B, 0, 0x0002, 0)
user32.keybd_event(0x20, 0, 0x0002, 0)
print("change to china")
self.view.run_command('maybe_mark_undo_groups_for_gluing')
if insert_command:
args = insert_args.copy()
args.update({'register': register})
self.view.run_command(insert_command, args)
self.view.settings().set('command_mode', False)
self.view.settings().set('inverse_caret_state', False)
update_status_line(self.view)
6:修改上面代码后,则再次把这个 Vintage.sublime-package 文作夹压缩成ZIP 文件,去掉ZIP文件类型,再放到原来的目录里,修改前自行备份原来的文件。
最后相说明的一点,如你的转换中英文的快捷键不是windows+空格,请自行修改成相应的虚拟键码。
参靠的知识有如下:
从下面知识里获得灵感,也感谢作者
https://www.zhihu.com/question/26983401
下面是关于获取输入法的方法
https://blog.csdn.net/shjsfx/article/details/106185038