圣者
精华
|
战斗力 鹅
|
回帖 0
注册时间 2005-8-12
|
本帖最后由 Tring 于 2021-7-30 03:23 编辑
STEAM上的日呆游戏也就这几年稍微好点,前几年几乎没一个能完全自定义按键的。
所以找个本地按键映射工具才是一本万利的解决方式。
贴个我自己用的python改键脚本吧:
- #! python3
- # coding: utf-8
- import mouse, keyboard
- class mapper(object):
-
- def __init__(self, key_map, swit_hotkey = 'ctrl+tab', exit_hotkey = 'ctrl+~'):
- self.swit_hotkey = swit_hotkey
- self.exit_hotkey = exit_hotkey
- self.key_map = [mp for cyc in key_map for mp in self.parse_cyclic(cyc)]
- self.map_stat = None
- print('use', self.exit_hotkey, 'to exit')
- print('use', self.swit_hotkey, 'to switch on/off')
- print('==========')
- print('key map:')
- for s, d in self.key_map:
- print(s, '->', d)
- print('==========')
- print('state:')
- self.hook()
- self.remap()
- def parse_cyclic(self, cyc):
- lcyc = len(cyc)
- if lcyc < 2:
- return []
- r = []
- for i in range(lcyc - 1):
- r.append([cyc[i], cyc[i + 1]])
- r.append([cyc[-1], cyc[0]])
- return r
- def hook(self):
- keyboard.add_hotkey(self.swit_hotkey, self.switch)
- def _is_mouse(self, s):
- ss = s.split(' ')
- if ss[0] == 'mouse':
- return [True, ' '.join(ss[1:])]
- elif ss[0] == 'num' and ss[1].isdigit():
- return [False, keyboard.key_to_scan_codes(ss[1])[-1]]
- else:
- return [False, s]
- def _remap_key_or_mouse(self, src, dst):
- im_src, src = self._is_mouse(src)
- im_dst, dst = self._is_mouse(dst)
- self.km_cyc = False
- if im_dst:
- def press(d):
- self.km_cyc = True
- mouse.press(d)
- def release(d):
- mouse.release(d)
- self.km_cyc = False
- else:
- def press(d):
- if not im_src or not self.km_cyc:
- keyboard.press(d)
- def release(d):
- if not im_src or not self.km_cyc:
- keyboard.release(d)
-
- def _hndl_key(ev):
- if ev.event_type == keyboard.KEY_DOWN:
- press(dst)
- else:
- release(dst)
- return False
- def _hndl_mouse(ev):
- if not ( isinstance(ev, mouse.ButtonEvent)
- and ev.button == src ):
- return
- #print(ev)
- if ev.event_type == mouse.DOWN:
- press(dst)
- elif ev.event_type == mouse.UP:
- release(dst)
- if im_src:
- return [mouse.hook(_hndl_mouse)]
- else:
- return keyboard.hook_key(src, _hndl_key, suppress=True)
- def remap(self):
- self.map_stat = []
- for mp in self.key_map:
- self.map_stat.append(self._remap_key_or_mouse(*mp))
- print('\ron ', end='')
- def unmap(self):
- for mp in self.map_stat:
- if isinstance(mp, list):
- mouse.unhook(mp[0])
- else:
- keyboard.unhook(mp)
- self.map_stat = None
- print('\roff', end='')
- def switch(self):
- if self.map_stat:
- self.unmap()
- else:
- self.remap()
- def wait(self):
- keyboard.wait(self.exit_hotkey)
- keyboard.unhook_all()
- mouse.unhook_all()
- if __name__ == '__main__':
- mapper([
- ['up', 'w', 'q', 'tab'],
- ['down', 's', 'space'],
- ['left', 'a', 'left shift'],
- ['right', 'd', 'mouse left'],
- ['f', 'c'],
- ['r', 'left ctrl', 'e'],
- ], swit_hotkey = '~+1', exit_hotkey = '~+esc').wait()
复制代码 需要用pip额外装mouse和keyboard两个库。
改键的设置在最底下。
|
评分
-
查看全部评分
|