afxのキーマップ設定AFX.KEYを書き換えるrubyスクリプトを作ってみた
概要
keymap.txtを読み込んでAFX.KEYを生成する。
keymap.txtはタブ区切りでキー、動作、コメントの順に書いておく。
[NORMAL]はファイラーでのキーマップ、[GVIEW]は画像ビューアーでのキーマップ、[TVIEW]はテキストビューアーでのキーマップとして書く。
[NORMAL]
h left ←
j down ↓
k up ↑
l right →
...
実行すると以下のようなAFX.KEYができる。
[NORMAL]
K0000="0072:0037"
K0001="0074:0040"
K0002="0075:0038"
K0003="0076:0039"
K0004="1072:0072"
...
ソース
# -*- coding: utf-8 -*- i = 0 key = "" act = "" a1 = 0 k1 = 0 infile = "keymap.txt" outfile = "AFX.KEY" #outfile = "TESTAFX.KEY" def keynum(key) num = 0 case key when "bs" then; num = 8 when "ret" then; num = 13 when "esc" then; num = 27 when "spc" then; num = 32 when "pageup" then; num = 33 when "pagedown" then; num = 34 when "end" then; num = 35 when "home" then; num = 36 when "left" then; num = 37 when "up" then; num = 38 when "right" then; num = 39 when "down" then; num = 40 when "ins" then; num = 45 when "del" then; num = 46 when "1" then; num = 49 when "2" then; num = 50 when "3" then; num = 51 when "4" then; num = 52 when "5" then; num = 53 when "6" then; num = 54 when "7" then; num = 55 when "8" then; num = 56 when "9" then; num = 57 when "a" then; num = 65 when "b" then; num = 66 when "c" then; num = 67 when "d" then; num = 68 when "e" then; num = 69 when "f" then; num = 70 when "g" then; num = 71 when "h" then; num = 72 when "i" then; num = 73 when "j" then; num = 74 when "k" then; num = 75 when "l" then; num = 76 when "m" then; num = 77 when "n" then; num = 78 when "o" then; num = 79 when "p" then; num = 80 when "q" then; num = 81 when "r" then; num = 82 when "s" then; num = 83 when "t" then; num = 84 when "u" then; num = 85 when "v" then; num = 86 when "w" then; num = 87 when "x" then; num = 88 when "y" then; num = 89 when "z" then; num = 90 when ":" then; num = 186 when "/" then; num = 191 when "\\" then; num = 220 when "_" then; num = 226 when "ten0" then; num = 96 when "ten*" then; num = 106 when "ten+" then; num = 107 when "ten-" then; num = 109 end return num end print "#{infile} から #{outfile} を作成 ... " i = 0; out = File.open(outfile, 'w') #print "[KEYCUST]\nON=1\n" out.puts "[KEYCUST]\nON=1\n" open(infile) { |file| while file.gets chomp! if $_ == "[NORMAL]" || $_ == "[GVIEW]" || $_ == "[TVIEW]" then # print "#{$_}\n" out.puts "#{$_}\n" i = 0 else key = "" act = "" key = scan(/^([^\t]*)\t+([^\t]*).*$/)[0][0] act = scan(/^([^\t]*)\t+([^\t]*).*$/)[0][1] k1 = 0 if key =~ /S-/ then; k1 += 1 end if key =~ /C-/ then; k1 += 2 end if key =~ /A-/ then; k1 += 4 end if k1 != 0 then; key = key.scan(/^.*-([^-]*)$/)[0][0] end key = keynum(key) if act =~ /^"(.*)"$/ then # print sprintf("K%04d=\"%01d%03d%s\"\n", i, k1, key, $1) out.puts sprintf("K%04d=\"%01d%03d%s\"\n", i, k1, key, $1) else a1 = 0 if act =~ /S-/ then; a1 += 1 end if act =~ /C-/ then; a1 += 2 end if act =~ /A-/ then; a1 += 4 end if a1 != 0 then; act = act.scan(/^.*-([^-]*)$/)[0][0] end act = keynum(act) # print sprintf("K%04d=\"%01d%03d:%01d%03d\"\n", i, k1, key, a1, act) out.puts sprintf("K%04d=\"%01d%03d:%01d%03d\"\n", i, k1, key, a1, act) end i = i + 1 end end } print "完了"; exit;
このスクリプトは1から作ったのではなくて、大学のころ教授にもらったperlのスクリプトをrubyに移植しただけです^^
1から書けるようになりたい><