スクリプト/キー入力を1行で取得する関数

最終投稿者:Material 305033 1 mini cfm_ 更新:2016/07/22 11:37:55
16/02/26 前ver関数をそのまま使えるようにしました。
16/02/20 関数追加、関数名短縮、解説を変更。

input系関数ver.2.1
キー入力とマウス入力をwhileやhasInput()を使わず、一行で取得できる関数です。

ver2.1の詳細な変更点
 keydown,key,mouse,mousex,mousey,anykeyの"get"を省略(getkey=>keyなど)
 click,mousemove,risetを追加


使用できる関数一覧
#========設定========
input_start()                 # 入力開始      (startInput()の代わりに)
input_end()                   # 入力終了      (endInput()の代わりに)

#====キーボード取得====
input_keydown(key)            # キー押下      (キーが押されると1度だけTrue)
input_key(key)                # キー押下中    (キーが押されているときTrue)

#====マウス取得====
input_click()                 # クリック      (マウスが押された時1度だけTrue、input_mouse("DOWN")と同じ)
input_mousemove()             # マウス移動    (マウスが動いたとき一度だけTrueを返す)
input_mousex(),input_mousey() # カーソル座標  (input_mousemove()を使う必要はない)



#====(特殊な操作)====
input_anykey()     #何らかのキーが押されている  (キーが1つでも押されているときTrue)
input_mouse(get)   #マウスの状態を詳細に取得
                   #(引数)"DOWN":今押された, "UP":今離された, "HOLD":押されている, "HOVER":離されている
input_reset()      #キー押下情報をリセット



サンプルスクリプト
#サンプル1 x,yを矢印キーで動かし、ENTERで出力
input_start()
x=0;y=0
while true
  if input_getkey("LEFT")
    x=x-1
  elsif input_getkey("RIGHT")
    x=x+1
  elsif input_getkey("UP")
    y=y-1
  elsif input_getkey("DOWN")
    y=y+1
  elsif input_getkey("ENTER")
    speak("x:"+x+", y:"+y)
  end
end
#サンプル2[A]キーを押すとaと表示、マウスクリックでマウス座標を表示
input_start()
while true
  if input_keydown("A")
    speak("a")
  elsif input_click()
    speak("x:"+input_mousex()+", y:"+input_mousey())
  end
end


使用上の注意
  • この関数を使わない従来の入力取得とは併用できません。入力が正常に取得できなくなります。
  • キーが押されたとき、getkeydown(押下)は事前にgetkey(押下中)を使ってもtrueを返します。input_reset()でリセットできます。

関数のスクリプト(これを貼り付けてください。)
def input_start()
    startInput()
    setVariable("mouse_down",0)
    setVariable("mouse_x",0)
    setVariable("mouse_y",0)
    setVariable("mouse_moved",false)
    setVariable("key_char",splitString("A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,RIGHT,LEFT,UP,DOWN,SPACE,SHIFT,ENTER,ESC,0,1,2,3,4,5,6,7,8,9,-", ","))
    i = 0
    while i < 45
      setVariable("input_new_" + getVariable("key_char")[i],false)
      setVariable("input_" + getVariable("key_char")[i],false)
      i = i + 1
    end
end
#キー入力を再取得する(内部処理用)
def input_update()
  while hasInput()
    takeInput()
    if isMouseMove()
      pos=getMousePosition()
      setVariable("mouse_x",pos[0])
      setVariable("mouse_y",pos[1])
      setVariable("mouse_moved",true)
    elsif isMouseDown()
      setVariable("mouse_down",3)
    elsif isMouseUp()
      setVariable("mouse_down",1)
     else
      i = 0
      while i < 45
        keychar2=getVariable("key_char")[i]
        if isKeyDown(keychar2)
          setVariable("input_" +keychar2,true)
          setVariable("input_new_" + keychar2,true)
        end
        if isKeyUp(getVariable("key_char")[i]) then setVariable("input_" + keychar2,false);end
        i = i + 1
      end
     end
  end
end
def input_keydown(key)
  input_update()
  if getVariable("input_new_" + key)
    setVariable("input_new_" + key,false)
    return getVariable("input_" + key)
  end
  return false
end
def input_key(key)
  input_update()
  return getVariable("input_" + key)
end
def input_anykey()
  input_update()
  i = 0
  while i < 45
    if getVariable("input_" + getVariable("key_char")[i]) then return true;end
    i = i + 1
  end
  return false
end
def input_click()
  input_update()
  i=getVariable("mouse_down")
  if i==3 then setVariable("mouse_down",2);end
  return i==3
end
def input_mouse(get)
  input_update()
  i=getVariable("mouse_down")
  case get
  when "DOWN"#押された
    if i==3 then setVariable("mouse_down",2);end
    return i==3
  when "UP"#離された
    if i==1 then setVariable("mouse_down",0);end
    return i==1
  when "HOLD" return i==2||i==3#押されている
  when "HOVER" return i==0||i==1#離されている
  end
end
def input_mousemove()
  if getVariable("mouse_moved")
    setVariable("mouse_moved",false)
    return true
  else
    return false
  end
end
def input_mousex()
  input_update()
  return getVariable("mouse_x")
end
def input_mousey()
  input_update()
  return getVariable("mouse_y")
end
def input_reset()
    setVariable("mouse_down",0)
    setVariable("mouse_x",0)
    setVariable("mouse_y",0)
    setVariable("mouse_moved",false)
    i = 0
    while i < 45
      setVariable("input_new_" + getVariable("key_char")[i],false)
      setVariable("input_" + getVariable("key_char")[i],false)
      i = i + 1
    end
end
def input_end()
  endInput()
end
#以下、ver2.1未満との互換
def input_getkeydown(key)
	return input_keydown(key)
end
def input_getkey(key)
  return input_key(key)
end
def input_getanykey()
  return input_anykey()
end
def input_getmouse(get)
  return input_mouse(get)
end
def input_getmousex()
  return input_mousex()
end
def input_getmousey()
  return input_mousey()
end

コメントする

コメントするには、ログインする必要があります。

コメント一覧

Material 305033 1 mini cfm_(投稿日:2016/01/06 15:01, 履歴)
2016.01.21 マウス操作に対応、関数名を変更。
2016.01.06 キーが新しく押下されたかの判定ができるよう、全体を変更しました。
            mini mosmoss(投稿日:2016/01/21 00:59, 履歴)
遅くにおつかれさまです。
着々と新要素が更新されていますね。
            mini mosmoss(投稿日:2016/05/08 08:46, 履歴)
最後の
def input_getmousey()
  return input_mousey
end

ですが,
input_mousey()
だと動きます!
Material 305033 1 mini cfm_(投稿日:2016/07/22 11:36, 履歴)
報告ありがとうございます、直しておきました