#------------------------------------------------------------------------------ # ●任意キー取得スクリプト by tonbi # # 最新版の情報やバグ報告はここ → http://www.mc.ccnw.ne.jp/sarada/toshi/ # # これを使用することで、イベントのスクリプトから、 # # get_press(variable,key) キーが押されているか取得 # get_trigger(variable,key) キーが押した瞬間であるか取得 # get_repeat(variable,key) キーが押されているか取得(一定時間ごとに) # get_press_all(variable) 上記の一括取得版 # get_trigger_all(variable) 上記の一括取得版 # get_repeat_all(variable) 上記の一括取得版 # # ※variable = 変数ID key = キーID です。 # # これらが実行できるようになります。 # それぞれ、指定したナンバーの変数に、押されていたら1、違ったら0が代入されます。 # また、下段の一括取得系は、指定した変数のナンバーから順に、キーID:0,1,2,・・・ # とすべてのキーIDが代入されていきます。 # # 条件分岐のスクリプトで、Key.press?(key) と入れると、変数使わず分岐できます。 # Key.trigger?(key)、Key.repeat?(key) もできます。 # # キーID初期設定は、(タイピングゲーム向き?) # # 0 : カーソルキー[←] # 1 : カーソルキー[↑] # 2 : カーソルキー[→] # 3 : カーソルキー[↓] # 4 : [ENTER] # 5 : [SPACE] # 6 : [ESC] # 7 : [BACKSPACE] # 8 : [DELETE] # 9 : [SHIFT] # 10 : [CTRL] # 11 : [ALT] # 12 : [TAB] # 13 : [CAPSLOCK] # 14〜23 : [0]〜[9](メインキーボード) # 24〜49 : [A]〜[Z] # 50〜59 : [0]〜[9](テンキー) # 60 : [,] (ね) # 61 : [.] (る) # 62 : [/] (め) # 63 : [_] (ろ) # 64 : [;] (れ) # 65 : [:] (け) # 66 : []] (む) # 67 : [@] (゛) # 68 : [[] (゜) # 69 : [-] (ほ) # 70 : [^] (へ) # 71 : [\] # # 下のほうの★印のコメントの辺りをいじれば、 # いらないキーを削除したり、増やしたりができます。 # キーコードの知識が必要ですけど。 # #------------------------------------------------------------------------------ # ●スクリプトから直接様々なキーを取得したい方へ # # 1.取得したいシーンのInput.updateの後ろあたりに「Key.update」を追加 # 2.Key.trigger?(keyID)として取得する。press?、repeat?でも可 #   ちなみに返り値はInputの場合と同じ。 # #------------------------------------------------------------------------------ #============================================================================== # ■ Key # キー取得を管理するクラス #============================================================================== module Key #---------------------------------------------------------- # ● オブジェクト初期化 #---------------------------------------------------------- def self.setup @keystatus=[] @getkeystate = Win32API.new("user32", "GetKeyState", "i", "i") end #---------------------------------------------------------- # ● 更新 #---------------------------------------------------------- def self.update for i in @keystatus num1 = false for j in i[1] num2=@getkeystate.call(j) if num2 != 1 and num2 != 0 num1 = true break end end if num1 == false if i[0] > 0 i[0] = i[0]*-1 else i[0] = 0 end else if i[0] > 0 i[0] += 1 else i[0] = 1 end end end end #---------------------------------------------------------- # ● キーの設定された最大数 #---------------------------------------------------------- def self.max return @keystatus.size end #---------------------------------------------------------- # ● キー設定を追加する # id 追加先ID # code 追加するキーコード #---------------------------------------------------------- def self.add_key(id,code) if @keystatus[id]=nil keystatus[id]=[] end @keystatus[id][0]=0 @keystatus[id][1].push(code) end #---------------------------------------------------------- # ● キー設定を削除する # id 削除ID #---------------------------------------------------------- def self.del_key(id) @keystatus[id]=nil end #---------------------------------------------------------- # ● キー設定を一括変更する # val キー設定の配列、2次元 # 例 [[65],[66,67],[68]] # なら、ID.0=Aキー、ID.1=BとCキー、ID.2=Dキーと設定 #---------------------------------------------------------- def self.add_key_set(val) @keystatus = [] for i in 0...val.size if @keystatus[i]==nil @keystatus[i]=[] end @keystatus[i][0]=0 @keystatus[i][1]=val[i] end end #---------------------------------------------------------- # ● キー設定を一括削除する #---------------------------------------------------------- def self.del_key_set @keystatus[i]=nil end #---------------------------------------------------------- # ● キー情報を返す。Inputとおなじ。 # id キーID #---------------------------------------------------------- def self.press?(id) if @keystatus[id][0] > 0 return true else return false end end def self.trigger?(id) if @keystatus[id][0] == 1 return true else return false end end def self.repeat?(id) if @keystatus[id][0] <= 0 return false else if @keystatus[id][0] % 5 == 1 and @keystatus[id][0] % 5 != 2 return true else return false end end end end #============================================================================== # ■ Scene_Title (キーの初期化呼び出し) #============================================================================== class Scene_Title alias main_tonbi12 main def main #---------------------------------------------------------------- # ★ここをいじれば、キーIDとキーコードの設定を変える事ができます。 # 一つのIDに複数キー入れたいならば # 変数 val = [[37],[38],[39],[40],[13,32]] # のように2次元配列になるようにしてください。 # [13,32]のように1つのIDに複数のキーを設定できます。 # set = [37,38,39,40,13,32,27,8,46,16,17,18,9,20,48,49,50,51,52,53,54,55,56,57, 65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90, 96,97,98,99,100,101,102,103,104,105,188,190,191,226,187,186,221,192,219,189, 222,220] # 2次元に変換 val = [] for i in set val.push([i]) end # set でなくて val に設定してください。set は使い捨て。 #---------------------------------------------------------------- Key.setup Key.add_key_set(val) Key.update main_tonbi12 end end #============================================================================== # ■ Scene_Map (キーの更新を追加) #============================================================================== class Scene_Map alias update_tonbi12 update def update Key.update update_tonbi12 end end #============================================================================== # ■ Scene_battle (キーの更新を追加) #============================================================================== class Scene_Battle alias update_tonbi12 update def update Key.update update_tonbi12 end end #============================================================================== # ■ Interpreter (イベント>スクリプト用メソッド) #============================================================================== class Interpreter def get_press(variable,id) $game_variables[variable]= Key.press?(id) == true ? 1 : 0 end def get_trigger(variable,id) $game_variables[variable]= Key.trigger?(id) == true ? 1 : 0 end def get_repeat(variable,id) $game_variables[variable]= Key.repeat?(id) == true ? 1 : 0 end def get_press_all(variable) for i in 0...Key.max $game_variables[variable+i]= Key.press?(i) == true ? 1 : 0 end end def get_trigger_all(variable) for i in 0...Key.max $game_variables[variable+i]= Key.trigger?(i) == true ? 1 : 0 end end def get_repeat_all(variable) for i in 0...Key.max $game_variables[variable+i]= Key.repeat?(i) == true ? 1 : 0 end end end